import { Injectable } from '@angular/core' ; import { ActivatedRouteSnapshot , CanActivate , Router , RouterStateSnapshot } from '@angular/router' ; import { firstValueFrom } from 'rxjs' ; import { AuthService } from '../services/auth.service' ; import { AuthStrategy } from './auth-strategy.service' ; import { ElectronService } from 'ngx-electron' ; import { Store } from '../services/store.service' ; @ Injectable ( ) export class AuthGuard implements CanActivate { constructor ( private readonly router , private readonly authService , private readonly electronService , private readonly authStrategy , private readonly store ) { } async canActivate ( route , state ) { const token = route . queryParamMap . get ( 'token' ) ; const userId = route . queryParamMap . get ( 'userId' ) ; if ( token && userId ) { this . store . token = token ; this . store . userId = userId ; } const isAuthenticated = await this . authService . isAuthenticated ( ) ; if ( isAuthenticated ) { return true ; } if ( this . electronService . isElectronApp ) { try { this . electronService . ipcRenderer . send ( 'logout' ) ; } catch ( error ) { } } await firstValueFrom ( this . authStrategy . logout ( ) ) ; this . router . navigate ( [ '/auth/login' ] , { queryParams : { returnUrl : state . url } } ) ; return false ; } }