Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { Router } from '@angular/router';
  3. import { Storage } from '@ionic/storage';
  4. import { ToastController, Platform } from '@ionic/angular';
  5. import { BehaviorSubject } from 'rxjs';
  6.  
  7.  
  8. @Injectable()
  9. export class AuthenticationService {
  10.  
  11. authState = new BehaviorSubject(false);
  12. public token : string;
  13.  
  14. constructor(
  15. private router: Router,
  16. private storage: Storage,
  17. private platform: Platform,
  18. public toastController: ToastController
  19. ){
  20. this.ifLoggedIn();
  21. }
  22.  
  23. async ifLoggedIn() {
  24. await this.storage.get('token').then((token) => {
  25. if(token){
  26. this.authState.next(true);
  27. this.token = token;
  28. }
  29. });
  30. }
  31.  
  32. login(){
  33. this.storage.set('token', 'asd123asdasd12354151asd').then((response) => {
  34. this.router.navigate(['app/inicio']);
  35. this.authState.next(true);
  36. });
  37. }
  38.  
  39. logout(){
  40. this.storage.remove('token').then(() => {
  41. this.router.navigate(['app/inicio']);
  42. this.authState.next(false);
  43. });
  44. }
  45.  
  46. isAuthenticated(){
  47. return this.authState.value;
  48. }
  49.  
  50. getToken(){
  51. return this.token;
  52. }
  53.  
  54.  
  55.  
  56. }
  57.  
  58. import { Component } from '@angular/core';
  59. import { Router } from '@angular/router';
  60. import { Events, MenuController, Platform } from '@ionic/angular';
  61. import { SplashScreen } from '@ionic-native/splash-screen/ngx';
  62. import { StatusBar } from '@ionic-native/status-bar/ngx';
  63. import { AuthenticationService } from './authentication.service';
  64.  
  65. @Component({
  66. selector: 'app-root',
  67. templateUrl: 'app.component.html',
  68. styleUrls: [
  69. './side-menu/styles/side-menu.scss',
  70. './side-menu/styles/side-menu.shell.scss',
  71. './side-menu/styles/side-menu.responsive.scss'
  72. ]
  73. })
  74. export class AppComponent {
  75.  
  76. isAuth : boolean;
  77.  
  78. constructor(
  79. public auth : AuthenticationService,
  80. private events: Events,
  81. private menu: MenuController,
  82. private platform: Platform,
  83. private splashScreen: SplashScreen,
  84. private statusBar: StatusBar,
  85. private router: Router,
  86. ) {
  87. this.initializeApp();
  88. }
  89.  
  90. initializeApp() {
  91. this.platform.ready().then(() => {
  92. this.statusBar.styleDefault();
  93. this.splashScreen.hide();
  94. this.isAuthenticated();
  95. });
  96. //this.isAuthenticated();
  97. }
  98.  
  99. isAuthenticated(){
  100. console.log(this.auth.getToken()); // devuelve un "undefined"
  101. console.log(this.auth.token); // devuelve un "undefined"
  102. console.log(this.auth); // y este es el más curioso devuelve el objeto auth y dentro del objeto se ve seteado el token y no puedo acceder a el de las formas anteriormente dichas.....
  103. }
  104.  
  105. logOut(){
  106. this.auth.logout();
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement