SANCTUM - After page refreshing user not logged in - Angular20 x Laravel12 #56747
Replies: 2 comments 1 reply
-
The Angular login calls /sanctum/csrf-cookie first and uses withCredentials: true. Laravel Auth::attempt handles login, no need for Auth::login. In the AuthGuard, check user.id instead of username to correctly verify login. Make sure cookies and CSRF token are properly set. |
Beta Was this translation helpful? Give feedback.
-
However, i fixed this Issue due to the middleware protection from auth:sanctum for my route auth/current-user with doing this lmao: and it works just perfectly fine |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
So if i refresh my page which is secured by my auth-guard and checks if the user is logged in, i get is not logged in from my backend laravel Auth controller. I dont know where to ask this question so im doing it here if someone had the same or an issue like this.
So if I press on the button after the login go to dashboard it does work and the user check does work:
[2025-08-25 13:56:04] local.INFO: Session dump: {"_token":"xJFXXnpsBc6iuXnU1HmG5Qa4N2axnXc55ZIg2xBk","_previous":{"url":"http://localhost:8000/sanctum/csrf-cookie"},"_flash":{"old":[],"new":[]},"login_web_59ba36addc2b2f9401580f014c7f58ea4e30989d":15} [2025-08-25 13:56:04] local.INFO: checkCurrentUser: User is logged in {"user_id":15}
But if i try to edit the link from localhost:4200/login to locahost:4200 or refresh the page localhost:4200 it does not work and send me back to the login and it logs a complete new Session dump _token:
[2025-08-25 13:57:49] local.INFO: Session dump: {"_token":"MLMlhAfg8U9tL1Dtv6e0IJIzHrwL41O12iU9dq1q"} [2025-08-25 13:57:49] local.INFO: checkCurrentUser: No user logged in
I dont know what im doing wrong tbh - Every help appreciated!
My files:
login.ts
AuthController:
auth-guard.ts:
import { Routes } from '@angular/router';
import { Dashboard } from './components/dashboard/dashboard';
import { WeeklySchedule } from './components/tools/weekly-schedule-tool/weekly-schedule/weekly-schedule';
import { Login } from './components/auth/login/login';
import { UserManagement } from './components/user-management/user-management';
import { authGuard } from './core/guards/auth-guard';
export const routes: Routes = [
{
path: 'login',
component: Login,
title: 'Login to KAT-Dashboard',
data: { headerType: 'login', headerTitle: 'KAT-Dashboard' }
},
];
SESSION_DRIVER=cookie
SESSION_LIFETIME=540
SESSION_ENCRYPT=true
SESSION_PATH=/
SESSION_DOMAIN=localhost
SANCTUM_STATEFUL_DOMAINS=localhost,localhost:8000,localhost:4200
SESSION_SAME_SITE=lax
SESSION_SECURE_COOKIE=false
'paths' => ['api/*', 'sanctum/csrf-cookie', 'login', 'auth/current-user'],
Beta Was this translation helpful? Give feedback.
All reactions