|
1 | | -import { |
2 | | - GoogleLoginProvider, |
3 | | - SocialAuthService, |
4 | | - SocialUser, |
5 | | -} from '@abacritt/angularx-social-login'; |
| 1 | +import { SocialAuthService, SocialUser } from '@abacritt/angularx-social-login'; |
6 | 2 | import { HttpClient } from '@angular/common/http';
|
7 | 3 | import { Injectable } from '@angular/core';
|
8 | 4 | import { Router } from '@angular/router';
|
9 | 5 | import { BehaviorSubject, Observable, map } from 'rxjs';
|
10 | 6 | import { environment } from 'src/environments/environment';
|
11 | 7 |
|
| 8 | +/** |
| 9 | + * AuthService |
| 10 | + * |
| 11 | + * This service handles authentication-related functionalities, including login, logout, |
| 12 | + * token management, and checking the authentication status. It interacts with the |
| 13 | + * `@abacritt/angularx-social-login` library to handle social logins and stores the |
| 14 | + * login state using BehaviorSubject. It also provides methods to refresh and validate |
| 15 | + * tokens with the backend auth server. The service is provided at the root level to ensure |
| 16 | + * a singleton instance throughout the application. |
| 17 | + */ |
12 | 18 | @Injectable({
|
13 | 19 | providedIn: 'root',
|
14 | 20 | })
|
@@ -51,41 +57,31 @@ export class AuthService {
|
51 | 57 | this.loginUserSubject.next(user);
|
52 | 58 | }
|
53 | 59 |
|
| 60 | + //log in the user with a token and update the authentication status |
54 | 61 | login(token: string): void {
|
55 | 62 | localStorage.setItem('token', token);
|
56 | 63 | this.isAuthenticatedSubject.next(true);
|
57 | 64 | }
|
58 | 65 |
|
| 66 | + // log out the user, clear the token, and navigate to the login page |
59 | 67 | logout(): void {
|
60 | 68 | localStorage.removeItem('token');
|
61 | 69 | this.isAuthenticatedSubject.next(false);
|
62 | 70 | this.authService.signOut();
|
63 | 71 | this.router.navigate(['/login']);
|
64 | 72 | }
|
65 | 73 |
|
| 74 | + // check if the user is authenticated based on the presence of a token |
66 | 75 | isAuthenticated(): boolean {
|
67 | 76 | const token = localStorage.getItem('token');
|
68 | 77 | return !!token;
|
69 | 78 | }
|
70 | | - |
| 79 | +// get the stored token from local storage |
71 | 80 | getToken(): string {
|
72 | 81 | return localStorage.getItem('token') || '';
|
73 | 82 | }
|
74 | 83 |
|
75 | | - refreshToken() { |
76 | | - const encodedIdToken = encodeURIComponent(this.getToken()); |
77 | | - return this.http |
78 | | - .get<any>( |
79 | | - `https://oauth2.googleapis.com/tokeninfo?id_token=${encodedIdToken}` |
80 | | - ) |
81 | | - .pipe( |
82 | | - map((user) => { |
83 | | - this.loginUserSubject.next(user); |
84 | | - return user; |
85 | | - }) |
86 | | - ); |
87 | | - } |
88 | | - |
| 84 | + // validate the user's token with the backend server |
89 | 85 | public validateToken(): Observable<any> {
|
90 | 86 | const token = encodeURIComponent(this.getToken());
|
91 | 87 | return this.http.post<any>(`${environment.auth_uri}validate-token`, {
|
|
0 commit comments