Skip to content

Commit

Permalink
Added login routes guard
Browse files Browse the repository at this point in the history
  • Loading branch information
ksenanayaka committed Jul 12, 2019
1 parent 84e2024 commit 2f15c22
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
import { DashboardComponent } from './dashboard/dashboard.component';
import { CanActivateRoutesGuard } from './can-activate-routes.guard';
import { CanActivateLoginRoutesGuard } from './can-activate-login-routes.guard';

const routes: Routes = [
{
Expand All @@ -14,11 +15,13 @@ const routes: Routes = [
},
{
path: 'home',
component: HomeComponent
component: HomeComponent,
canActivate: [CanActivateLoginRoutesGuard]
},
{
path: 'login',
component: LoginComponent
component: LoginComponent,
canActivate: [CanActivateLoginRoutesGuard]
},
{
path: 'dashboard',
Expand All @@ -30,6 +33,6 @@ const routes: Routes = [
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
providers: [CanActivateRoutesGuard]
providers: [CanActivateRoutesGuard, CanActivateLoginRoutesGuard]
})
export class AppRoutingModule {}
16 changes: 16 additions & 0 deletions src/app/can-activate-login-routes.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { CanActivateLoginRoutesGuard } from './can-activate-login-routes.guard';
import { inject, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

describe('CanActivateLoginRoutesGuard', () => {
beforeEach(() => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [CanActivateLoginRoutesGuard]
});
});

it('should create an instance', inject([CanActivateLoginRoutesGuard], (guard: CanActivateLoginRoutesGuard) => {
expect(guard).toBeTruthy();
}));
});
17 changes: 17 additions & 0 deletions src/app/can-activate-login-routes.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { CanActivate, Router } from '@angular/router';
import { Injectable } from '@angular/core';
import { AuthService } from './auth.service';
import { Observable } from 'rxjs';

@Injectable()
export class CanActivateLoginRoutesGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}

public canActivate(): Observable<boolean> | Promise<boolean> | boolean {
if (this.authService.isSignedIn()) {
this.router.navigate(['/dashboard']);
return false;
}
return true;
}
}

0 comments on commit 2f15c22

Please sign in to comment.