import { CanActivateFn, Router } from '@angular/router';
import { LoginDataService } from '../services/login-data.service';
import { inject } from '@angular/core';

export const loginGuard: CanActivateFn = async (route, state) => {
  const loginDataService: LoginDataService = inject(LoginDataService);
  const router: Router = inject(Router);

  let hasInfo = loginDataService.isLogin();
  if(hasInfo){
    let token = localStorage.getItem('token');
    if(token != null){
      let verify = await loginDataService.verifyToken(token);
      if(verify){
        router.navigate(['sam/home']);
        return false;
      }
    }
  }
  
  return true;
};
