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.

Payload Auth Login not setting HTTP-only cookie

default discord avatar
realluciano2 years ago
10

Hello everyone,



Trust you are well & having a great day!



What do I need to do to allow this code to set a cookie?



result = await payload.login({ collection: 'accounts', data: { email: email, password: password, }, });

Thank you!

  • default discord avatar
    realluciano2 years ago

    Thank you for sending me this file, you are correct the code does show that, however no cookie is being attached to the res on my end!

  • discord user avatar
    jarrod_not_jared
    2 years ago

    do you have auth.cookies.domains configured by chance? (on your auth collection config)

  • default discord avatar
    realluciano2 years ago

    Yes,

    auth.cookies.domain: 'localhost:8080'
  • discord user avatar
    jarrod_not_jared
    2 years ago

    and that is you CMS domain?

  • default discord avatar
    realluciano2 years ago

    For my development environment, yes.

  • discord user avatar
    jarrod_not_jared
    2 years ago

    curious if the same thing happens when you remove that (I believe it will act the same)

  • default discord avatar
    realluciano2 years ago

    You are right, removing

    auth.cookies.domain

    makes it act as before



    Is the code at

    https://payloadcms.com/docs/authentication/operations#login

    meant to be implemented on the front end? Because mine is run on the backend



    After setting the cookie manually & visiting

    http://localhost:8080/api/accounts/me

    , that works. The Payload Auth Middleware however is not working when checking

    req.user

    @281120856527077378

    just want to confirm that Auth Login is now setting a cookie once I added payload.login

    res: res,

    & removed

    auth.cookies.domain

    Thank you for your help!

  • discord user avatar
    jarrod_not_jared
    2 years ago

    That is great, thank you for following up

  • default discord avatar
    realluciano2 years ago

    Also, to anyone who is wondering how to implement the Payload Auth Middleware, follow this code

    https://discord.com/channels/967097582721572934/1070942828009627698/1071720017068036106
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.