nuxt-community / auth-module

Zero-boilerplate authentication support for Nuxt 2
https://auth.nuxtjs.org
MIT License
1.93k stars 925 forks source link

Sanctum Providers : does not persist CSRF Token through requests #1355

Closed vwasteels closed 2 years ago

vwasteels commented 2 years ago

Version

module: 5.0.0 nuxt: 2.15.8

Nuxt configuration

mode:

Nuxt configuration

  axios: {
    proxy: true,
    credentials: true,
    debug: process.env.DEBUG,
  },

  // NOTE: proxy is used to prevent CORS issues during development
  proxy: {
    '/api': {
      target: process.env.API_DOMAIN,
      pathRewrite: { '^/api': '/' }
    }
  },

  auth: {
    strategies: {
      laravelSanctum: {
        provider: 'laravel/sanctum',
        url: '/api',
      }
    },
  },

Checklist

Reproduction

What is expected?

POST on /login should reach API corectly

What is actually happening?

It return a 419 with Exception : CSRF token mismatch.

Steps to reproduce

I followed this issue for more documentation : https://github.com/nuxt-community/auth-module/issues/1164#issuecomment-839199946

First call to /csfr-cookie woks well and returns 2 cookies on domain .indivisible.test Screenshot -  2021-12-07 à 15 53 03

2nd call to the login endpoint, tiggered this way :

    this.$auth.loginWith('laravelSanctum', {
        data: {
          email: 'someone@email.com',
          password: 'somepassword'
        }
      })

returns a 419 with Exception : CSRF token mismatch. Screenshot -  2021-12-07 à 15 53 28

with no Cookie present in the Resquest Headers, which seems normal since the previous SET-COOKIE was calling a different domain than localhost, but then, how is it suppose to work ?

On the Laravel side :

Thanks a lot for your help, I'm very stucked on this !!

vwasteels commented 2 years ago

any thought on this ? I am still stuck :( Thank you anyway

T-Zahil commented 2 years ago

Any news ? :/

vwasteels commented 2 years ago

yes I made it work by adding a ngynx proxy to serve nuxt on the same top-level domain than the API

this reading helped me understanding : https://dev.to/nicolus/laravel-sanctum-explained-spa-authentication-45g1

here is my ngynx config for this :

server {
    server_name mynuxtsite.mytoploveldomain.com;
    listen 127.0.0.1:80;
    location / {
            proxy_pass      http://localhost:3000;
            proxy_set_header    Host             $host;
            proxy_set_header    X-Real-IP        $remote_addr;
            proxy_set_header    X-Forwarded-For    $proxy_add_x_forwarded_for;
            proxy_set_header    X-Client-Verify  SUCCESS;
            proxy_set_header    X-Client-DN      $ssl_client_s_dn;
            proxy_set_header    X-SSL-Subject    $ssl_client_s_dn;
            proxy_set_header    X-SSL-Issuer     $ssl_client_i_dn;
            proxy_read_timeout 1800;
            proxy_connect_timeout 1800;
            chunked_transfer_encoding on;
            proxy_set_header X-NginX-Proxy true;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_http_version 1.1;
            proxy_redirect off;
            proxy_buffering off;
    }
}
T-Zahil commented 2 years ago

Thanks a lot @vwasteels !

bmulholland commented 2 years ago

Looks like this was resolved by fixing the laravel hosting setup, so closing it out.

jerickcm commented 2 years ago

Im facing the same issue in my local development can anyone help me with this