Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 KB | None | 0 0
  1. import {Injectable, Inject} from '@angular/core';
  2. // import {Http} from '@angular/http';
  3.  
  4.  
  5. import {Observable} from 'rxjs/Observable';
  6. import {BehaviorSubject} from 'rxjs/BehaviorSubject';
  7. import 'rxjs/add/observable/throw';
  8. import 'rxjs/add/observable/fromPromise';
  9. import 'rxjs/add/operator/map';
  10. import 'rxjs/add/operator/mergeMap';
  11. import 'rxjs/add/operator/toPromise';
  12. import 'rxjs/add/operator/catch';
  13.  
  14. import {API_ENDPOINTS, IApiEndpoints} from "../config/API_ENDPOINTS.config";
  15. import {APPLICATION_SETTINGS, IApplicationSettings} from "../config/APPLICATION_SETTINGS.config";
  16.  
  17. import{AuthHttpService} from '../authHttp/authHttp.service'
  18.  
  19. import {User} from './user.model';
  20.  
  21.  
  22. @Injectable()
  23. export class AuthenticationService {
  24.  
  25. private STORAGE_IDENTIFIER: string;
  26.  
  27. public TOKEN: any;
  28.  
  29. private _currentUser: BehaviorSubject<any>;
  30. public currentUser: Observable<any>;
  31.  
  32. constructor(@Inject(API_ENDPOINTS) private endpoints: IApiEndpoints,
  33. @Inject(APPLICATION_SETTINGS) private appSettings: IApplicationSettings,
  34. // private http: Http,
  35. // private storage: Storage,
  36. public authHttpService: AuthHttpService) {
  37.  
  38. this.STORAGE_IDENTIFIER = appSettings.storageIdentifier + 'AUTH';
  39.  
  40. this._currentUser = new BehaviorSubject(null);
  41. this.currentUser = this._currentUser.asObservable();
  42.  
  43. const token = localStorage.getItem(this.STORAGE_IDENTIFIER + '_TOKEN');
  44. if (token) {
  45. this.TOKEN = token;
  46.  
  47. let base64Url = this.TOKEN.split('.')[1];
  48. let base64 = base64Url.replace('-', '+').replace('_', '/');
  49. JSON.parse(window.atob(base64));
  50. console.log('base64', JSON.parse(window.atob(base64)))
  51. }
  52.  
  53. //
  54. // this.storage.get(this.STORAGE_IDENTIFIER).then(auth => {
  55. // if(auth) {
  56. // this.TOKEN = auth.token;
  57. // console.log(auth, 'wru token');
  58. // }
  59. // });
  60.  
  61. }
  62.  
  63. /**
  64. * Login
  65. *
  66. * @param email {string}
  67. * @param password {string}
  68. * @returns {Observable<R>}
  69. */
  70. login(email: string, password: string): Observable<any> {
  71.  
  72. let url = [
  73. this.endpoints.root.production,
  74. this.endpoints.usersProperties.login
  75. ].join('/');
  76.  
  77. let payload = {
  78. email: email,
  79. password: password,
  80. };
  81.  
  82. return this.authHttpService.post(url, payload)
  83. .map(response => {
  84. let body = response.json();
  85.  
  86. console.log(body);
  87.  
  88. this._setUser(body);
  89.  
  90. console.log('body', body);
  91. return body;
  92. // return Observable.fromPromise(this._setUser(body));
  93. }
  94. )
  95.  
  96. }
  97.  
  98. // /**
  99. // * Logout
  100. // */
  101. // logout(): void {
  102. // this._clearUser();
  103. // }
  104.  
  105. resetPassword(email: string) {
  106.  
  107. let url = [
  108. this.endpoints.root.production,
  109. this.endpoints.usersProperties.resetPassword
  110. ].join('/');
  111.  
  112. let payload = {email: email};
  113.  
  114. return this.authHttpService.post(url, payload)
  115. .map(response => {
  116.  
  117. let body = response.json();
  118.  
  119. return body;
  120.  
  121. })
  122.  
  123. }
  124.  
  125. registerUser(name, password, email, locale, country) {
  126. console.log('inside auth service');
  127. let url = [
  128. this.endpoints.root.production,
  129. this.endpoints.usersProperties.createUser
  130. ].join('/');
  131.  
  132. let payload = {email: email, password: password, name: name, locale: locale, country: country};
  133.  
  134. return this.authHttpService.post(url, payload)
  135. .map(response => {
  136.  
  137. let body = response.json();
  138.  
  139. return body;
  140.  
  141. })
  142. }
  143.  
  144. /**
  145. *
  146. * @returns {Observable<R>}
  147. */
  148. me(): Observable<any> {
  149.  
  150. if (!this.TOKEN) {
  151. return Observable.throw(new Error('MISSING_TOKEN'));
  152. }
  153.  
  154. let url = [
  155. this.endpoints.root.production,
  156. this.endpoints.usersProperties.me
  157. ].join('/');
  158.  
  159.  
  160. return this.authHttpService.get(url, this.TOKEN)
  161. .map(response => {
  162.  
  163. let body = response.json();
  164.  
  165. this._setUser(body);
  166.  
  167. return body;
  168.  
  169. })
  170. // .catch(this._handleError.bind(this));
  171.  
  172. }
  173.  
  174. //
  175. // updateMe(user: User) {
  176. //
  177. // if(!this.TOKEN) {return}
  178. //
  179. // let url = [
  180. // this.endpoints.root.production,
  181. // // this.endpoints.frontendUser.editUser
  182. // ].join('/');
  183. //
  184. // return this.authHttpService.post(url, user)
  185. // .map(response => {
  186. //
  187. // let body = response.json();
  188. //
  189. // this._setUser(body);
  190. //
  191. // return body;
  192. //
  193. // })
  194. // .catch(this._handleError.bind(this));
  195. //
  196. // }
  197.  
  198.  
  199. // updatePassword(user: User){
  200. // if(!this.TOKEN) {return}
  201. //
  202. // let url = [
  203. // this.endpoints.root.production,
  204. // // this.endpoints.frontendUser.editPassword
  205. // ].join('/');
  206. //
  207. //
  208. // return this.authHttpService.patch(url, user)
  209. // .map(response => {
  210. //
  211. // let body = response.json();
  212. //
  213. // this._setUser(body);
  214. //
  215. // return body;
  216. //
  217. // })
  218. // .catch(this._handleError.bind(this));
  219. //
  220. //
  221. //
  222. // }
  223.  
  224.  
  225. /**
  226. * Check if the user is authenticated based on their auth token being present in Storage
  227. * @returns {Promise<boolean>|PromiseLike<boolean>}
  228. */
  229. isAuthenticated() {
  230. const token = localStorage.getItem(this.STORAGE_IDENTIFIER + '_TOKEN');
  231. return !!token;
  232. }
  233.  
  234. /**
  235. * Helper method for updating user and authentication state
  236. * @param data
  237. * @returns {Observable<T>}
  238. * @private
  239. */
  240. private _setUser(data) {
  241. let user = Object.assign(new User(), data);
  242.  
  243. this.TOKEN = user.token;
  244.  
  245. this._currentUser.next(user);
  246.  
  247. localStorage.setItem(this.STORAGE_IDENTIFIER + '_TOKEN', this.TOKEN);
  248.  
  249. }
  250.  
  251. /**
  252. * Refresh auth token
  253. * @returns {any}
  254. */
  255. public refreshToken() {
  256.  
  257. if (!this.TOKEN) {
  258. return Observable.throw(new Error('MISSING_TOKEN'));
  259. }
  260.  
  261. let url = [
  262. this.endpoints.root.production,
  263. this.endpoints.usersProperties.refreshToken
  264. ].join('/');
  265.  
  266.  
  267. return this.authHttpService.patch(url,'', this.TOKEN)
  268. .map(response => {
  269.  
  270. let body = response.json();
  271.  
  272. console.log('body refresh token', body);
  273. // this._setUser(body);
  274.  
  275. return body;
  276.  
  277. })
  278. }
  279.  
  280.  
  281. //
  282. // /**
  283. // * Helper method for clearing user and authentication state
  284. // * @private
  285. // */
  286. // private _clearUser() {
  287. //
  288. // this.TOKEN = undefined;
  289. // // localStorage.removeItem(this.STORAGE_IDENTIFIER + '_TOKEN');
  290. // this._currentUser.next(null);
  291. //
  292. // }
  293. //
  294. // /**
  295. // * Helper method for handling error responses
  296. // * @param error
  297. // * @returns {ErrorObservable}
  298. // * @private
  299. // */
  300. // private _handleError(error) {
  301. //
  302. // let body = error.json();
  303. //
  304. // let response = body.errors || {};
  305. //
  306. // console.error(response);
  307. //
  308. // return Observable.throw(response);
  309. //
  310. // }
  311. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement