diff --git a/apps/frontend/src/app/services/auth.service.ts b/apps/frontend/src/app/services/auth.service.ts index d8697b7..ec7c2c4 100644 --- a/apps/frontend/src/app/services/auth.service.ts +++ b/apps/frontend/src/app/services/auth.service.ts @@ -23,6 +23,7 @@ export class AuthService { private currentUser = signal(null); public readonly user = this.currentUser.asReadonly(); private refreshing = false; + private refreshInterval?: ReturnType; login(): void { // Redirect to API login endpoint @@ -33,6 +34,7 @@ export class AuthService { return this.api.get('/auth/me').pipe( tap(response => { this.currentUser.set(response.user); + this.startRefreshTimer(); }), catchError(error => { if (error.status === 401) { @@ -40,9 +42,11 @@ export class AuthService { switchMap(() => this.api.get('/auth/me')), tap(response => { this.currentUser.set(response.user); + this.startRefreshTimer(); }), catchError(() => { this.currentUser.set(null); + this.stopRefreshTimer(); return throwError(() => error); }) ); @@ -66,20 +70,45 @@ export class AuthService { tap(response => { this.currentUser.set(response.user); this.refreshing = false; + this.startRefreshTimer(); }), catchError(error => { this.refreshing = false; this.currentUser.set(null); + this.stopRefreshTimer(); return throwError(() => error); }) ); } + private startRefreshTimer(): void { + this.stopRefreshTimer(); + + // Refresh token every 13 minutes (before 15-minute expiry) + const refreshIntervalMs = 13 * 60 * 1000; + this.refreshInterval = setInterval(() => { + this.refreshToken().subscribe({ + error: (err) => { + console.error('Background token refresh failed:', err); + this.stopRefreshTimer(); + } + }); + }, refreshIntervalMs); + } + + private stopRefreshTimer(): void { + if (this.refreshInterval) { + clearInterval(this.refreshInterval); + this.refreshInterval = undefined; + } + } + logout(): Observable<{ message: string }> { return this.api.post<{ message: string }>('/auth/logout', {}).pipe( tap(() => { this.currentUser.set(null); this.api.clearCsrfToken(); + this.stopRefreshTimer(); this.router.navigate(['/']); }) ); @@ -87,6 +116,7 @@ export class AuthService { clearUser(): void { this.currentUser.set(null); + this.stopRefreshTimer(); } isAuthenticated(): boolean {