Spaces:
Paused
Paused
Update flare-ui/src/app/services/auth.service.ts
Browse files
flare-ui/src/app/services/auth.service.ts
CHANGED
|
@@ -1,57 +1,65 @@
|
|
| 1 |
-
import { Injectable, inject } from '@angular/core';
|
| 2 |
-
import { HttpClient } from '@angular/common/http';
|
| 3 |
-
import { Router } from '@angular/router';
|
| 4 |
-
import { BehaviorSubject, Observable, tap } from 'rxjs';
|
| 5 |
-
|
| 6 |
-
interface LoginResponse {
|
| 7 |
-
token: string;
|
| 8 |
-
username: string;
|
| 9 |
-
}
|
| 10 |
-
|
| 11 |
-
@Injectable({
|
| 12 |
-
providedIn: 'root'
|
| 13 |
-
})
|
| 14 |
-
export class AuthService {
|
| 15 |
-
private http = inject(HttpClient);
|
| 16 |
-
private router = inject(Router);
|
| 17 |
-
|
| 18 |
-
private tokenKey = 'flare_token';
|
| 19 |
-
private usernameKey = 'flare_username';
|
| 20 |
-
|
| 21 |
-
private loggedInSubject = new BehaviorSubject<boolean>(this.hasToken());
|
| 22 |
-
public loggedIn$ = this.loggedInSubject.asObservable();
|
| 23 |
-
|
| 24 |
-
login(username: string, password: string): Observable<LoginResponse> {
|
| 25 |
-
return this.http.post<LoginResponse>('/api/login', { username, password })
|
| 26 |
-
.pipe(
|
| 27 |
-
tap(response => {
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
this.loggedInSubject.next(true);
|
| 31 |
-
})
|
| 32 |
-
);
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
logout(): void {
|
| 36 |
-
localStorage.removeItem(this.tokenKey);
|
| 37 |
-
localStorage.removeItem(this.usernameKey);
|
| 38 |
-
this.loggedInSubject.next(false);
|
| 39 |
-
this.router.navigate(['/login']);
|
| 40 |
-
}
|
| 41 |
-
|
| 42 |
-
getToken(): string | null {
|
| 43 |
-
return localStorage.getItem(this.tokenKey);
|
| 44 |
-
}
|
| 45 |
-
|
| 46 |
-
getUsername(): string | null {
|
| 47 |
-
return localStorage.getItem(this.usernameKey);
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
|
|
|
| 1 |
+
import { Injectable, inject } from '@angular/core';
|
| 2 |
+
import { HttpClient } from '@angular/common/http';
|
| 3 |
+
import { Router } from '@angular/router';
|
| 4 |
+
import { BehaviorSubject, Observable, tap } from 'rxjs';
|
| 5 |
+
|
| 6 |
+
interface LoginResponse {
|
| 7 |
+
token: string;
|
| 8 |
+
username: string;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
@Injectable({
|
| 12 |
+
providedIn: 'root'
|
| 13 |
+
})
|
| 14 |
+
export class AuthService {
|
| 15 |
+
private http = inject(HttpClient);
|
| 16 |
+
private router = inject(Router);
|
| 17 |
+
|
| 18 |
+
private tokenKey = 'flare_token';
|
| 19 |
+
private usernameKey = 'flare_username';
|
| 20 |
+
|
| 21 |
+
private loggedInSubject = new BehaviorSubject<boolean>(this.hasToken());
|
| 22 |
+
public loggedIn$ = this.loggedInSubject.asObservable();
|
| 23 |
+
|
| 24 |
+
login(username: string, password: string): Observable<LoginResponse> {
|
| 25 |
+
return this.http.post<LoginResponse>('/api/login', { username, password })
|
| 26 |
+
.pipe(
|
| 27 |
+
tap(response => {
|
| 28 |
+
this.setToken(response.token);
|
| 29 |
+
this.setUsername(response.username);
|
| 30 |
+
this.loggedInSubject.next(true);
|
| 31 |
+
})
|
| 32 |
+
);
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
logout(): void {
|
| 36 |
+
localStorage.removeItem(this.tokenKey);
|
| 37 |
+
localStorage.removeItem(this.usernameKey);
|
| 38 |
+
this.loggedInSubject.next(false);
|
| 39 |
+
this.router.navigate(['/login']);
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
getToken(): string | null {
|
| 43 |
+
return localStorage.getItem(this.tokenKey);
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
getUsername(): string | null {
|
| 47 |
+
return localStorage.getItem(this.usernameKey);
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
setToken(token: string): void {
|
| 51 |
+
localStorage.setItem(this.tokenKey, token);
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
setUsername(username: string): void {
|
| 55 |
+
localStorage.setItem(this.usernameKey, username);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
hasToken(): boolean {
|
| 59 |
+
return !!this.getToken();
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
isLoggedIn(): boolean {
|
| 63 |
+
return this.hasToken();
|
| 64 |
+
}
|
| 65 |
}
|