Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. onSubmit(userData: any): void {
  2. this.user = new UserData(userData);
  3. this.services.getAccess(this.user).subscribe();
  4. }
  5.  
  6. getAccess(user: UserData): any {
  7. let headers = new Headers();
  8. let client_id = 'fge432358768979fgwefg34f34';
  9. let credentials = 'grant_type=password&client_id=' + client_id + '&client_secret=' + user.secretKey + '&username=' + user.email + '&password=' + user.password;
  10. headers.append('Content-Type', 'application/x-www-form-urlencoded');
  11.  
  12. return this.http.post(
  13. this.baseApiUrl + '/oauth/access_token',
  14. credentials,
  15. {
  16. headers: headers
  17. }
  18. )
  19. .map(this.handleSuccess)
  20. .catch(this.handleError);
  21. }
  22.  
  23. handleError(error: Response | any) {
  24. console.log('error');
  25. swal("Oops...", 'The informations you’ve entered doesn’t match any account. ', "error");
  26. return Observable.throw(error);
  27. }
  28.  
  29. handleSuccess(res: Response) {
  30. if (res.status === 200) {
  31. swal(
  32. {
  33. title: "Good job!",
  34. text: "You have successfully logged in.",
  35. timer: 2000,
  36. type: "success",
  37. showConfirmButton: false
  38. }
  39. );
  40. this.setCookie(res.json().access_token)
  41. }
  42. }
  43.  
  44. setCookie(accessToken: string): void {
  45. this.cookie.setForOneHour(accessToken);
  46. }
  47.  
  48. .map(data => this.handleSuccess(data))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement