Advertisement
kklevi

api service

May 28th, 2023
1,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //api.service.ts
  2.  
  3. import { Injectable } from '@angular/core';
  4. import { Router } from '@angular/router';
  5.  
  6.  
  7. @Injectable({
  8.   providedIn: 'root'
  9. })
  10. export class ApiService {
  11.  
  12.   router: Router
  13.   constructor(router: Router) {
  14.     this.router = router
  15.   }
  16.  
  17.   public isLoggedIn():boolean {
  18.  
  19.     let token = localStorage.getItem('forum-token')
  20.     // TODO check expiration date etc.
  21.     let tokenExpireString = localStorage.getItem('forum-token-expiration')
  22.     return token !== null
  23.   }
  24.  
  25.   public canActivate():boolean{
  26.     if(!this.isLoggedIn()){
  27.       this.router.navigate(['/login'])
  28.       return false;
  29.     }
  30.     return true;
  31.   }
  32.  
  33.   //private tokenExpired(token: string) {
  34.     //const expiry = (JSON.parse(atob(token.split('.')[1]))).exp;
  35.     //const expiry = token;
  36.     //return (Math.floor((new Date).getTime() / 1000)) >= expiry;
  37.   //}
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement