Route Guards
Protect pages.
Some pages are only for logged in users, like the basket. A route guard checks before the page opens.
- Only for logged in users
- Check first
- Route guards
canActivate
A guard is a function, that runs before the route opens. Return true, and the page opens. Return a URL tree, and the user is sent there instead. The route lists its guards, in can activate.
| key | value |
|---|---|
| CanActivateFn | a guard function |
| return true | open the page |
| return router.parseUrl('/login') | go elsewhere |
| canActivate: [guard] | on the route |
Blocked by the guard
The guard injects the auth service. If the user is logged in, it returns true. If not, it returns the login page's address. The basket route lists this guard. We open slash basket, without logging in. Run it. The guard sent us to slash login instead.
import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
import { Auth } from './auth';
export const loggedInGuard: CanActivateFn = () => {
const auth = inject(Auth);
return auth.loggedIn() || inject(Router).parseUrl('/login');
};Also uses auth.ts
import { Service, signal } from '@angular/core';
@Service()
export class Auth {
readonly loggedIn = signal(false);
}Also uses login.ts
import { Component, inject } from '@angular/core';
import { Router } from '@angular/router';
import { Auth } from './auth';
@Component({
selector: 'app-login',
template: `<h2>Please log in</h2><button (click)="logIn()">Log in</button>`,
})
export class Login {
auth = inject(Auth);
router = inject(Router);
logIn() {
this.auth.loggedIn.set(true);
this.router.navigate(['/basket']);
}
}Also uses basket-page.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-basket-page',
template: `<h2>Your basket</h2><p>Only logged in users can see this page.</p>`,
})
export class BasketPage {}Also uses app.routes.ts
import { Routes } from '@angular/router';
import { Login } from './login';
import { BasketPage } from './basket-page';
import { loggedInGuard } from './auth-guard';
export const routes: Routes = [
{ path: 'login', component: Login },
{ path: 'basket', component: BasketPage, canActivate: [loggedInGuard] },
];Also uses app.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet],
template: `<h1>Page Turners</h1><router-outlet />`,
})
export class App {}Also uses _url
/basketAlso uses _bar
1Logging in
Log in sets the signal to true, and goes to the basket. This time, the guard returns true. We click log in. Run it. The address is slash basket, and the page opened.
import { Component, inject } from '@angular/core';
import { Router } from '@angular/router';
import { Auth } from './auth';
@Component({
selector: 'app-login',
template: `<h2>Please log in</h2><button (click)="logIn()">Log in</button>`,
})
export class Login {
auth = inject(Auth);
router = inject(Router);
logIn() {
this.auth.loggedIn.set(true);
this.router.navigate(['/basket']);
}
}Also uses auth.ts
import { Service, signal } from '@angular/core';
@Service()
export class Auth {
readonly loggedIn = signal(false);
}Also uses basket-page.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-basket-page',
template: `<h2>Your basket</h2><p>Only logged in users can see this page.</p>`,
})
export class BasketPage {}Also uses app.routes.ts
import { Routes } from '@angular/router';
import { Login } from './login';
import { BasketPage } from './basket-page';
import { loggedInGuard } from './auth-guard';
export const routes: Routes = [
{ path: 'login', component: Login },
{ path: 'basket', component: BasketPage, canActivate: [loggedInGuard] },
];Also uses app.ts
import { Component } from '@angular/core';
import { RouterOutlet } from '@angular/router';
@Component({
selector: 'app-root',
imports: [RouterOutlet],
template: `<h1>Page Turners</h1><router-outlet />`,
})
export class App {}Also uses auth-guard.ts
import { inject } from '@angular/core';
import { CanActivateFn, Router } from '@angular/router';
import { Auth } from './auth';
export const loggedInGuard: CanActivateFn = () => {
const auth = inject(Auth);
return auth.loggedIn() || inject(Router).parseUrl('/login');
};Also uses actions.json
[{"click": "button"}]Also uses _url
/loginAlso uses _bar
1Summary
A guard runs before a route opens, and returns true, or a URL tree to go elsewhere. Next, some practice.
Key takeaways- CanActivateFn
- canActivate: [ ]
- parseUrl('/login')