Skip to content

Commit dce77b9

Browse files
author
Bezael
committed
Refactor authService
1 parent 1b8518a commit dce77b9

File tree

5 files changed

+21
-33
lines changed

5 files changed

+21
-33
lines changed

src/app/pages/auth/auth.service.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,25 @@ const helper = new JwtHelperService();
1414
providedIn: 'root',
1515
})
1616
export class AuthService {
17-
private loggedIn = new BehaviorSubject<boolean>(false);
18-
private role = new BehaviorSubject<Roles>(null);
19-
private userToken = new BehaviorSubject<string>(null);
17+
private user = new BehaviorSubject<UserResponse>(null);
18+
2019
constructor(private http: HttpClient, private router: Router) {
2120
this.checkToken();
2221
}
23-
24-
get isLogged(): Observable<boolean> {
25-
return this.loggedIn.asObservable();
22+
get user$(): Observable<UserResponse> {
23+
return this.user.asObservable();
2624
}
2725

28-
get isAdmin$(): Observable<string> {
29-
return this.role.asObservable();
26+
get userValue(): UserResponse {
27+
return this.user.getValue();
3028
}
31-
32-
get userTokenValue(): string {
33-
return this.userToken.getValue();
34-
}
35-
3629
login(authData: User): Observable<UserResponse | void> {
3730
return this.http
3831
.post<UserResponse>(`${environment.API_URL}/auth/login`, authData)
3932
.pipe(
4033
map((user: UserResponse) => {
4134
this.saveLocalStorage(user);
42-
this.loggedIn.next(true);
43-
this.role.next(user.role);
44-
this.userToken.next(user.token);
35+
this.user.next(user);
4536
return user;
4637
}),
4738
catchError((err) => this.handlerError(err))
@@ -50,9 +41,7 @@ export class AuthService {
5041

5142
logout(): void {
5243
localStorage.removeItem('user');
53-
this.loggedIn.next(false);
54-
this.role.next(null);
55-
this.userToken.next(null);
44+
this.user.next(null);
5645
this.router.navigate(['/login']);
5746
}
5847

@@ -65,9 +54,7 @@ export class AuthService {
6554
if (isExpired) {
6655
this.logout();
6756
} else {
68-
this.loggedIn.next(true);
69-
this.role.next(user.role);
70-
this.userToken.next(user.token);
57+
this.user.next(user);
7158
}
7259
}
7360
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
<h1 *ngIf="authSvc.isLogged | async">Welcome, user</h1>
1+
<h1 *ngIf="authSvc.user$ | async">Welcome, user</h1>
22
<p>home works!</p>

src/app/shared/components/header/header.component.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { UserResponse } from './../../models/user.interface';
12
import {
23
Component,
34
OnInit,
@@ -26,13 +27,12 @@ export class HeaderComponent implements OnInit, OnDestroy {
2627
constructor(private authSvc: AuthService) {}
2728

2829
ngOnInit(): void {
29-
this.authSvc.isLogged
30+
this.authSvc.user$
3031
.pipe(takeUntil(this.destroy$))
31-
.subscribe((res) => (this.isLogged = res));
32-
33-
this.authSvc.isAdmin$
34-
.pipe(takeUntil(this.destroy$))
35-
.subscribe((res: Roles) => (this.isAdmin = res));
32+
.subscribe((user: UserResponse) => {
33+
this.isLogged = true;
34+
this.isAdmin = user?.role;
35+
});
3636
}
3737

3838
ngOnDestroy(): void {

src/app/shared/guards/check-login.guard.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { UserResponse } from './../models/user.interface';
12
import { Injectable } from '@angular/core';
23
import { CanActivate } from '@angular/router';
34
import { Observable } from 'rxjs';
@@ -11,9 +12,9 @@ export class CheckLoginGuard implements CanActivate {
1112
constructor(private authSvc: AuthService) {}
1213

1314
canActivate(): Observable<boolean> {
14-
return this.authSvc.isLogged.pipe(
15+
return this.authSvc.user$.pipe(
1516
take(1),
16-
map((isLogged: boolean) => !isLogged)
17+
map((user: UserResponse) => (!user ? true : false))
1718
);
1819
}
1920
}

src/app/shared/interceptors/admin-interceptor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ export class AdminInterceptor implements HttpInterceptor {
1111
constructor(private authSvc: AuthService) {}
1212
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<any> {
1313
if (req.url.includes('users')) {
14-
const authToken = this.authSvc.userTokenValue;
14+
const userValue = this.authSvc.userValue;
1515
const authReq = req.clone({
1616
setHeaders: {
17-
auth: authToken,
17+
auth: userValue.token,
1818
},
1919
});
2020
return next.handle(authReq);

0 commit comments

Comments
 (0)