Skip to content

Commit 329b8d3

Browse files
committed
fix: use auth guard for angular
1 parent 9ecaf03 commit 329b8d3

8 files changed

Lines changed: 63 additions & 12 deletions

File tree

frontend/package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"@angular/platform-browser": "^14.3.0",
1919
"@angular/platform-browser-dynamic": "^14.3.0",
2020
"@angular/router": "^14.3.0",
21+
"@auth0/angular-jwt": "^5.1.2",
2122
"ngx-cookie": "^6.0.1",
2223
"zone.js": "~0.11.4"
2324
},

frontend/src/app/app-routing.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import { NgModule } from '@angular/core';
22
import { RouterModule, Routes } from '@angular/router';
33
import {LoginComponent} from "./login/login.component";
44
import {HomeComponent} from "./home/home.component";
5+
import {AuthGuard} from "./guards/auth.guard";
56

67
const routes: Routes = [
78
{ path: "login", component: LoginComponent },
8-
{ path: "home", component: HomeComponent },
9-
{ path: "", redirectTo: "/home", pathMatch: "full"}
9+
{ path: "home", component: HomeComponent, canActivate: [AuthGuard] },
10+
{ path: "**", redirectTo: "/home", pathMatch: "full"}
1011
];
1112
@NgModule({
1213
imports: [RouterModule.forRoot(routes)],

frontend/src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { LayoutModule } from '@angular/cdk/layout';
1010
import { LoginComponent } from './login/login.component';
1111
import { HomeComponent } from './home/home.component';
1212
import {CookieModule} from "ngx-cookie";
13+
import {JWT_OPTIONS, JwtHelperService} from "@auth0/angular-jwt";
1314

1415
@NgModule({
1516
declarations: [
@@ -29,7 +30,8 @@ import {CookieModule} from "ngx-cookie";
2930
],
3031
exports: [
3132
],
32-
providers:[],
33+
providers:[{ provide: JWT_OPTIONS, useValue: JWT_OPTIONS },
34+
JwtHelperService],
3335
bootstrap: [AppComponent]
3436
})
3537
export class AppModule { }

frontend/src/app/auth.service.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ import {Account, Token} from "./stats.service";
33
import {HttpClient, HttpErrorResponse, HttpHeaders} from "@angular/common/http";
44
import {environment} from "../environments/environment";
55
import {CookieService} from "ngx-cookie";
6+
import {JwtHelperService} from "@auth0/angular-jwt";
67

78
@Injectable({
89
providedIn: 'root'
910
})
1011
export class AuthService {
1112

1213
constructor(private http: HttpClient,
13-
private _cookieService: CookieService) { }
14+
private _cookieService: CookieService,
15+
private jwtHelper: JwtHelperService) { }
1416

1517
public async login(username: string, password: string): Promise<boolean> {
1618
return new Promise<boolean>(res => {
@@ -44,7 +46,7 @@ export class AuthService {
4446
}
4547

4648
public isLoggedIn(): boolean {
47-
let str = this._cookieService.get("token");
48-
return str != null;
49+
let token = this._cookieService.get("token");
50+
return token != null && !this.jwtHelper.isTokenExpired(token);
4951
}
5052
}
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+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Injectable } from '@angular/core';
2+
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
3+
import { Observable } from 'rxjs';
4+
import {AuthService} from "../auth.service";
5+
6+
@Injectable({
7+
providedIn: 'root'
8+
})
9+
export class AuthGuard implements CanActivate {
10+
11+
constructor(public auth: AuthService, public router: Router) {}
12+
canActivate(
13+
route: ActivatedRouteSnapshot,
14+
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
15+
if (!this.auth.isLoggedIn()) {
16+
this.router.navigate(['login']);
17+
return false;
18+
}
19+
return true;
20+
}
21+
22+
}

frontend/src/app/home/home.component.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,10 @@ declare const genSkin: any;
1515
export class HomeComponent implements OnInit {
1616

1717
constructor(private _authService: AuthService,
18-
private _statsService: StatsService,
19-
private router: Router) { }
18+
private _statsService: StatsService) { }
2019

2120

2221
ngOnInit(): void {
23-
if(!this._authService.isLoggedIn()) {
24-
this.router.navigate(['login']);
25-
}
26-
2722
this._statsService.getSkinName().subscribe((skin: any) => {
2823
console.log(skin.skin);
2924

0 commit comments

Comments
 (0)