Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Login Local API res object

default discord avatar
duckduckduckgooselast year
20

Hi, I've looked at the docs where it says

  res: res, // used to automatically set an HTTP-only auth cookie

.



So I wrote the following login function in a server action:

export async function onLoginSubmit(values) {

    let req = new Request('http:localhost:3000/api/users/login');
    let res = new Response();

    const result = await payload.login({
        collection: 'users',
        data: values,
        req: req,
        res: res
    })
    return result
}

.



However this hasn't correctly set the http-only cookie. Can anyone help explain why. I have a feeling I haven't managed

res

correctly?

  • default discord avatar
    tyteen4a03last year
    res

    should be passed to you by Payload (check function arguments), you should not create it yourself

  • default discord avatar
    duckduckduckgooselast year

    Sorry I'm probably being a bit daft. How does payload provide res to me?



    If you mean the function arguments of

    onLoginSubmit

    , I wrote that function myself, and where I call it from has no way of accessing payload (is a client-side component)?

  • default discord avatar
    tyteen4a03last year

    How is the function supposed to be invoked?

  • default discord avatar
    duckduckduckgooselast year

    Login button in client component calls

    onLoginSubmit

    which is a server action



    I guess I could just do REST login in client, but surely local api is the better way to do it?

  • default discord avatar
    tyteen4a03last year

    You don't have access to

    payload

    in a client component

  • default discord avatar
    duckduckduckgooselast year

    Sorry am I missing something. You can't use payload in a client component right?

  • default discord avatar
    tyteen4a03last year

    Nope

  • default discord avatar
    duckduckduckgooselast year

    Yep, hence why the server action.

  • default discord avatar
    tyteen4a03last year
    https://payloadcms.com/docs/authentication/operations

    this is the rest API for logging in, I presume this is for the frontend?

  • default discord avatar
    duckduckduckgooselast year

    Yes frontend login.

  • default discord avatar
    tyteen4a03last year

    hmm so the flow is 1) you have a login button that triggers a server action; 2) the server action logs the user in and sets the cookie?

  • default discord avatar
    duckduckduckgooselast year

    Ideally yes. In reality no cookie is set.

  • default discord avatar
    tyteen4a03last year

    I think if you're in a server action context you'll need to call

    cookies()

    manually from nextjs

  • default discord avatar
    duckduckduckgooselast year

    Does cookies work with http-only?



    IE could read it

  • default discord avatar
    duckduckduckgooselast year

    Ok sure. So the plan would be, make the request in server actions, I will recieve a response from payload with the http-only cookie. And then I use this next.js function to read the cookie and move it onto the client side?

  • default discord avatar
    tyteen4a03last year

    oh, you

    can

    use local API in the server action if your payload app is the same app as the frontend, you call

    getPayload()

    to get a copy of payload, then use

    payload.login()

    https://payloadcms.com/docs/local-api/overview#login

    just that iirc you need to take care of actually setting the cookie

  • default discord avatar
    duckduckduckgooselast year

    Ok thanks, I'll give it a go and lyk whether it works!

  • default discord avatar
    archvile5168last year
    https://github.com/payloadcms/payload/issues/9829

    export type Options<TSlug extends CollectionSlug> = {
        collection: TSlug;
        context?: RequestContext;
        data: AuthOperationsFromCollectionSlug<TSlug>['login'];
        depth?: number;
        fallbackLocale?: string;
        locale?: string;
        overrideAccess?: boolean;
        req?: Partial<PayloadRequest>;
        showHiddenFields?: boolean;
    };


    @114557048678514693


    The Local API for

    login

    : it does NOT have a

    res

    field...,


    So how can I set the cookie automatically? The documentation is wrong?



    Edit:


    Yes I can use the Local API and write the cookie like this:


          const cookieStore = await cookies()
    
          cookieStore.set({
            name: 'payload-token',
            value: result.token,
            httpOnly: true,
            secure: process.env.NODE_ENV === 'production',
            path: '/',
            sameSite: 'strict',
          })

    but the docs is just not correct.

  • default discord avatar
    tyteen4a03last year

    yeah they should fix it

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.