UNPKG

jangular-cli

Version:

A powerful CLI tool for rapidly bootstrapping Angular 17 & Spring Boot (Java 21) applications with integrated security, services, and enterprise-ready best practices.

25 lines (20 loc) 666 B
import { inject } from '@angular/core'; import { ActivatedRouteSnapshot, Router, RouterStateSnapshot, UrlTree } from '@angular/router'; import { AuthService } from './services/auth/auth.service'; export const authGuard = ( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ): boolean | UrlTree => { const authService = inject(AuthService); const router = inject(Router); // Allow unauthenticated access to register if (state.url.startsWith('/auth/register')) { return true; } if (authService.isAuthenticated()) { return true; } return router.createUrlTree(['/auth/login'], { queryParams: { returnUrl: state.url } }); };