fix: fixing guard by adding toastr
This commit is contained in:
parent
879151ea6c
commit
11698f0abb
|
|
@ -3,7 +3,7 @@ import { DashboardComponent } from './pages/dashboard/dashboard.component';
|
|||
import { LayoutsComponent } from './pages/dashboard/layouts/layouts.component';
|
||||
import { GraphComponent } from './pages/dashboard/page/graph/graph.component';
|
||||
import { AuthComponent } from './pages/auth/auth.component';
|
||||
import { AuthGuard } from './cores/guard/guards/auth.guard';
|
||||
import { AuthGuard } from './cores/guards/auth.guard';
|
||||
import { RegisterComponent } from './pages/register/register.component';
|
||||
|
||||
export const routes: Routes = [
|
||||
|
|
|
|||
|
|
@ -1,17 +0,0 @@
|
|||
import { TestBed } from '@angular/core/testing';
|
||||
import { CanActivateFn } from '@angular/router';
|
||||
|
||||
import { authGuard } from './auth.guard';
|
||||
|
||||
describe('authGuard', () => {
|
||||
const executeGuard: CanActivateFn = (...guardParameters) =>
|
||||
TestBed.runInInjectionContext(() => authGuard(...guardParameters));
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(executeGuard).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
|
@ -1,18 +1,20 @@
|
|||
import { Injectable } from '@angular/core';
|
||||
import { CanActivate, Router } from '@angular/router';
|
||||
import { ToastrService } from 'ngx-toastr';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class AuthGuard implements CanActivate {
|
||||
|
||||
constructor(private router: Router) {}
|
||||
constructor(private router: Router, private toast: ToastrService) {}
|
||||
|
||||
canActivate(): boolean {
|
||||
const token = localStorage.getItem('accessToken');
|
||||
if (token) {
|
||||
return true;
|
||||
} else {
|
||||
this.toast.error('You need to login first');
|
||||
this.router.navigate(['auth']);
|
||||
return false;
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user