Guest User

Untitled

a guest
Jan 31st, 2018
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. login(username: string, password: string): Observable<boolean> {
  2. var headers = new Headers({
  3. "Content-Type": "application/json",
  4. "Accept": "application/json"
  5. });
  6.  
  7. let postData = {
  8. grant_type: "password",
  9. client_id: 2,
  10. client_secret: "RGNmOzt7WQ8SdNiCcJKKDoYrsFqI2tudopFjOJU3",
  11. username: "albanafmeti@gmail.com",
  12. password: "password",
  13. scope: ""
  14. }
  15.  
  16. return this.http.post('http://localhost:8000/oauth/token', JSON.stringify(postData), {
  17. headers: headers
  18. })
  19. .map((response: Response) => {
  20. // login successful if there's a jwt token in the response
  21. let token = response.json() && response.json().token;
  22. if (token) {
  23. // set token property
  24. this.token = token;
  25.  
  26. // store username and jwt token in local storage to keep user logged in between page refreshes
  27. localStorage.setItem('currentUser', JSON.stringify({ username: username, token: token }));
  28.  
  29. // return true to indicate successful login
  30. return true;
  31. } else {
  32. // return false to indicate failed login
  33. return false;
  34. }
  35. });
  36. }
Add Comment
Please, Sign In to add comment