Advertisement
Guest User

Untitled

a guest
Sep 14th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. import {Injectable} from '@angular/core';
  2. import {Http} from '@angular/http';
  3. import {Api} from './api';
  4. import {Settings} from './settings';
  5. import 'rxjs/add/operator/map';
  6. import 'rxjs/add/operator/toPromise';
  7. import {Storage} from "@ionic/storage";
  8.  
  9.  
  10. @Injectable()
  11. export class Auth {
  12. isLoggedIn: Boolean;
  13. user_id: String;
  14. user_token: String;
  15.  
  16. constructor(public http: Http, public api: Api, public settings: Settings, public storage: Storage) {
  17. }
  18.  
  19. login(user) {
  20.  
  21.  
  22. this.logout();
  23.  
  24.  
  25. this.storage.set('user_id', user.id);
  26. this.storage.set('user_jwt', user.jwt);
  27.  
  28. this.isLoggedIn = true;
  29. this.user_id = user._id;
  30. this.user_token = user.jwt;
  31.  
  32.  
  33. }
  34.  
  35. logout() {
  36.  
  37. this.storage.remove('user_id');
  38. this.storage.remove('user_jwt');
  39.  
  40. this.isLoggedIn = false;
  41. this.user_id = null;
  42. this.user_token = null;
  43. }
  44.  
  45. isAuthenticated() {
  46. return this.isLoggedIn;
  47. }
  48.  
  49. getUser() {
  50.  
  51. console.log("UserID: " + this.user_id);
  52. console.log("UserToken: " + this.user_token);
  53.  
  54. if (this.isAuthenticated()) {
  55. return {
  56. id: this.user_id,
  57. token: this.user_token,
  58. };
  59. }
  60.  
  61. }
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement