import { HttpErrorResponse } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { Router } from '@angular/router' ; import { Actions , Effect , ofType } from '@ngrx/effects' ; import { DataPersistence } from '@nrwl/angular' ; import { map , tap } from 'rxjs/operators' ; import { LoginSuccessPayload } from '@pimp-my-pr/shared/domain' ; import { GetUserSuccessPayload } from '@pimp-my-pr/pmp-web/auth/domain' ; import { AuthDataService } from '../services/auth-data.service' ; import { fromAuthActions } from './auth.actions' ; import { AuthPartialState } from './auth.reducer' ; @ Injectable ( ) export class AuthEffects { @ Effect ( ) login$ = this . dp . fetch ( fromAuthActions . Types . Login , { run : ( action ) => this . authDataService . login ( action . payload ) . pipe ( tap ( ( res ) => this . authDataService . saveToken ( res . token ) ) , map ( ( res ) => new fromAuthActions . LoginSuccess ( res ) ) ) , onError : ( action , error ) => { return new fromAuthActions . LoginFail ( error ) ; } } ) ; @ Effect ( ) getUser$ = this . dp . fetch ( fromAuthActions . Types . GetUser , { run : ( ) => this . authDataService . getUser ( ) . pipe ( map ( ( res ) => new fromAuthActions . GetUserSuccess ( res ) ) ) , onError : ( action , error ) => { return new fromAuthActions . GetUserFail ( error ) ; } } ) ; @ Effect ( { dispatch : false } ) logout$ = this . actions$ . pipe ( ofType ( fromAuthActions . Types . Logout ) , tap ( ( ) => { this . authDataService . clearSavedToken ( ) ; } ) ) ; constructor ( private dp , private actions$ , private router , private authDataService ) { } }