kubaK's avatar
Level 1

Vue + Laravel Sanctum auth error

Hi, im learning to create a vue SPA with sanctum auth. i built a bit of my api and was testing with postman, adding the token for each protected post and get. That was working all fine. But now i have problems integrating my api into vite.

i can login using this method and get the cookie stored in my browser:

async login() {
     try {
       axios.get(MY_URL/sanctum/csrf-cookie', { withCredentials: true }).then(response => {
         console.log(response);
         axios.post('MY_URL/api/login', this.formData).then(response => {
           console.log('success logged in');
           this.$router.push('/admin')
         });
       });
     } catch (error) {
       console.log('error')
     }
   }

However after logging in i get unauthorized 401 on my GET request using:

  const data = axios.get('MY_URL/api/contracts');
  console.log(data);

I have been thinking that its a problem with my sanctum stateful domains My vite is running on: localhost:5173, my laravel API running on: localhost:8000

from what ive read my stateful domain should then be localhost:5173 but if i enter localhost with port into my stateful_domain i get 419 error. Without port number it works fine. Could this be the problem?

SANCTUM_STATEFUL_DOMAINS = "localhost"

// instead of:
SANCTUM_STATEFUL_DOMAINS = "localhost:5173"
0 likes
2 replies
Thunderson's avatar

check the axios configuration have you windows.axios.defaults.withCredentials = true

kubaK's avatar
Level 1

@Thunderson i have, cookies get stored but all get request that are protected come back unautherized

Please or to participate in this conversation.