Files
NowChess-Frontend/src/app/services/auth.service.ts
T
shosho996 ff75c8ce2f feat: NCS-63 User account implementation (#2)
User Profile info, no game before login/register, menu bar

---------

Co-authored-by: Lala, Shahd <Shahd.Lala@sybit.de>
Co-authored-by: shahdlala66 <shahd.lala66@gmail.com>
Reviewed-on: #2
2026-05-06 10:51:30 +02:00

94 lines
2.9 KiB
TypeScript

import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject, Observable } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { environment } from '../../environments/environment';
import { LoginRequest, RegisterRequest, RegisterResponse, LoginResponse, CurrentUser } from '../models/auth.models';
@Injectable({ providedIn: 'root' })
export class AuthService {
private readonly apiBase = environment.apiBaseUrl;
private readonly accountServiceUrl = environment.accountServiceUrl;
private readonly http = inject(HttpClient);
private currentUserSubject = new BehaviorSubject<CurrentUser | null>(null);
public currentUser$ = this.currentUserSubject.asObservable();
constructor() {
this.loadCurrentUser();
}
login(username: string, password: string): Observable<LoginResponse> {
return this.http
.post<LoginResponse>(`${this.accountServiceUrl}/api/account/login`, {
username,
password
})
.pipe(
tap((response) => {
localStorage.setItem('token', response.token);
localStorage.setItem('username', username);
// After login, fetch current user info
this.getCurrentUser().subscribe();
})
);
}
register(username: string, password: string, email?: string): Observable<RegisterResponse> {
return this.http
.post<RegisterResponse>(`${this.accountServiceUrl}/api/account`, {
username,
password,
email
})
.pipe(
tap((response) => {
localStorage.setItem('username', response.username);
localStorage.setItem('userId', response.id);
this.currentUserSubject.next({
id: response.id,
username: response.username,
rating: response.rating,
createdAt: response.createdAt
});
})
);
}
getCurrentUser(): Observable<CurrentUser> {
return this.http.get<CurrentUser>(`${this.accountServiceUrl}/api/account/me`).pipe(
tap((user) => {
localStorage.setItem('username', user.username);
localStorage.setItem('userId', user.id);
this.currentUserSubject.next(user);
})
);
}
logout(): void {
localStorage.removeItem('token');
localStorage.removeItem('username');
localStorage.removeItem('userId');
this.currentUserSubject.next(null);
}
isLoggedIn(): boolean {
return !!localStorage.getItem('token');
}
private loadCurrentUser(): void {
const token = localStorage.getItem('token');
const username = localStorage.getItem('username');
const userId = localStorage.getItem('userId');
if (token && username && userId) {
// Try to verify token is still valid by fetching current user
this.getCurrentUser().subscribe({
error: () => {
// Token is invalid, clear it
this.logout();
}
});
}
}
}