0

l'm working on a angular application and l have two service which are after login service and before login service. when l use these two guards my application routes itself after every 10 minutes e.g. let's say l navigate from home page to account page, after 10 minutes of leaving the application idle, it refresh itself and routes back to home page without user input. l want to stop this behavior so that it won't navigate on it's own.

Here are my guards l have tried

after-login.service.ts

import { Injectable } from "@angular/core";
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { TokenService } from './token.service';

@Injectable()

export class AfterLoginService implements CanActivate {

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | Observable<boolean> | Promise<boolean> {
    console.log(this.Token.loggedIn());
    return this.Token.loggedIn();
  }
  constructor(private Token: TokenService) { }
}

Here is my before-login.service.ts

import { Injectable } from "@angular/core";
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { TokenService } from './token.service';

@Injectable()

export class BeforeLoginService implements CanActivate {
  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean |    Observable<boolean> | Promise<boolean> {
    return !this.Token.loggedIn();
  }
  constructor(private Token: TokenService) { }
}

Here is my auth.service.ts

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { TokenService } from './token.service';

@Injectable()

export class AuthService {
  private loggedIn = new BehaviorSubject<boolean>(this.Token.loggedIn());
  authStatus = this.loggedIn.asObservable();

  changeAuthStatus(value: boolean) {
    this.loggedIn.next(value)
  }
  constructor(private Token: TokenService) {

  }
}

token.service.ts

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class TokenService {

  private iss = {
    login: 'https://Backend/api/login.php',
    signup: 'https://Backend/api/signup.php',
  }

  constructor() { }

  handle(token) {
    this.set(token);
  }

  set(token) {
    localStorage.setItem('token', token);
  }

  get() {
    return localStorage.getItem('token');
  }

  remove() {
    localStorage.removeItem('token');
  }

  isValid() {
    const token = this.get();
    if (token && 'undefined' != token) {
      const payload = this.payload(token);
      if (payload) {
        //will need to remove >= and use >
        return Object.values(this.iss).indexOf(payload.iss) >= -1 ? true 
: false;
      }
    }
    return false;
  }

  payload(token) {
    // console.log(token);
    const payload = token.split('.')[1];
    return this.decode(payload);
  }

  decode(payload) {
    return JSON.parse(atob(payload));
  }

  loggedIn() {
    return this.isValid();
  }
}

1 Answers1

-1

you probably have some code written in token service to check if the user is logged in and clear token if inactive for 10 mins.