Question presented to candidate:
"We need three rules in our app: only signed-in users can open /account, only admins should even be able to load the admin area, and users with unsaved edits must be warned before they leave the editor. What does Angular give you for this, and in what order do those checks run?"
What a strong answer should cover:
- A route guard is a function the router calls during navigation; it returns
true,false, aUrlTree/RedirectCommand(redirect), or a Promise / Observable of one of those. - The guard types and when to use each:
canMatch,canActivate,canActivateChild,canDeactivate, plus the deprecatedcanLoad; resolvers run after all guards. - The order:
canMatchduring matching, thencanDeactivateon the route being left, thencanActivateChild/canActivatefrom parent to child, then resolvers. - Returning
falsecancels the navigation (NavigationCancel,navigateByUrlresolvesfalse); returning aUrlTreecancels it and starts a new navigation to that URL. - Guards are UX, not security: the server must still check every request.
Clarifying questions expected:
- "Should users who are not admins see a 404, a 'not allowed' page, or the login page?"
- "Where does the session state live: a service with a signal, a token in storage, a server call?"
Code / implementation expected: Yes β a route config using each guard type, with a log of the order they run in.