|
| 1 | +import {Injectable} from '@angular/core'; |
| 2 | +import {Http, Headers} from '@angular/http'; |
| 3 | +import { |
| 4 | + ActionTypes, |
| 5 | + AuthenticateAction, |
| 6 | + ProcessTokenAction, |
| 7 | + AuthErrorAction, |
| 8 | + AuthSuccessAction, |
| 9 | + LogoutAction, |
| 10 | + ClearAuthStateAction, |
| 11 | + LoadAndProcessTokenAction |
| 12 | +} from './auth.actions'; |
| 13 | +import {Effect, Actions} from '@ngrx/effects'; |
| 14 | +import {Observable} from 'rxjs/Rx'; |
| 15 | +import {go} from '@ngrx/router-store'; |
| 16 | +import {LocalStorage} from 'ng2-webstorage'; |
| 17 | +import {Action} from '@ngrx/store'; |
| 18 | + |
| 19 | + |
| 20 | +@Injectable() |
| 21 | +export class AuthEffects { |
| 22 | + |
| 23 | + @Effect() authenticate$: Observable<Action> = this.actions$ |
| 24 | + .ofType(ActionTypes.AUTHENTICATE) |
| 25 | + .map(action => action as AuthenticateAction) |
| 26 | + .switchMap((action: AuthenticateAction) => { |
| 27 | + if (!action.payload.username.trim()) { |
| 28 | + return Observable.of(new AuthErrorAction('Username cannot be blank')); |
| 29 | + } |
| 30 | + if (!action.payload.password.trim()) { |
| 31 | + return Observable.of(new AuthErrorAction('Password cannot be blank')); |
| 32 | + } |
| 33 | + const headers = new Headers(); |
| 34 | + headers.append('Content-Type', `application/x-www-form-urlencoded`); |
| 35 | + const payload = 'username=' + encodeURIComponent(action.payload.username) |
| 36 | + + '&password=' + encodeURIComponent(action.payload.password); |
| 37 | + return this.http |
| 38 | + .post('/api/authentication', payload, {headers: headers}) |
| 39 | + .map(res => new AuthSuccessAction(res.text())) |
| 40 | + .catch(() => Observable.of(new AuthErrorAction('Authentication failed'))); |
| 41 | + }); |
| 42 | + |
| 43 | + @Effect() authSuccess$: Observable<Action> = this.actions$ |
| 44 | + .ofType(ActionTypes.AUTH_SUCCESS) |
| 45 | + .map(action => action as AuthSuccessAction) |
| 46 | + .switchMap(action => { |
| 47 | + this.jwtToken = action.payload; |
| 48 | + return Observable.concat( |
| 49 | + Observable.of(go('/')), |
| 50 | + Observable.of(new ProcessTokenAction(action.payload)) |
| 51 | + ); |
| 52 | + }); |
| 53 | + |
| 54 | + @Effect() logout$: Observable<Action> = this.actions$ |
| 55 | + .ofType(ActionTypes.LOGOUT) |
| 56 | + .map(action => action as LogoutAction) |
| 57 | + .map(() => { |
| 58 | + this.jwtToken = null; |
| 59 | + return new ClearAuthStateAction(); |
| 60 | + }); |
| 61 | + |
| 62 | + @Effect() loadAndProcessToken$: Observable<Action> = this.actions$ |
| 63 | + .ofType(ActionTypes.LOAD_AND_PROCESS_TOKEN) |
| 64 | + .map(action => action as LoadAndProcessTokenAction) |
| 65 | + .switchMap(() => { |
| 66 | + if (!!this.jwtToken) { |
| 67 | + return Observable.of(new ProcessTokenAction(this.jwtToken)); |
| 68 | + } else { |
| 69 | + return Observable.empty(); |
| 70 | + } |
| 71 | + }); |
| 72 | + |
| 73 | + @LocalStorage('token') |
| 74 | + private jwtToken: string; |
| 75 | + |
| 76 | + constructor(private http: Http, private actions$: Actions) { |
| 77 | + } |
| 78 | + |
| 79 | +} |
0 commit comments