Skip to content

Commit 98e3ebc

Browse files
committed
Using guards to block login from logged users
1 parent c0406d6 commit 98e3ebc

5 files changed

Lines changed: 61 additions & 10 deletions

File tree

src/app/app-routing.module.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import { NgModule } from '@angular/core';
22
import { RouterModule, Routes } from '@angular/router';
33
import { AuthenticatedGuard } from './authentication/guard/authenticated.guard';
4-
4+
import { LoggedOutGuard } from './authentication/guard/logged-out.guard';
5+
56
const loginModule = () => import('@app/login/login.module').then(m => m.LoginModule);
67
const dataModule = () => import('@app/data/data.module').then(m => m.DataModule);
78

89
const routes: Routes = [
910
{ path: '', redirectTo: '/data', pathMatch: 'full' },
10-
{ path: 'login', loadChildren: loginModule },
11+
{ path: 'login', loadChildren: loginModule, canActivate: [LoggedOutGuard] },
1112
{ path: 'data', loadChildren: dataModule, canActivate: [AuthenticatedGuard] }
1213
];
1314

src/app/authentication/guard/authenticated.guard.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Injectable } from '@angular/core';
2-
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
2+
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
3+
import { Observable } from 'rxjs';
34
import { AuthenticationService } from '../service/authentication.service';
45

56
@Injectable({
@@ -14,7 +15,9 @@ export class AuthenticatedGuard implements CanActivate {
1415
private authenticationService: AuthenticationService
1516
) { }
1617

17-
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
18+
canActivate(
19+
route: ActivatedRouteSnapshot,
20+
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
1821
const logged = this.authenticationService.getUser().logged;
1922
let active;
2023

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 { LoggedOutGuard } from './logged-out.guard';
4+
5+
describe('LoggedOutGuard', () => {
6+
let guard: LoggedOutGuard;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({});
10+
guard = TestBed.inject(LoggedOutGuard);
11+
});
12+
13+
it('should be created', () => {
14+
expect(guard).toBeTruthy();
15+
});
16+
});
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Injectable } from '@angular/core';
2+
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
3+
import { Observable } from 'rxjs';
4+
import { AuthenticationService } from '../service/authentication.service';
5+
6+
@Injectable({
7+
providedIn: 'root'
8+
})
9+
export class LoggedOutGuard implements CanActivate {
10+
11+
private homeUrl = '/';
12+
13+
constructor(
14+
private router: Router,
15+
private authenticationService: AuthenticationService
16+
) { }
17+
18+
canActivate(
19+
route: ActivatedRouteSnapshot,
20+
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
21+
const logged = this.authenticationService.getUser().logged;
22+
let active;
23+
24+
if (logged) {
25+
// Logged in
26+
// Redirect to home
27+
active = false;
28+
this.router.navigate([this.homeUrl]);
29+
} else {
30+
// Not logged in
31+
active = true;
32+
}
33+
34+
return active;
35+
}
36+
37+
}

src/app/login/view/login-view/login-view.component.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { HttpErrorResponse } from '@angular/common/http';
21
import { Component, OnInit } from '@angular/core';
32
import { ActivatedRoute, Router } from '@angular/router';
43
import { AuthenticationService } from '@app/authentication/service/authentication.service';
@@ -24,11 +23,6 @@ export class LoginViewComponent implements OnInit {
2423
) { }
2524

2625
ngOnInit() {
27-
// redirect to home if already logged in
28-
if (this.authenticationService.getUser().logged) {
29-
this.router.navigate(['/']);
30-
}
31-
3226
// get return url from route parameters or default to '/'
3327
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
3428
}

0 commit comments

Comments
 (0)