Guest User

Untitled

a guest
Mar 19th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpHeaders} from '@angular/common/http';
  3. import { Observable } from 'rxjs/Observable';
  4. import { Cookie } from 'ng2-cookies';
  5. import 'rxjs/add/operator/map';
  6.  
  7. @Injectable()
  8. export class AuthenticationService {
  9. constructor(private http: HttpClient){}
  10.  
  11. login(username:string,password:string){
  12. return this.http.post<any>('/api/auth',{username:username,password:password},)
  13. .map(user => {
  14. //login successful if there a jwt token in the response
  15. if(user && user.token){
  16. localStorage.setItem('currentUser',JSON.stringify(user));
  17. }
  18. return user;
  19. })
  20. }
  21.  
  22. logout(){
  23. //remove user from local storage to log user out
  24. localStorage.removeItem('CurrentUser');
  25. }
  26. }
Add Comment
Please, Sign In to add comment