You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
front/src/app/auth.guard.ts

28 lines
762 B

import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { LocalStorageService } from './services/local-storage/local-storage.service';
import { ModalService } from './services/modal/modal.service';
@Injectable({
providedIn: 'root',
})
export class AuthGuard implements CanActivate {
constructor(
private localStorageService: LocalStorageService,
private router: Router,
private loginModalService: ModalService
) {}
canActivate(): boolean {
const token = this.localStorageService.getToken();
if (token) {
return true;
} else {
this.router.navigate(['/']).then(() => {
this.loginModalService.openModal('login-modal');
});
return false;
}
}
}