diff --git a/src/app/auth.guard.spec.ts b/src/app/auth.guard.spec.ts index ede04a8..596fc7e 100644 --- a/src/app/auth.guard.spec.ts +++ b/src/app/auth.guard.spec.ts @@ -1,17 +1,17 @@ import { TestBed } from '@angular/core/testing'; import { Router } from '@angular/router'; import { AuthGuard } from './auth.guard'; -import { LocalStorageService } from './services/cookies/cookies.service'; +import { CookiesService } from './services/cookies/cookies.service'; import { ModalService } from './services/modal/modal.service'; describe('AuthGuard', () => { let guard: AuthGuard; - let localStorageServiceSpy: jasmine.SpyObj; + let cookiesServiceSpy: jasmine.SpyObj; let routerSpy: jasmine.SpyObj; let loginModalServiceSpy: jasmine.SpyObj; beforeEach(() => { - localStorageServiceSpy = jasmine.createSpyObj('LocalStorageService', [ + cookiesServiceSpy = jasmine.createSpyObj('CookiesService', [ 'getToken', ]); routerSpy = jasmine.createSpyObj('Router', ['navigate']); @@ -22,7 +22,7 @@ describe('AuthGuard', () => { TestBed.configureTestingModule({ providers: [ AuthGuard, - { provide: LocalStorageService, useValue: localStorageServiceSpy }, + { provide: CookiesService, useValue: cookiesServiceSpy }, { provide: Router, useValue: routerSpy }, { provide: ModalService, useValue: loginModalServiceSpy }, ], @@ -32,13 +32,13 @@ describe('AuthGuard', () => { }); it('should allow activation when token exists', () => { - localStorageServiceSpy.getToken.and.returnValue('valid-token'); + cookiesServiceSpy.getToken.and.returnValue('valid-token'); const result = guard.canActivate(); expect(result).toBeTrue(); }); it('should deny activation and trigger redirect and modal when token is missing', async () => { - localStorageServiceSpy.getToken.and.returnValue(null); + cookiesServiceSpy.getToken.and.returnValue(null); routerSpy.navigate.and.returnValue(Promise.resolve(true)); const result = guard.canActivate(); diff --git a/src/app/components/login-page/login-page.component.ts b/src/app/components/login-page/login-page.component.ts index 918317b..b318ada 100644 --- a/src/app/components/login-page/login-page.component.ts +++ b/src/app/components/login-page/login-page.component.ts @@ -10,7 +10,6 @@ import { import { Router } from '@angular/router'; import { Subscription } from 'rxjs'; import { User } from '../../model/User'; -import { LocalStorageService } from '../../services/cookies/cookies.service'; import { ModalService } from '../../services/modal/modal.service'; import { AuthService } from '../../services/auth/auth.service'; @@ -31,8 +30,7 @@ export class LoginPageComponent { private authService: AuthService, private fb: FormBuilder, private router: Router, - private modalService: ModalService, - private localStorageService: LocalStorageService + private modalService: ModalService ) { this.userForm = this.fb.group({ login: [this.user.login, [Validators.required, Validators.minLength(3)]], diff --git a/src/app/services/auth/auth.service.ts b/src/app/services/auth/auth.service.ts index b23036a..74f0b00 100644 --- a/src/app/services/auth/auth.service.ts +++ b/src/app/services/auth/auth.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; import { BehaviorSubject, Observable, tap } from 'rxjs'; import { environment } from '../../../environment'; -import { LocalStorageService } from '../cookies/cookies.service'; +import { CookiesService } from '../cookies/cookies.service'; import { AuthResponse } from '../../model/AuthResponse'; @Injectable({ @@ -14,11 +14,11 @@ export class AuthService { username$ = new BehaviorSubject(''); isLoggedIn$ = new BehaviorSubject(false); - constructor(private http: HttpClient, private localStorageService: LocalStorageService) { - const token = this.localStorageService.getToken(); + constructor(private http: HttpClient, private cookiesService: CookiesService) { + const token = this.cookiesService.getToken(); if (token) { - this.isAdminSubject.next(this.localStorageService.getIsAdmin() === 'true'); - this.username$.next(this.localStorageService.getUsername() || ''); + this.isAdminSubject.next(this.cookiesService.getIsAdmin() === 'true'); + this.username$.next(this.cookiesService.getUsername() || ''); this.isLoggedIn$.next(true); } } @@ -30,9 +30,9 @@ export class AuthService { return this.http.post(`${environment.apiURL}/login`, payload).pipe( tap(response => { - this.localStorageService.setToken(response.access_token); - this.localStorageService.setIsAdmin(response.is_admin); - this.localStorageService.setUsername(username); + this.cookiesService.setToken(response.access_token); + this.cookiesService.setIsAdmin(response.is_admin); + this.cookiesService.setUsername(username); this.isAdminSubject.next(response.is_admin); this.username$.next(username); this.isLoggedIn$.next(true); @@ -41,8 +41,8 @@ export class AuthService { } logout(): void { - this.localStorageService.removeToken(); - this.localStorageService.removeIsAdmin(); + this.cookiesService.removeToken(); + this.cookiesService.removeIsAdmin(); this.isAdminSubject.next(false); this.username$.next(''); this.isLoggedIn$.next(false); @@ -51,9 +51,9 @@ export class AuthService { register(username: string, password: string): Observable { return this.http.post(`${environment.apiURL}/register`, { username, password }).pipe( tap(response => { - this.localStorageService.setToken(response.access_token); - this.localStorageService.setIsAdmin(response.is_admin); - this.localStorageService.setUsername(username); + this.cookiesService.setToken(response.access_token); + this.cookiesService.setIsAdmin(response.is_admin); + this.cookiesService.setUsername(username); this.isAdminSubject.next(response.is_admin); this.username$.next(username); this.isLoggedIn$.next(true); @@ -66,7 +66,7 @@ export class AuthService { } getAuthHeaders(): HttpHeaders { - const token = this.localStorageService.getToken(); + const token = this.cookiesService.getToken(); return new HttpHeaders().set('Authorization', `Bearer ${token}`); } diff --git a/src/app/services/cookies/cookies.service.ts b/src/app/services/cookies/cookies.service.ts index bd49639..5686393 100644 --- a/src/app/services/cookies/cookies.service.ts +++ b/src/app/services/cookies/cookies.service.ts @@ -6,7 +6,7 @@ import { CookieService } from 'ngx-cookie-service'; @Injectable({ providedIn: 'root', }) -export class LocalStorageService { +export class CookiesService { private readonly AUTH_TOKEN_KEY = 'auth_token'; private readonly USERNAME_KEY = 'username'; private readonly IS_ADMIN_KEY = 'isAdmin'; @@ -52,7 +52,6 @@ export class LocalStorageService { return null; } } - console.log("Token", token); return token || null; }