Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Injectable } from '@angular/core';
  2. import {Observable} from 'rxjs';
  3. import 'rxjs/add/operator/map';
  4. import {AuthState, LOGIN, LOGOUT, LOGIN_SUCCESS, LOGIN_FAILURE, AuthHeaders, currentUser} from '../reducers/auth';
  5. import {select, Store} from '@ngrx/store';
  6. import {ApiService} from '../services/api.service';
  7. import {LocalStorageService} from '../services/local-storage.service';
  8.  
  9. @Injectable()
  10. export class AuthService {
  11.  
  12.   // Auth Headers
  13.   private _authHeaders;
  14.   get authHeaders(): AuthHeaders {
  15.     return this._authHeaders;
  16.   }
  17.   set authHeaders(val: AuthHeaders) {
  18.     this._authHeaders = val;
  19.     // only save to local storage if headers exist
  20.     if (val) {
  21.       this.storage.setAuthHeaders(val);
  22.     }
  23.   }
  24.  
  25.   // Auth State Observable
  26.   private _auth$: Observable<AuthState>;
  27.   get auth$(): Observable<AuthState> { return this._auth$; }
  28.  
  29.   // Auth Store
  30.   get store(): Store<AuthState> { return this._store; }
  31.  
  32.   constructor(private _store: Store<AuthState>, private api: ApiService, private storage: LocalStorageService) {
  33.     this._auth$ = _store.pipe(select('auth'));
  34.     this.auth$.subscribe((state: AuthState) => {
  35.       this.authHeaders = state.authHeaders;
  36.     });
  37.   }
  38.  
  39.   login(data) {
  40.     this.store.dispatch({ type: LOGIN });
  41.     this.api.login(data)
  42.       .subscribe(
  43.         res => {
  44.           this.login_success(res);
  45.         },
  46.         err => {
  47.           this.login_failure(err.json().errors[0]);
  48.         }
  49.       );
  50.   }
  51.  
  52.   validateToken(): Observable<any> {
  53.     if (!this.authHeaders) {
  54.       // set directly with _ so we don't save into local storage the same value again
  55.       this._authHeaders = this.storage.getAuthHeaders();
  56.     }
  57.     return this.api.validateToken(this.authHeaders).map(
  58.       res => {
  59.         res.headers = this.authHeaders;
  60.         this.login_success(res);
  61.         return res;
  62.       }
  63.     );
  64.   }
  65.  
  66.   logout() {
  67.     this.api.logout(this.authHeaders)
  68.       .subscribe(
  69.         res => {
  70.           this.logout_success();
  71.         },
  72.         err => {
  73.           this.logout_success();
  74.         }
  75.       );
  76.   }
  77.  
  78.   getLoggedInUser() {
  79.     return this.store.pipe(select(currentUser));
  80.   }
  81.  
  82.   private logout_success() {
  83.     this.storage.deleteAuthHeaders();
  84.     this.store.dispatch({ type: LOGOUT });
  85.   }
  86.  
  87.   private login_success(res) {
  88.     this.store.dispatch({
  89.       type: LOGIN_SUCCESS,
  90.       payload: {
  91.         user: res.body.data,
  92.         authHeaders: res.headers
  93.       }
  94.     });
  95.   }
  96.  
  97.   private login_failure(error: string = 'Unknown error') {
  98.     this.store.dispatch({ type: LOGIN_FAILURE, payload: {error: error} });
  99.   }
  100.  
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement