|
|
|
@@ -1,4 +1,5 @@ |
|
|
|
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/switchMap'; |
|
|
|
@@ -23,12 +24,32 @@ export class AuthEffects { |
|
|
|
return fromPromise(firebase.auth().currentUser.getIdToken()); |
|
|
|
}) |
|
|
|
.mergeMap((token: string) => { |
|
|
|
this.router.navigate(['/']); |
|
|
|
return [ |
|
|
|
{ type: AuthActions.SIGNUP }, |
|
|
|
{ type: AuthActions.SET_TOKEN, payload: token } |
|
|
|
]; |
|
|
|
}); |
|
|
|
|
|
|
|
@Effect() |
|
|
|
authSignin = this.actions$.ofType(AuthActions.TRY_SIGNIN) |
|
|
|
.map((action: AuthActions.TrySignup) => { |
|
|
|
return action.payload; |
|
|
|
}) |
|
|
|
.switchMap((authData: {username: string, password: string}) => { |
|
|
|
return fromPromise(firebase.auth().signInWithEmailAndPassword(authData.username, authData.password)); |
|
|
|
}) |
|
|
|
.switchMap(() => { |
|
|
|
return fromPromise(firebase.auth().currentUser.getIdToken()); |
|
|
|
}) |
|
|
|
.mergeMap((token: string) => { |
|
|
|
this.router.navigate(['/']); |
|
|
|
return [ |
|
|
|
{ type: AuthActions.SIGNIN }, |
|
|
|
{ type: AuthActions.SET_TOKEN, payload: token } |
|
|
|
] |
|
|
|
}); |
|
|
|
|
|
|
|
// variable with dollar sign at the end marks an observable |
|
|
|
constructor(private actions$: Actions) {} |
|
|
|
constructor(private router: Router, private actions$: Actions) {} |
|
|
|
} |