Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. login(username: string, password: string) {
  2. let reqUrl = AppSettings.__USER_TOKEN_URL;
  3. let reqHeaders = this.authConfig.token.headers;
  4. let reqBody = encodeURI(
  5. this.authConfig.token.body
  6. .replace(/{{ username }}/g, username)
  7. .replace(/{{ password }}/g, password));
  8.  
  9. //
  10. // Get token, then get user identity, if login successfull.
  11. //
  12.  
  13. return this.http.post(reqUrl, reqBody, reqHeaders)
  14. .map((response) => this.getIdentity(response))
  15. .catch(this.handleErr);
  16. }
  17.  
  18. private getIdentity(response: Response) {
  19.  
  20. //
  21. // Get user identity based on token.
  22. //
  23.  
  24. let body = response.json();
  25. let token = body.access_token;
  26.  
  27. if (null != token && undefined != token) {
  28. this.authConfig
  29. .identity
  30. .headers
  31. .headers.set('authorization', 'Bearer ' + token);
  32.  
  33. let reqUrl = AppSettings.__USER_IDENTITY_URL
  34. let reqHeaders = this.authConfig.identity.headers;
  35. let reqbody = this.authConfig.identity.body;
  36.  
  37. return this.http.post(reqUrl, reqbody, reqHeaders)
  38. .map((response) => this.setUser(response))
  39. .catch(this.handleErr)
  40. .subscribe();
  41. }
  42. }
  43.  
  44. login() {
  45. this.loading = true;
  46. this.authenticationService.login(this.model.username, this.model.password).subscribe(
  47. data => { },
  48. error => { console.log('Error authenticating: ' + error); },
  49. () => { this.router.navigate([this.returnUrl]) });
  50. }
  51.  
  52. private getIdentity(response: Response) {
  53.  
  54. //
  55. // Get user identity based on token.
  56. //
  57.  
  58. let body = response.json();
  59. let token = body.access_token;
  60.  
  61. if (null != token && undefined != token) {
  62. this.authConfig
  63. .identity
  64. .headers
  65. .headers.set('authorization', 'Bearer ' + token);
  66.  
  67. let reqUrl = AppSettings.__USER_IDENTITY_URL
  68. let reqHeaders = this.authConfig.identity.headers;
  69. let reqbody = this.authConfig.identity.body;
  70.  
  71. return this.http.post(reqUrl, reqbody, reqHeaders)
  72. .map((response) => this.setUser(response))//return observable.
  73. }
  74. }
  75.  
  76. return this.http.post(reqUrl, reqBody, reqHeaders)
  77. .switchMap((response) => this.getIdentity(response))
  78. .catch(this.handleErr);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement