Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { AuthHttp, JwtHelper } from 'angular2-jwt';
  3. import {Http, RequestOptions, Headers, Response} from "@angular/http";
  4. import 'rxjs/add/operator/map';
  5. import {ConfigurationBase} from "../configuration/config.service";
  6. import {Observable} from "rxjs";
  7. import {IUser, IMessage, IRegisterUser, IErrorMessage} from "../models/models";
  8.  
  9.  
  10. @Injectable()
  11. export class AuthService {
  12.  
  13. private apiEndpoint;
  14. private headers;
  15. private options;
  16. private _tokenData: any;
  17.  
  18. constructor(private _config: ConfigurationBase,
  19. private _authHttp: AuthHttp,
  20. private _http: Http,
  21. private _jwtHelper: JwtHelper) {
  22. this.apiEndpoint = this._config.apiEndpoint;
  23. this.headers = new Headers({'Content-Type': 'application/json', 'X-Requested-With':'XMLHttpRequest' });
  24. this.options = new RequestOptions({ headers: this.headers });
  25. }
  26.  
  27. login(email: string, password: string) {
  28. let body = JSON.stringify({ email: email, password: password });
  29.  
  30.  
  31. return this._http.post(this.apiEndpoint + "/user/signin", body , this.options)
  32. .map(res => res.json())
  33. .subscribe(
  34. data => {
  35. if(data.token != ""){
  36. this.token = data.token;
  37. }
  38. },
  39. error => {
  40. switch (error.status) {
  41. case 401:
  42. console.log("Invalid Credentials");
  43. break;
  44. default:
  45. console.log("An error occured. Please contact support.");
  46. }
  47. },
  48. () => {
  49. }
  50. );
  51. }
  52.  
  53. private get token() {
  54. return sessionStorage.getItem(this._config.tokenName);
  55. }
  56.  
  57. private set token(token: string) {
  58. sessionStorage.removeItem(this._config.tokenName);
  59. this._tokenData = null;
  60. if (token) {
  61. sessionStorage.setItem(this._config.tokenName, token);
  62. }
  63. }
  64.  
  65. private get refresh_token(): string {
  66. let refresh_token: string;
  67. let token: string = this.token;
  68. if (token) {
  69. refresh_token = JSON.parse(token).refresh_token;
  70. }
  71. return refresh_token;
  72. }
  73.  
  74. private get tokenData(): any {
  75. if (!this.token) {
  76. this._tokenData = this.token;
  77. }
  78.  
  79. if (!this._tokenData && this.token) {
  80. this._tokenData = this._jwtHelper.decodeToken(this.token);
  81. }
  82. return this._tokenData;
  83. }
  84.  
  85. getUserDetails(): Observable<IUser> {
  86. var url = this.apiEndpoint + "/user/signedin";
  87. console.log("gfsagfas");
  88. return this._authHttp.request(url, {
  89. method: "get"
  90. }).map(
  91. response => {
  92. var data = response.text();
  93. var status = response.status.toString();
  94.  
  95. if (status === "200") {
  96. var result200: IUser = null;
  97. result200 = data === "" ? null : <IUser>JSON.parse(data);
  98. console.log(result200);
  99. return result200;
  100. }
  101. else {
  102. throw "error_no_callback_for_the_received_http_status";
  103. }
  104. }).catch((error) => this.catchError(error));
  105. }
  106.  
  107. registerUser(request: IRegisterUser): Observable<IMessage> {
  108. var url = this.apiEndpoint + "/user/register";
  109. var content = JSON.stringify(request);
  110. return this._http.post(url, content, this.options).map(
  111. response => {
  112. var data = response.text();
  113. var status = response.status.toString();
  114. if (status === "200") {
  115. var result200: IMessage = null;
  116. result200 = data === "" ? null : <IMessage>JSON.parse(data);
  117. return result200;
  118. }
  119. else {
  120. throw "error_no_callback_for_the_received_http_status";
  121. }
  122. }).catch((error) => this.catchError(error));
  123. }
  124.  
  125. updateUser(request: IRegisterUser): Observable<IMessage> {
  126. var url = this.apiEndpoint + "/user/update";
  127. var content = JSON.stringify(request);
  128. return this._authHttp.post(url, content, this.options).map(
  129. response => {
  130. var data = response.text();
  131. var status = response.status.toString();
  132. if (status === "200") {
  133. var result200: IMessage = null;
  134. result200 = data === "" ? null : <IMessage>JSON.parse(data);
  135. return result200;
  136. }
  137. else {
  138. throw "error_no_callback_for_the_received_http_status";
  139. }
  140. }).catch((error) => this.catchError(error));
  141. }
  142.  
  143. signout(): Observable<IMessage>{
  144. var url = this.apiEndpoint + "/user/signout";
  145. return this._authHttp.post(url, null,this.options).map(
  146. response => {
  147. console.log(response);
  148. var data = response.text();
  149. var status = response.status.toString();
  150. if (status === "200") {
  151. var result200: IMessage = null;
  152. result200 = data === "" ? null : <IMessage>JSON.parse(data);
  153. this.proceedSignOut();
  154. return result200;
  155. }
  156. else {
  157. throw "error_no_callback_for_the_received_http_status";
  158. }
  159. }).catch((error) => this.catchError(error));
  160. }
  161.  
  162. private proceedSignOut(){
  163. this.token = null;
  164. }
  165.  
  166. private catchError(error: Response) {
  167. if (error instanceof Response) {
  168. console.log(error);
  169. return Observable.throw(JSON.parse(error.text()));
  170. }
  171. else {
  172. throw "error_no_callback_for_the_received_http_status";
  173. }
  174. }
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement