|
|
|
@@ -0,0 +1,34 @@ |
|
|
|
import { Injectable } from '@angular/core'; |
|
|
|
import { Actions, Effect } from '@ngrx/effects'; |
|
|
|
import 'rxjs/add/operator/map'; |
|
|
|
import 'rxjs/add/operator/switchMap'; |
|
|
|
import 'rxjs/add/operator/mergeMap'; |
|
|
|
import { fromPromise } from 'rxjs/observable/fromPromise'; |
|
|
|
import * as firebase from 'firebase'; |
|
|
|
|
|
|
|
import * as AuthActions from './auth.actions'; |
|
|
|
import * as fromAuth from './auth.reducers'; |
|
|
|
|
|
|
|
@Injectable() |
|
|
|
export class AuthEffects { |
|
|
|
@Effect() |
|
|
|
authSignup = this.actions$.ofType(AuthActions.TRY_SIGNUP) |
|
|
|
.map((action: AuthActions.TrySignup) => { |
|
|
|
return action.payload; |
|
|
|
}) |
|
|
|
.switchMap((authData: {username: string, password: string}) => { |
|
|
|
return fromPromise(firebase.auth().createUserWithEmailAndPassword(authData.username, authData.password)); |
|
|
|
}) |
|
|
|
.switchMap(() => { |
|
|
|
return fromPromise(firebase.auth().currentUser.getIdToken()); |
|
|
|
}) |
|
|
|
.mergeMap((token: string) => { |
|
|
|
return [ |
|
|
|
{ type: AuthActions.SIGNUP }, |
|
|
|
{ type: AuthActions.SET_TOKEN, payload: token } |
|
|
|
] |
|
|
|
}); |
|
|
|
|
|
|
|
// variable with dollar sign at the end marks an observable |
|
|
|
constructor(private actions$: Actions) {} |
|
|
|
} |