dystcz / nuxt-sanctum-auth

Nuxt 3 + laravel sanctum authentication
134 stars 25 forks source link

XSRF cookie not being set #12

Closed JoshPJackson closed 1 year ago

JoshPJackson commented 1 year ago

Great package! thanks for making it. I'm having trouble logging in with my laravel backend. Using the login example code:

async function login() {
    try {
        await $sanctumAuth.login(
            {
                email: email.value,
                password: password.value
            },
            // optional callback function
            (data) => {
                console.log(data)
                router.push('/')
            }
        )
    } catch (e) {
        // your error handling
        errors.value = e.errors
    }
}

I can see the request to grab the xsrf cookie:

Screenshot 2023-04-01 200013

But the cookie doesn't seem to be set. When I inspect the login request the x-xsrf-token field is empty:

Screenshot 2023-04-01 200222 Screenshot 2023-04-01 200237

It looks like the cookie isn't being set for some reason. I've followed the default installation instructions. Can you help?

AndreasHerss commented 1 year ago

If you look at the request to the /csrf-token endpoint under the Set-Cookie you will see a little exclamation mark, if you hover it you will see the errors.

The problem is your site is being served over HTTP, on localhost. and therefore the Set-Cookie is being blocked due to the same-site settings on your cookie.

You need to serve your nuxt app over https, and loosen the cookie samesite restrictions :)

kreejzak commented 1 year ago

Hi @JoshPJackson, what are the contents of your SESSION_DOMAINS and SANCTUM_STATEFUL_DOMAINS? Have you specified localhost:3000 as one of the entries?

SESSION_DOMAINS=localhost,localhost:8000,localhost:3000,127.0.0.1,127.0.0.1:8000,::1
SANCTUM_STATEFUL_DOMAINS=localhost,localhost:8000,localhost:3000,127.0.0.1,127.0.0.1:8000,::1

If not, this could solve the problem with same-site cookie setting.

Hope this helps.

JoshPJackson commented 1 year ago

Ah ok, looks like it is my end then. Thanks for coming back to me @kreejzak and @AndreasHerss 👍