Advertisement
Guest User

Untitled

a guest
Oct 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Http , Response } from '@angular/http';
  3. import { Headers } from '@angular/http';
  4. import { Observable } from 'rxjs/Rx';
  5. import {User} from '../models/user.model';
  6.  
  7. @Injectable()
  8. export class LoginService {
  9. private OauthLoginEndPointUrl = 'http://localhost:8080/api/oauth/token'; // Oauth Login EndPointUrl to web API
  10. private OauthCheckRole = 'http://localhost:8080/api/role';
  11. //private OauthTeacher = 'http://localhost:8080/api/teacher/user';
  12. //private OauthStudent = 'http://localhost:8080/api/student/user';
  13. private OauthUser = 'http://localhost:8080/api/user';
  14. private clientId = 'trusted-client';
  15. private clientSecret = 'secret';
  16. private basicHeader = btoa(this.clientId + ':' + this.clientSecret);
  17.  
  18. constructor(public http: Http) {}
  19.  
  20. login(username, password): Observable<any> {
  21.  
  22. const creds = 'grant_type=password'
  23. + '&username=' + username
  24. + '&password=' + password;
  25.  
  26. const headers = new Headers();
  27. headers.append('Authorization', 'Basic ' + this.basicHeader);
  28. headers.append('Content-Type', 'application/x-www-form-urlencoded');
  29.  
  30. return this.http.post(this.OauthLoginEndPointUrl , creds, {headers: headers}).map(this.handleData)
  31. .catch(this.handleError);
  32. }
  33.  
  34. getByUsername(username: string): Observable<User> {
  35. const headers = new Headers();
  36. headers.append('Authorization', 'Basic ' + this.basicHeader);
  37. headers.append('Content-Type', 'application/x-www-form-urlencoded');
  38. const creds = 'access_token=' + localStorage.getItem('token');
  39.  
  40.  
  41. const getUrl = `${this.OauthUser}?username=${username}&${creds}`;
  42. console.log(getUrl);
  43.  
  44. return this.http.get(getUrl, {headers: headers})
  45. .map(this.handleData)
  46. .catch(this.handleError);
  47. }
  48.  
  49. checkToken(accessToken): Observable<any> {
  50. const creds = 'access_token=' + accessToken;
  51.  
  52. const headers = new Headers();
  53. headers.append('Authorization', 'Basic ' + this.basicHeader);
  54. headers.append('Content-Type', 'application/x-www-form-urlencoded');
  55.  
  56. return this.http.get(this.OauthCheckRole + '?' + creds, {headers: headers}).map(this.handleData)
  57. .catch(this.handleError);
  58. }
  59.  
  60. private handleData(res: Response) {
  61. const body = res.json();
  62. return body;
  63. }
  64.  
  65. private handleError (error: any) {
  66. const errMsg = (error.message) ? error.message :
  67. error.status ? `${error.status} - ${error.statusText}` : 'Server error';
  68. console.error(errMsg); // log to console instead
  69. return Observable.throw(errMsg);
  70. }
  71.  
  72. public logout() {
  73. localStorage.removeItem('token');
  74. localStorage.removeItem('teacherId');
  75. localStorage.removeItem('studentId');
  76. localStorage.removeItem('username');
  77. localStorage.removeItem('role');
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement