Guest User

Untitled

a guest
May 23rd, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { RequestOptions} from '@angular/http';
  3. import {User} from '../model/model.user';
  4. import 'rxjs/add/operator/map';
  5. import {HttpClient, HttpHeaders} from '@angular/common/http';
  6.  
  7. @Injectable()
  8. export class AuthService {
  9. constructor(public http: HttpClient) { }
  10.  
  11. public logIn(user: User) {
  12.  
  13. const headers = new HttpHeaders();
  14. headers.append('Accept', 'application/json')
  15. // creating base64 encoded String from user name and password
  16. const base64Credential: string = btoa( user.username + ':' + user.password);
  17. headers.append('Authorization', 'Basic ' + base64Credential);
  18. // this is where i'm having a problem :
  19. const httpOptions = new RequestOptions();
  20. httpOptions.headers = headers;
  21.  
  22. return this.http.get('http://localhost:8081/' + '/account/login' ,
  23. httpOptions)
  24. .map(resp => {
  25. // login successful if there's a jwt token in the response
  26. const user = resp.json().principal; // the returned user object is a principal object
  27. if (user) {
  28. // store user details in local storage to keep user logged in between page refreshes
  29. localStorage.setItem('currentUser', JSON.stringify(user));
  30. }
  31. });
  32. }
  33.  
  34.  
  35. logOut() {
  36. // remove user from local storage to log user out
  37. return this.http.post('http://localhost:8081/' + 'logout', {} )
  38. .map(resp => {
  39. localStorage.removeItem('currentUser');
  40. });
  41.  
  42. }
  43. }
Add Comment
Please, Sign In to add comment