File tree Expand file tree Collapse file tree
timeless-api/src/main/webui/src/app Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import { SignInComponent } from './pages/sign/sign-in/sign-in.component';
66import { RegisteredComponent } from './pages/sign/registered/registered.component' ;
77import { UserConfigComponent } from './pages/user-config/user-config.component' ;
88import { RecordsComponent } from './components/records/records.component' ;
9+ import { AuthGuard } from './guards/auth_guard.guard' ;
910
1011export const routes : Routes = [
1112 {
@@ -29,6 +30,8 @@ export const routes: Routes = [
2930 {
3031 path : 'home' ,
3132 component : HomeComponent ,
33+ canActivate : [ AuthGuard ] ,
34+ canActivateChild : [ AuthGuard ] ,
3235 children : [
3336 {
3437 path : '' ,
Original file line number Diff line number Diff line change 1+ import { Injectable } from '@angular/core' ;
2+ import {
3+ CanActivate ,
4+ CanActivateChild ,
5+ Router ,
6+ ActivatedRouteSnapshot ,
7+ RouterStateSnapshot ,
8+ UrlTree ,
9+ } from '@angular/router' ;
10+ import { timelessLocalStorageKey } from '../constants' ;
11+ import { TimelessApiService } from '../timeless-api.service' ;
12+ import { Observable , of } from 'rxjs' ;
13+ import { catchError , map } from 'rxjs/operators' ;
14+
15+ @Injectable ( { providedIn : 'root' } )
16+ export class AuthGuard implements CanActivate , CanActivateChild {
17+ constructor (
18+ private readonly router : Router ,
19+ private readonly apiService : TimelessApiService ,
20+ ) { }
21+
22+ canActivate (
23+ route : ActivatedRouteSnapshot ,
24+ state : RouterStateSnapshot ,
25+ ) : Observable < boolean | UrlTree > {
26+ return this . apiService . userInfo ( ) . pipe (
27+ map ( ( ) => true ) ,
28+ catchError ( ( ) => {
29+ return of ( this . router . createUrlTree ( [ '/' ] ) ) ;
30+ } ) ,
31+ ) ;
32+ }
33+
34+ canActivateChild (
35+ childRoute : ActivatedRouteSnapshot ,
36+ state : RouterStateSnapshot ,
37+ ) : Observable < boolean | UrlTree > {
38+ return this . canActivate ( childRoute , state ) ;
39+ }
40+ }
Original file line number Diff line number Diff line change @@ -51,7 +51,11 @@ export class TimelessApiService {
5151 throw new Error ( ) ;
5252 }
5353 const user = JSON . parse ( data ) ;
54- return this . httpClient . get ( `/api/users/${ user . id } ` ) ;
54+ return this . httpClient . get ( `/api/users/${ user . id } ` , {
55+ headers : {
56+ Authorization : `Bearer ${ user . token } ` ,
57+ } ,
58+ } ) ;
5559 }
5660
5761 deleteRecord ( id : number ) {
You can’t perform that action at this time.
0 commit comments