Advertisement
Guest User

Untitled

a guest
Mar 13th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpHeaders } from '@angular/common/http';
  3. import { Observable } from 'rxjs';
  4. import { Storage } from '@ionic/storage';
  5. import 'rxjs/add/operator/map'
  6.  
  7. const httpOptions = {
  8. headers: new HttpHeaders({
  9. 'Content-Type' : 'application/json',
  10. 'Authorization' : 'Basic ' + btoa('user'+':'+'pass')
  11. })
  12. };
  13.  
  14. @Injectable()
  15. export class AuthenticationService {
  16. public token: string;
  17.  
  18. constructor(private http: HttpClient, private _storage: Storage) {
  19. _storage.get('currentUser')
  20. .then(res => {
  21. let currentUser = res;
  22. this.token = currentUser && currentUser.token;
  23. })
  24. }
  25.  
  26. login(municipio, username: string, password: string): Observable<boolean> {
  27. return this.http.post('http://localhost:8080/datasnap/rest/TService/Login/',
  28. { ibge: municipio.ibge, username: username, password: password },
  29. httpOptions)
  30. .map(res => {
  31. let token = res && res['token'];
  32. if (token) {
  33. this.token = token;
  34. this._storage.set('currentUser', { username: username, municipio: municipio, token: token });
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. });
  40. }
  41.  
  42. logout(): void {
  43. this.token = null;
  44. this._storage.remove('currentUser');
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement