Some auth route guard and link refactoring.
This commit is contained in:
parent
08d21631e2
commit
13a282f71f
@ -3,6 +3,7 @@ import { RecipePage } from './pages/recipe-page/recipe-page';
|
||||
import { RecipesPage } from './pages/recipes-page/recipes-page';
|
||||
import { RecipesSearchPage } from './pages/recipes-search-page/recipes-search-page';
|
||||
import { RecipeUploadPage } from './pages/recipe-upload-page/recipe-upload-page';
|
||||
import { authGuard } from './shared/guards/auth-guard';
|
||||
|
||||
export const routes: Routes = [
|
||||
{
|
||||
@ -16,6 +17,7 @@ export const routes: Routes = [
|
||||
{
|
||||
path: 'recipe-upload',
|
||||
component: RecipeUploadPage,
|
||||
canActivate: [authGuard]
|
||||
},
|
||||
{
|
||||
path: 'recipes/:username/:slug',
|
||||
|
||||
5
src/app/shared/components/nav/NavLinkConfig.ts
Normal file
5
src/app/shared/components/nav/NavLinkConfig.ts
Normal file
@ -0,0 +1,5 @@
|
||||
export interface NavLinkConfig {
|
||||
relativeUrl: string;
|
||||
title: string;
|
||||
disabled?: boolean;
|
||||
}
|
||||
@ -1,8 +1,10 @@
|
||||
<nav>
|
||||
<h2>Nav</h2>
|
||||
<ul>
|
||||
<li><a [routerLink]="'/'">Browse Recipes</a></li>
|
||||
<li><a [routerLink]="'/recipes-search'">Search Recipes</a></li>
|
||||
<li><a [routerLink]="'/recipe-upload'">Upload Recipe</a></li>
|
||||
@for (link of links(); track link.relativeUrl) {
|
||||
@if (!link.disabled) {
|
||||
<li><a [routerLink]="link.relativeUrl">{{ link.title }}</a></li>
|
||||
}
|
||||
}
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
import { Component } from '@angular/core';
|
||||
import { Component, computed, inject } from '@angular/core';
|
||||
import { RouterLink } from '@angular/router';
|
||||
import { AuthService } from '../../services/AuthService';
|
||||
import { NavLinkConfig } from './NavLinkConfig';
|
||||
|
||||
@Component({
|
||||
selector: 'app-nav',
|
||||
@ -7,4 +9,26 @@ import { RouterLink } from '@angular/router';
|
||||
templateUrl: './nav.html',
|
||||
styleUrl: './nav.css',
|
||||
})
|
||||
export class Nav {}
|
||||
export class Nav {
|
||||
|
||||
private readonly authService = inject(AuthService);
|
||||
|
||||
protected readonly links = computed<NavLinkConfig[]>(() => {
|
||||
return [
|
||||
{
|
||||
relativeUrl: '/',
|
||||
title: 'Browse Recipes',
|
||||
},
|
||||
{
|
||||
relativeUrl: '/recipes-search',
|
||||
title: 'Search Recipes',
|
||||
},
|
||||
{
|
||||
relativeUrl: '/recipe-upload',
|
||||
title: 'Upload Recipe',
|
||||
disabled: this.authService.accessToken() === null
|
||||
}
|
||||
];
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
17
src/app/shared/guards/auth-guard.spec.ts
Normal file
17
src/app/shared/guards/auth-guard.spec.ts
Normal file
@ -0,0 +1,17 @@
|
||||
import { TestBed } from '@angular/core/testing';
|
||||
import { CanActivateFn } from '@angular/router';
|
||||
|
||||
import { authGuard } from './auth-guard';
|
||||
|
||||
describe('authGuardGuard', () => {
|
||||
const executeGuard: CanActivateFn = (...guardParameters) =>
|
||||
TestBed.runInInjectionContext(() => authGuard(...guardParameters));
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({});
|
||||
});
|
||||
|
||||
it('should be created', () => {
|
||||
expect(executeGuard).toBeTruthy();
|
||||
});
|
||||
});
|
||||
8
src/app/shared/guards/auth-guard.ts
Normal file
8
src/app/shared/guards/auth-guard.ts
Normal file
@ -0,0 +1,8 @@
|
||||
import { CanActivateFn } from '@angular/router';
|
||||
import { inject } from '@angular/core';
|
||||
import { AuthService } from '../services/AuthService';
|
||||
|
||||
export const authGuard: CanActivateFn = (route, state) => {
|
||||
const authService = inject(AuthService);
|
||||
return authService.accessToken() !== null;
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user