import { Injectable } from '@angular/core' ; import { of , concat } from 'rxjs' ; import { Effect , Actions , ofType } from '@ngrx/effects' ; import { catchError , switchMap , tap } from 'rxjs/operators' ; import { Router , ActivatedRoute } from '@angular/router' ; import { Store , select } from '@ngrx/store' ; import * as Auth from './auth.actions' ; import { AuthService } from '../services/auth.service' ; import { IAuthState } from './auth.reducer' ; import { ICurrentUserState } from '@app/features/auth/store/currentUser.reducer' ; @ Injectable ( ) export class AuthEffects { constructor ( private _action$ , private _auth , private readonly _router , private readonly _route , private readonly _store ) { } @ Effect ( ) loginAction$ = this . _action$ . pipe ( ofType ( Auth . AuthActions . LOGIN ) , switchMap ( ( action ) => this . _auth . doAuth ( action . payload ) ) , switchMap ( ( result ) => ( result . user ) ? of ( new Auth . LoginSuccessAction ( result ) ) : of ( new Auth . ErrorAction ( result ) ) ) , catchError ( ( err ) => of ( new Auth . ErrorAction ( err ) ) ) ) ; @ Effect ( ) checkMainAction$ = this . _action$ . pipe ( ofType ( Auth . AuthActions . CHECK_AUTH ) , switchMap ( _ => this . _store . pipe ( select ( state => state . currentUser ) ) ) , switchMap ( currentUser => this . _auth . isAuth ( currentUser === null ) ) , switchMap ( ( res ) => ( res . token ) ? of ( new Auth . CheckAuthSuccessAction ( res ) ) : of ( new Auth . CheckAuthNoUserSuccessAction ( ) ) ) , catchError ( ( res ) => of ( new Auth . ErrorAction ( res ) ) ) ) ; @ Effect ( ) logoutAction$ = this . _action$ . pipe ( ofType ( Auth . AuthActions . LOGOUT ) , switchMap ( ( ) => this . _auth . doLogout ( ) ) , switchMap ( action => concat ( of ( new Auth . TokenDeleteAction ( ) ) , of ( new Auth . LogoutSuccessAction ( ) ) ) ) , catchError ( ( err ) => of ( new Auth . ErrorAction ) ) ) ; @ Effect ( ) createUserAction$ = this . _action$ . pipe ( ofType ( Auth . AuthActions . CREATE ) , switchMap ( ( action ) => this . _auth . doCreateUser ( action . payload ) ) , switchMap ( ( result ) => ( result . user ) ? of ( new Auth . CreateSuccessAction ( result ) ) : of ( new Auth . ErrorAction ( result ) ) ) , catchError ( ( err ) => of ( new Auth . ErrorAction ( err ) ) ) ) ; @ Effect ( ) userSuccessAction$ = this . _action$ . pipe ( ofType ( Auth . AuthActions . CREATE_SUCCESS , Auth . AuthActions . LOGIN_SUCCESS ) , switchMap ( ( action ) => { const { payload = null } = action ; return ( payload ) ? concat ( of ( new Auth . TokenSaveSuccessAction ( payload ) ) , of ( new Auth . CheckAuthAction ( ) ) ) : of ( new Auth . ErrorAction ( ) ) ; } ) , catchError ( ( err ) => of ( new Auth . ErrorAction ( err . message ) ) ) , tap ( _ => { const returnUrl = this . _route . snapshot . queryParams [ 'returnUrl' ] || '' ; this . _router . navigate ( [ `/${ ( returnUrl ) ? returnUrl : 'index' }` ] ) ; } ) ) ; }