浏览代码

24-332 Fixing something, removing auth service

ngrx
Nils Dittberner 8 年前
父节点
当前提交
ee038d91b5
共有 5 个文件被更改,包括 15 次插入66 次删除
  1. +5
    -3
      src/app/auth/auth-guard.service.ts
  2. +0
    -56
      src/app/auth/auth.service.ts
  3. +7
    -0
      src/app/auth/ngrx/auth.effects.ts
  4. +0
    -2
      src/app/core/core.module.ts
  5. +3
    -5
      src/app/core/header/header.component.ts

+ 5
- 3
src/app/auth/auth-guard.service.ts 查看文件

@@ -13,8 +13,10 @@ export class AuthGuard implements CanActivate {
constructor(private store: Store<fromApp.AppState>) {}

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;
});
}
}

+ 0
- 56
src/app/auth/auth.service.ts 查看文件

@@ -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<fromApp.AppState>) {}

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(['/']);
}
}

+ 7
- 0
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) {}
}

+ 0
- 2
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}
],


+ 3
- 5
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<fromAuth.State>;

constructor(private dataStorageService: DataStorageService,
private authService: AuthService,
private store: Store<fromApp.AppState>) {}
constructor(private dataStorageService: DataStorageService, private store: Store<fromApp.AppState>) {}

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());
}
}

正在加载...
取消
保存