I'm using Laravel Sanctum on a LAMP Stack. I have my frontend react app pointed to
/var/www/app.example.com
and my backend Laravel pointed to /var/www/appapi.example.com
on the same server. Both load fine.
I am currently building off of this tutorial - https://dev.to/dog_smile_factory/series/5857
If you open the dev tools and follow its workflow, you can register for a new user, be logged in, and then it will auto attempt to hit the api/users route - which always returns unauthenticated
.
Even with everything as wide open as I can figure, I'm not getting through - here is what I have:
CORS.php
'paths' => ['api/*', 'sanctum/csrf-cookie', '*'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => true,
.env
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:2tD+oAGu+NOPE+NOPE+NOPE+gq9brRpfuKCL+t4M=
APP_DEBUG=true
APP_URL=appapi.example.com
LOG_CHANNEL=stack
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=apppue
DB_USERNAME=root
DB_PASSWORD=NopeNopeNope
BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=cookie
SESSION_LIFETIME=120
MEMCACHED_HOST=127.0.0.1
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
SESSION_DOMAIN=.example.com
SANCTUM_STATEFUL_DOMAINS=.example.com
kernel.php
'api' => [
EnsureFrontendRequestsAreStateful::class,
'throttle:60,1',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
api.php
Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
return $request->user();
});
Route::post('/login', 'UserController@login');
Route::post('/register', 'UserController@register');
Route::get('/logout', 'UserController@logout');
On my React App
All axios requests look like this (withCredentials = true
):
axios.defaults.withCredentials = true;
// CSRF COOKIE
axios.get(hostName + "sanctum/csrf-cookie").then(
(response) => {
//console.log(response);
// SIGNUP / REGISTER
axios
.post(hostName + "api/register", {
name: userNameInput,
email: userEmail,
password: userPassword,
})
.then(
(response) => {
//console.log(response);
// GET USER
axios.get(hostName + "api/user").then(
(response) => {
//console.log(response);
setUserId(response.data.id);
setUserName(response.data.name);
setErrorMessage("");
setAuthStatus(LOGGED_IN);
},
// GET USER ERROR
(error) => {
setErrorMessage("Could not complete the sign up");
}
);
},
// SIGNUP ERROR
(error) => {
if (error.response.data.errors.name) {
setErrorMessage(error.response.data.errors.name[0]);
} else if (error.response.data.errors.email) {
setErrorMessage(error.response.data.errors.email[0]);
} else if (error.response.data.errors.password) {
setErrorMessage(error.response.data.errors.password[0]);
} else if (error.response.data.message) {
setErrorMessage(error.response.data.message);
} else {
setErrorMessage("Could not complete the sign up");
}
}
);
},
// COOKIE ERROR
(error) => {
setErrorMessage("Could not complete the sign up");
}
);
};
The x-xrfs-token is getting saved correctly from what I can tell and passed in with each of the requests. Maybe I'm misunderstanding something about this though and that's why I can't hit my authenticated route api/users
.
I've been working on this for 4 days following several tutorials, laravel docs, and scavaging the web - somehow I'm still missing something. Most tutorials are doing this on localhost
and I'm configuring it on a LAMP stack. It's the only piece I see that is different. What am I doing wrong?