parent
b7b5574dbc
commit
152d25a1e9
@ -1,12 +1,11 @@
|
||||
import { Routes } from '@angular/router';
|
||||
import { AuthGuard } from './auth.guard';
|
||||
import { HomePageComponent } from './components/home-page/home-page.component';
|
||||
import { LeafletMapComponent } from './components/leaflet-map/leaflet-map.component';
|
||||
import { LoginPageComponent } from './components/login-page/login-page.component';
|
||||
import { NotFoundComponent } from './components/not-found/not-found.component';
|
||||
|
||||
export const routes: Routes = [
|
||||
{ path: '', component: HomePageComponent },
|
||||
{ path: 'map', component: LeafletMapComponent },
|
||||
{ path: 'sign', component: LoginPageComponent },
|
||||
{ path: 'map', component: LeafletMapComponent, canActivate: [AuthGuard] },
|
||||
{ path: '**', component: NotFoundComponent },
|
||||
];
|
||||
|
@ -0,0 +1,17 @@
|
||||
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();
|
||||
});
|
||||
});
|
@ -0,0 +1,23 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { CanActivate, Router } from '@angular/router';
|
||||
import { LocalStorageService } from './services/localstorage.service';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root',
|
||||
})
|
||||
export class AuthGuard implements CanActivate {
|
||||
constructor(
|
||||
private localStorageService: LocalStorageService,
|
||||
private router: Router
|
||||
) {}
|
||||
|
||||
canActivate(): boolean {
|
||||
const token = this.localStorageService.getToken();
|
||||
if (token) {
|
||||
return true;
|
||||
} else {
|
||||
this.router.navigate(['?sign']);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue