Angular Security
ASecurityUse a functional `CanActivateFn` and return a `UrlTree` for unauthenticated users instead of imperatively navigating inside the guard: ```typescript import { inject } from '@angular/core'; import { CanActivateFn, Router } from '@angular/router'; import { AuthService } from './auth.service'; export const authGuard: CanActivateFn = (_route, state) => { const auth = inject(AuthService); const router = inject(Router); return auth.isAuthenticated() ? true : router.createUrlTree(['/login'], { query...
- 584 stars
- 0 votes
- 0 copies
- 4 views
- Added September 5, 2026
Works with
Security analysis
100/100Pro scans all 18 files and shows the line behind each finding
npx -y skills add HoangNguyen0403/agent-skills-standard --skill angular-security --agent claude-codeAre you the author of Angular Security?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-angular-security-e1bdcee5)# Protecting routes with Angular auth guards
Use a functional `CanActivateFn` and return a `UrlTree` for unauthenticated users instead of imperatively navigating inside the guard:
```typescript
import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
import { AuthService } from './auth.service';
export const authGuard: CanActivateFn = (_route, state) => {
const auth = inject(AuthService);
const router = inject(Router);
return auth.isAuthenticated()
? true
: router.createUrlTree(['/login'], {
queryParams: { returnUrl: state.url },
});
};
```
Apply it to every sensitive route, including lazy-loaded protected areas:
```typescript
{
path: 'account',
canActivate: [authGuard],
loadComponent: () => import('./account.component').then(m => m.AccountComponent),
}
```
If authentication is asynchronous, return an `Observable<boolean | UrlTree>` or `Promise<boolean | UrlTree>` and fail closed while the session check is unresolved or fails. Add separate role/permission guards where appropriate, but keep authorization checks on the server for every protected API and operation; a browser guard can be bypassed. Do not treat client-side route protection as the security boundary. Use a secure server session, preferably via an `HttpOnly` cookie, and do not put auth tokens in `localStorage` or `sessionStorage`.
Files in this skill
- eval-1.baseline.md
- eval-1.with-skill.md
- eval-2.baseline.md
- eval-2.with-skill.md
- eval-3.baseline.md
- eval-3.with-skill.md
- eval-4.baseline.md
- eval-4.with-skill.md
- eval-5.baseline.md
- eval-5.with-skill.md
- eval-6.baseline.md
- eval-6.with-skill.md
- trigger-1.md
- trigger-2.md
- trigger-3.md
- trigger-4.md
- trigger-5.md
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.