From 3846601831d640e25942e72af470e868b2190aa2 Mon Sep 17 00:00:00 2001 From: Nils Dittberner Date: Mon, 27 Nov 2017 15:38:22 +0100 Subject: [PATCH] 20-258 Protecting new and edit by auth guard --- src/app/app-routing.module.ts | 5 +++-- src/app/app.module.ts | 8 +++++++- src/app/auth/auth-guard.service.ts | 14 ++++++++++++++ src/app/auth/auth.service.ts | 15 +++++++++++++++ src/app/header/header.component.html | 9 ++++++--- src/app/header/header.component.ts | 7 ++++++- 6 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 src/app/auth/auth-guard.service.ts diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 7cb9187..2d55f77 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -8,14 +8,15 @@ import { RecipeStartComponent } from './recipes/recipe-start/recipe-start.compon import { RecipeEditComponent } from './recipes/recipe-edit/recipe-edit.component'; import { SignupComponent } from './auth/signup/signup.component'; import { SigninComponent } from './auth/signin/signin.component'; +import { AuthGuard } from './auth/auth-guard.service'; const appRoutes: Routes = [ { path: '', redirectTo: '/recipes', pathMatch: 'full' }, { path: 'recipes' , component: RecipesComponent, children: [ { path: '', component: RecipeStartComponent }, - { path: 'new', component: RecipeEditComponent }, + { path: 'new', component: RecipeEditComponent, canActivate: [AuthGuard] }, { path: ':id', component: RecipeDetailComponent }, - { path: ':id/edit', component: RecipeEditComponent} + { path: ':id/edit', component: RecipeEditComponent, canActivate: [AuthGuard]} ] }, { path: 'shopping-list', component: ShoppingListComponent }, { path: 'signup', component: SignupComponent }, diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 0e7e0f1..c2a5565 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -23,6 +23,7 @@ import { DataStorageService } from './shared/data-storage.service'; import { SignupComponent } from './auth/signup/signup.component'; import { SigninComponent } from './auth/signin/signin.component'; import { AuthService } from './auth/auth.service'; +import { AuthGuard } from './auth/auth-guard.service'; @NgModule({ @@ -49,7 +50,12 @@ import { AuthService } from './auth/auth.service'; HttpModule, AppRoutingModule ], - providers: [ShoppingListService, RecipeService, DataStorageService, AuthService], + providers: [ + ShoppingListService, + RecipeService, + DataStorageService, + AuthService, + AuthGuard], bootstrap: [AppComponent] }) export class AppModule { } diff --git a/src/app/auth/auth-guard.service.ts b/src/app/auth/auth-guard.service.ts new file mode 100644 index 0000000..82a26ed --- /dev/null +++ b/src/app/auth/auth-guard.service.ts @@ -0,0 +1,14 @@ +import { CanActivate } from "@angular/router"; +import { ActivatedRouteSnapshot, RouterStateSnapshot } from "@angular/router/src/router_state"; +import { Injectable } from "@angular/core"; +import { AuthService } from "./auth.service"; + +@Injectable() +export class AuthGuard implements CanActivate { + + constructor(private authService: AuthService) {} + + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { + return this.authService.isAuthenticated(); + } +} \ No newline at end of file diff --git a/src/app/auth/auth.service.ts b/src/app/auth/auth.service.ts index bfea68f..40752d5 100644 --- a/src/app/auth/auth.service.ts +++ b/src/app/auth/auth.service.ts @@ -1,8 +1,13 @@ import * as firebase from 'firebase'; +import { Router } from '@angular/router'; +import { Injectable } from '@angular/core'; +@Injectable() export class AuthService { token: string; + constructor(private router: Router) {} + signupUser(email: string, password: string) { firebase.auth().createUserWithEmailAndPassword(email, password).catch( error => console.log(error) @@ -13,6 +18,7 @@ export class AuthService { firebase.auth().signInWithEmailAndPassword(email, password) .then( response => { + this.router.navigate(['/']); firebase.auth().currentUser.getIdToken() .then( (token: string) => this.token = token @@ -24,6 +30,11 @@ export class AuthService { ); } + logout() { + firebase.auth().signOut(); + this.token = null; + } + getToken() { firebase.auth().currentUser.getIdToken() .then( @@ -31,4 +42,8 @@ export class AuthService { ); return this.token; } + + isAuthenticated() { + return this.token != null; + } } \ No newline at end of file diff --git a/src/app/header/header.component.html b/src/app/header/header.component.html index 73583bd..6775324 100644 --- a/src/app/header/header.component.html +++ b/src/app/header/header.component.html @@ -10,9 +10,12 @@
  • Shopping List