Why does my csrf token expires as soon as I try to use it?
I'm having a weird problem with the XSRF token and session token I receive and send back with Angular.
When I make a request to the endpoint sanctum/csrf, I do get a XSRF-TOKEN and SESSION-TOKEN with a 2h lifetime as a response. But on subsequent request to a laravel route, the tokens I send are set to be expired the moment I received them? Resulting in a "csrf toekn mismatch" error. The tokens I receive and the ones I use in further requests are indeed the same. But the expiration time changes it seems. I'm lost there...
constructor(private testService: TestService, private http: HttpClient) { }
ngOnInit(): void {
this.http.get('http://localhost:8000/sanctum/csrf-cookie', { withCredentials: true }).subscribe(() => {
console.log('CSRF cookie set.');
this.http.post('http://localhost:8000/api/login', {
email: 'exemple@gmail.com',
password: 'exemple-'
}, { withCredentials: true }).subscribe((response) => {
console.log(response);
this.http.get('http://localhost:8000/api/user', { withCredentials: true }).subscribe((response => {
console.log(response);
}));
});
this is the code I'm using. The first get method sends me the token, 2h lifetime. The next post method uses the tokens, but the expire date is set to the exact time I received it. I guess the "csrf token mismatch" error I get comes from this.
Any clue?