Guest User

Untitled

a guest
Jul 5th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3. import { map } from 'rxjs/operators';
  4.  
  5. @Injectable()
  6. export class AuthenticationService {
  7. constructor(private http: HttpClient) { }
  8.  
  9. login(username: string, password: string) {
  10. return this.http.post<any>(`${config.apiUrl}/users/authenticate`, {
  11. username: username, password: password })
  12. .pipe(map(user => {
  13. // login successful if there's a jwt token in the response
  14. if (user && user.token) {
  15. // store user details and jwt token in local storage to keep user logged in between page refreshes
  16. localStorage.setItem('currentUser', JSON.stringify(user));
  17. }
  18.  
  19. return user;
  20. }));
  21. }
  22.  
  23. logout() {
  24. // remove user from local storage to log user out
  25. localStorage.removeItem('currentUser');
  26. }
  27. }
Add Comment
Please, Sign In to add comment