79642137

Date: 2025-05-28 11:59:14
Score: 1
Natty:
Report link

The 401 Unauthorized error or missing cookies when using Laravel Sanctum with React is likely due to CORS or CSRF issues. Ensure:

Laravel:

React (Axios):

import axios from 'axios';

const apiClient = axios.create({
    baseURL: 'http://127.0.0.1:8000',
    withCredentials: true,
    withXSRFToken: true,
});

const login = async (email, password) => {
    await apiClient.get('/sanctum/csrf-cookie');
    await apiClient.post('/login', { email, password });
    const user = await apiClient.get('/user');
    return user.data;
};

Check browser DevTools for XSRF-TOKEN and laravel_session cookies.

Reasons:
  • Blacklisted phrase (1): thX
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: bhavdeep