Skip to content

Commit

Permalink
Implemented auth guard
Browse files Browse the repository at this point in the history
  • Loading branch information
pimmerks committed Jun 2, 2020
1 parent 437e737 commit a142651
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
5 changes: 4 additions & 1 deletion src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { AuthGuard } from '@helpers/guards/auth/auth.guard';


const routes: Routes = [
{ path: 'home', loadChildren: () => import('./pages/home/home.module').then(m => m.HomeModule) },
{ path: 'home', loadChildren: () => import('./pages/home/home.module').then(m => m.HomeModule),
canActivate: [AuthGuard], canActivateChild: [AuthGuard] },

{ path: 'auth', loadChildren: () => import('./pages/auth/auth.module').then(m => m.AuthModule) },
];

Expand Down
29 changes: 25 additions & 4 deletions src/app/helpers/guards/auth/auth.guard.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
import { Injectable } from '@angular/core';
import { CanActivate, CanActivateChild, CanLoad, Route, UrlSegment, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { CanActivate, CanActivateChild, CanLoad, Route, UrlSegment, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthenticationService } from '@services/authentication/authentication.service';

@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate, CanActivateChild, CanLoad {
constructor(
private readonly authService: AuthenticationService,
private readonly router: Router
) { }

public canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return true;
if (!this.authService.isAuthenticated) {
const redirect = this.router.parseUrl('/auth/login');
redirect.queryParams = { returnUrl: state.url };
return redirect;
}

return true;
}

public canActivateChild(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return true;
if (!this.authService.isAuthenticated) {
const redirect = this.router.parseUrl('/auth/login');
redirect.queryParams = { returnUrl: state.url };
return redirect;
}

return true;
}

public canLoad(
route: Route,
segments: UrlSegment[]): Observable<boolean> | Promise<boolean> | boolean {
return true;
return this.authService.isAuthenticated;
}

}
13 changes: 6 additions & 7 deletions src/app/pages/auth/components/login/login.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,12 @@ export class LoginComponent {
flatMap(_ => this.returnUrl$),
flatMap(returnUrl => from(this.router.navigateByUrl(returnUrl))))
.subscribe(token => {
this.loginBtnState = ClrLoadingState.SUCCESS;
},
(error: IError) => {
console.warn('error:', error);
this.error = error;
this.loginBtnState = ClrLoadingState.ERROR;
});
this.loginBtnState = ClrLoadingState.SUCCESS;
}, (error: IError) => {
console.warn('error:', error);
this.error = error;
this.loginBtnState = ClrLoadingState.ERROR;
});
}

}
15 changes: 7 additions & 8 deletions src/app/pages/auth/components/register/register.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ export class RegisterComponent {
flatMap(_ => this.returnUrl$),
flatMap(returnUrl => from(this.router.navigateByUrl(returnUrl))))
.subscribe(token => {
this.registerBtnState = ClrLoadingState.SUCCESS;
},
(error: IError) => {
console.warn('error:', error);
this.error = error;
this.registerBtnState = ClrLoadingState.ERROR;
});
this.registerBtnState = ClrLoadingState.SUCCESS;
},
(error: IError) => {
console.warn('error:', error);
this.error = error;
this.registerBtnState = ClrLoadingState.ERROR;
});
}

}
6 changes: 5 additions & 1 deletion src/app/services/authentication/authentication.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { JwtTokenService } from '@services/jwt-token/jwt-token.service';
import { BehaviorSubject, Observable, of } from 'rxjs';
import { BaseClientService } from '@services/base-client/base-client.service';
import { IToken } from '@models/token.model';
import { map, tap } from 'rxjs/operators';
import { map, tap, take } from 'rxjs/operators';

@Injectable({
providedIn: 'root'
Expand All @@ -23,6 +23,10 @@ export class AuthenticationService {
return this.authSubject.asObservable();
}

public get isAuthenticated() {
return this.authSubject.value;
}

public login(email: string, password: string): Observable<boolean> {
return this.http.post<IToken>('auth/login', { email, password })
.pipe(
Expand Down

0 comments on commit a142651

Please sign in to comment.