From ede8581ae8fb13d9903ac5910b082310383ca652 Mon Sep 17 00:00:00 2001 From: Iana Kaminska <65045059+Takewkat@users.noreply.github.com> Date: Wed, 24 Aug 2022 14:11:39 +0200 Subject: [PATCH] update auth.service.ts When the page is refreshed, the user is logged out every time This makes it difficult to complete the student's task (project 6) --- src/app/services/auth.service.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app/services/auth.service.ts b/src/app/services/auth.service.ts index 30011df65..eec64457f 100644 --- a/src/app/services/auth.service.ts +++ b/src/app/services/auth.service.ts @@ -8,7 +8,7 @@ import { Router } from '@angular/router'; }) export class AuthService { - isAuth$ = new BehaviorSubject(false); + isAuth$ = new BehaviorSubject(localStorage.getItem('authToken') ? true : false); private authToken = ''; private userId = ''; @@ -20,7 +20,7 @@ export class AuthService { } getToken() { - return this.authToken; + return localStorage.getItem('authToken'); } getUserId() { @@ -31,14 +31,14 @@ export class AuthService { return this.http.post<{ userId: string, token: string }>('http://localhost:3000/api/auth/login', {email: email, password: password}).pipe( tap(({ userId, token }) => { this.userId = userId; - this.authToken = token; + localStorage.setItem('authToken', token); this.isAuth$.next(true); }) ); } logout() { - this.authToken = ''; + localStorage.removeItem('authToken'); this.userId = ''; this.isAuth$.next(false); this.router.navigate(['login']);