From ee038d91b5ab3406f642a26a588b22f6dd3a0a94 Mon Sep 17 00:00:00 2001 From: Nils Dittberner Date: Mon, 4 Dec 2017 10:48:15 +0100 Subject: [PATCH] 24-332 Fixing something, removing auth service --- src/app/auth/auth-guard.service.ts | 8 +++-- src/app/auth/auth.service.ts | 56 --------------------------------- src/app/auth/ngrx/auth.effects.ts | 7 +++++ src/app/core/core.module.ts | 2 -- src/app/core/header/header.component.ts | 8 ++--- 5 files changed, 15 insertions(+), 66 deletions(-) delete mode 100644 src/app/auth/auth.service.ts diff --git a/src/app/auth/auth-guard.service.ts b/src/app/auth/auth-guard.service.ts index 29a9958..6674d29 100644 --- a/src/app/auth/auth-guard.service.ts +++ b/src/app/auth/auth-guard.service.ts @@ -13,8 +13,10 @@ export class AuthGuard implements CanActivate { constructor(private store: Store) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { - return this.store.select('auth').map((authState: fromAuth.State) => { - return authState.authenticated; - }); + return this.store.select('auth') + .take(1) + .map((authState: fromAuth.State) => { + return authState.authenticated; + }); } } \ No newline at end of file diff --git a/src/app/auth/auth.service.ts b/src/app/auth/auth.service.ts deleted file mode 100644 index b26fd04..0000000 --- a/src/app/auth/auth.service.ts +++ /dev/null @@ -1,56 +0,0 @@ -import * as firebase from 'firebase'; -import { Router } from '@angular/router'; -import { Injectable } from '@angular/core'; -import { Store } from '@ngrx/store'; - -import * as fromApp from '../ngrx/app.reducers'; -import * as AuthActions from './ngrx/auth.actions'; - -@Injectable() -export class AuthService { - constructor(private router: Router, private store: Store) {} - - signupUser(email: string, password: string) { - firebase.auth().createUserWithEmailAndPassword(email, password) - .then( - user => { - firebase.auth().currentUser.getIdToken() - .then( - (token: string) => { - this.store.dispatch(new AuthActions.SetToken(token)); - } - ); - this.store.dispatch(new AuthActions.Signup()); - this.router.navigate(['/']); - } - ) - .catch( - error => console.log(error) - ); - } - - signinUser(email: string, password: string) { - firebase.auth().signInWithEmailAndPassword(email, password) - .then( - response => { - firebase.auth().currentUser.getIdToken() - .then( - (token: string) => { - this.store.dispatch(new AuthActions.SetToken(token)); - } - ); - this.store.dispatch(new AuthActions.Signin()); - this.router.navigate(['/']); - } - ) - .catch( - error => console.log(error) - ); - } - - logout() { - firebase.auth().signOut(); - this.store.dispatch(new AuthActions.Logout()); - this.router.navigate(['/']); - } -} \ No newline at end of file diff --git a/src/app/auth/ngrx/auth.effects.ts b/src/app/auth/ngrx/auth.effects.ts index 6564775..7352e99 100644 --- a/src/app/auth/ngrx/auth.effects.ts +++ b/src/app/auth/ngrx/auth.effects.ts @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core'; import { Router } from '@angular/router'; import { Actions, Effect } from '@ngrx/effects'; import 'rxjs/add/operator/map'; +import 'rxjs/add/operator/do'; import 'rxjs/add/operator/switchMap'; import 'rxjs/add/operator/mergeMap'; import { fromPromise } from 'rxjs/observable/fromPromise'; @@ -50,6 +51,12 @@ export class AuthEffects { ] }); + @Effect({dispatch: false}) + authLogout = this.actions$.ofType(AuthActions.LOGOUT) + .do(() => { + this.router.navigate(['/']); + }); + // variable with dollar sign at the end marks an observable constructor(private router: Router, private actions$: Actions) {} } \ No newline at end of file diff --git a/src/app/core/core.module.ts b/src/app/core/core.module.ts index 08b7925..a13e14b 100644 --- a/src/app/core/core.module.ts +++ b/src/app/core/core.module.ts @@ -7,7 +7,6 @@ import { SharedModule } from "../shared/shared.module"; import { AppRoutingModule } from "../app-routing.module"; import { RecipeService } from "../recipes/recipe.service"; import { DataStorageService } from "../shared/data-storage.service"; -import { AuthService } from "../auth/auth.service"; import { AuthInterceptor } from "../shared/auth.interceptor"; import { LoggingInterceptor } from "../shared/logging.interceptor"; @@ -27,7 +26,6 @@ import { LoggingInterceptor } from "../shared/logging.interceptor"; providers: [ RecipeService, DataStorageService, - AuthService, {provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true}, {provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptor, multi: true} ], diff --git a/src/app/core/header/header.component.ts b/src/app/core/header/header.component.ts index 19d3240..0041198 100644 --- a/src/app/core/header/header.component.ts +++ b/src/app/core/header/header.component.ts @@ -3,9 +3,9 @@ import { Store } from "@ngrx/store"; import { Observable } from "rxjs/Observable"; import { DataStorageService } from "../../shared/data-storage.service"; -import { AuthService } from "../../auth/auth.service"; import * as fromApp from '../../ngrx/app.reducers'; import * as fromAuth from '../../auth/ngrx/auth.reducers'; +import * as AuthActions from '../../auth/ngrx/auth.actions'; @Component({ selector: 'app-header', @@ -14,9 +14,7 @@ import * as fromAuth from '../../auth/ngrx/auth.reducers'; export class HeaderComponent implements OnInit { authState: Observable; - constructor(private dataStorageService: DataStorageService, - private authService: AuthService, - private store: Store) {} + constructor(private dataStorageService: DataStorageService, private store: Store) {} ngOnInit() { this.authState = this.store.select('auth') @@ -35,6 +33,6 @@ export class HeaderComponent implements OnInit { } onLogout() { - this.authService.logout(); + this.store.dispatch(new AuthActions.Logout()); } } \ No newline at end of file