Skip to content

Commit 8f49746

Browse files
committed
🛂 add auth guard
1 parent f53cf37 commit 8f49746

File tree

5 files changed

+75
-4
lines changed

5 files changed

+75
-4
lines changed

src/app/app.routing.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ const routes: Routes = [
1313
{
1414
path: '**',
1515
redirectTo: '/',
16-
pathMatch: 'full',
17-
},
16+
}
1817
];
1918

2019
@NgModule({
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { AuthGuard } from './auth.guard';
4+
5+
describe('AuthGuard', () => {
6+
let guard: AuthGuard;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({});
10+
guard = TestBed.inject(AuthGuard);
11+
});
12+
13+
it('should be created', () => {
14+
expect(guard).toBeTruthy();
15+
});
16+
});

src/app/core/guards/auth.guard.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Injectable } from '@angular/core';
2+
import { CanActivate, Router, UrlTree } from '@angular/router';
3+
4+
import { Observable, tap } from 'rxjs';
5+
6+
import { AuthService } from '@services/auth.service';
7+
8+
@Injectable({
9+
providedIn: 'root'
10+
})
11+
export class AuthGuard implements CanActivate {
12+
constructor(
13+
private authService: AuthService,
14+
private router: Router,
15+
) {}
16+
canActivate()
17+
: Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
18+
return this.authService.validateToken().pipe(tap(isAuthenticated => {
19+
if (!isAuthenticated) {
20+
this.router.navigateByUrl('/login');
21+
}
22+
}));
23+
}
24+
}

src/app/core/services/auth.service.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,32 @@ import { Injectable } from '@angular/core';
33
import { Router } from '@angular/router';
44
import { environment } from 'environments/environment';
55

6-
import { Observable, map } from 'rxjs';
6+
import { Observable, catchError, map, of } from 'rxjs';
7+
8+
import { User } from '@models/user.model';
79

810
import { IUserCreated } from '@interfaces/response.interface';
911
import { ICreateAccount, ILogin } from '@interfaces/user.interface';
1012

13+
import Storage from '@utils/storage.util';
14+
1115
const base_url = environment.base_url;
1216

1317
@Injectable({
1418
providedIn: 'root'
1519
})
1620
export class AuthService {
1721

22+
userActive: User;
23+
24+
get headers() {
25+
return {
26+
headers: {
27+
'x-token': Storage.getLocalStorage('token') || ''
28+
}
29+
};
30+
}
31+
1832
constructor(
1933
private http: HttpClient,
2034
private router: Router
@@ -27,6 +41,21 @@ export class AuthService {
2741

2842
login(data: ILogin): Observable<IUserCreated> {
2943
const url = `${base_url}/auth/login`;
30-
return this.http.post<IUserCreated>(url, data);
44+
return this.http.post<IUserCreated>(url, data).pipe(map((resp) => {
45+
this.userActive = resp.user;
46+
Storage.savelocalStorage('token', resp.token);
47+
return resp;
48+
}));
49+
}
50+
51+
validateToken(): Observable<boolean> {
52+
const url = `${base_url}/auth/renew`;
53+
return this.http.get<IUserCreated>(url, this.headers)
54+
.pipe(map((resp) => {
55+
this.userActive = resp.user;
56+
Storage.removeLocalStorage('token');
57+
Storage.savelocalStorage('token', resp.token);
58+
return true;
59+
}), catchError(() => of(false)));
3160
}
3261
}

src/app/features/features.routing.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { NgModule } from "@angular/core";
22
import { RouterModule, Routes } from "@angular/router";
33

4+
import { AuthGuard } from "@guards/auth.guard";
5+
46
import { FeaturesComponent } from "./features.component";
57

68
const routes: Routes = [
79
{
810
path: '',
911
component: FeaturesComponent,
12+
canActivate: [AuthGuard],
1013
loadChildren: () => import('./home/home.routing')
1114
.then(m => m.HomeRoutingModule),
1215
},

0 commit comments

Comments
 (0)