Created
July 24, 2017 14:02
-
-
Save d4hines/45749dcd69a8aef14a8b034aef1ba8a8 to your computer and use it in GitHub Desktop.
Full Auth Effects for Issue with ngrx/effects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'rxjs/add/operator/catch'; | |
import 'rxjs/add/operator/do'; | |
import 'rxjs/add/operator/exhaustMap'; | |
import 'rxjs/add/operator/map'; | |
import 'rxjs/add/operator/take'; | |
import { Observable } from 'rxjs/Observable'; | |
import 'rxjs/add/observable/timer'; | |
import { Injectable } from '@angular/core'; | |
import { Router } from '@angular/router'; | |
import { Effect, Actions } from '@ngrx/effects'; | |
import { of } from 'rxjs/observable/of'; | |
import { AuthService } from '../services/auth.service'; | |
import * as fromAuth from '../actions/auth'; | |
@Injectable() | |
export class AuthEffects { | |
@Effect() | |
checkLogin$ = this.actions$ | |
.ofType(fromAuth.CHECK_LOGIN) | |
.exhaustMap(auth => | |
this.authService | |
.checkAuth() | |
.map(user => new fromAuth.LoginSuccess({ user: { name: user } })) | |
.catch(error => of(new fromAuth.LoginFailure(error))), | |
); | |
@Effect() | |
login$ = this.actions$ | |
.ofType(fromAuth.LOGIN) | |
.map((action: fromAuth.Login) => action.payload) | |
.exhaustMap(auth => | |
this.authService | |
.login(auth) | |
.map(user => new fromAuth.LoginSuccess({ user })) | |
.catch(error => of(new fromAuth.LoginFailure(error))), | |
); | |
@Effect() | |
logout$ = this.actions$ | |
.ofType(fromAuth.LOGOUT) | |
.exhaustMap(auth => | |
this.authService | |
.logout() | |
.map(user => new fromAuth.LoginRedirect()), | |
); | |
@Effect({ dispatch: false }) | |
loginSuccess$ = this.actions$ | |
.ofType(fromAuth.LOGIN_SUCCESS) | |
.do(() => this.router.navigate(['/'])); | |
@Effect({ dispatch: false }) | |
loginRedirect$ = this.actions$ | |
.ofType(fromAuth.LOGIN_REDIRECT, fromAuth.LOGOUT) | |
.do(authed => { | |
this.router.navigate(['/login']); | |
}); | |
@Effect() | |
loginTimeOut$ = this.actions$ | |
.ofType(fromAuth.LOGIN_SUCCESS); | |
// .flatMap(auth => Observable.timer(5000) | |
// .map(() => 'Aasdf'), | |
// ); | |
constructor( | |
private actions$: Actions, | |
private authService: AuthService, | |
private router: Router, | |
) {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment