What does CanActivate do?
CanActivate is a route guard that decides whether a route can be entered.
If it returns true, the route opens.
If false, navigation is blocked (Angular stays on the current page).
Example:
typescript
@Injectable({ providedIn: 'root' })
export class AuthGuard implements CanActivate {
canActivate(): boolean {
return isUserLoggedIn(); // or token-check logic
}
}Usage:
typescript
{ path: 'dashboard', component: DashboardComponent, canActivate: [AuthGuard] }What it can return:
true/false- a synchronous decisionObservable<boolean>orPromise<boolean>- if the check is asynchronous (for example, a server request)
Summary:
CanActivate is an entry filter.
It checks: "Does the user have the right to access this route?".
If not, it simply does not let them in.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.