Title: Render 404 page with CanActivate guard without aborting navigation · Issue #17004 · angular/angular · GitHub
Open Graph Title: Render 404 page with CanActivate guard without aborting navigation · Issue #17004 · angular/angular
X Title: Render 404 page with CanActivate guard without aborting navigation · Issue #17004 · angular/angular
Description: I'm submitting a ... (check one with "x") [ ] bug report => search github for a similar issue or PR before submitting [X] feature request [ ] support request => Please do not submit support request here, instead see https://github.com/an...
Open Graph Description: I'm submitting a ... (check one with "x") [ ] bug report => search github for a similar issue or PR before submitting [X] feature request [ ] support request => Please do not submit support request...
X Description: I'm submitting a ... (check one with "x") [ ] bug report => search github for a similar issue or PR before submitting [X] feature request [ ] support request => Please do not su...
Opengraph URL: https://github.com/angular/angular/issues/17004
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Render 404 page with CanActivate guard without aborting navigation","articleBody":"**I'm submitting a ...** (check one with \"x\")\n```\n[ ] bug report => search github for a similar issue or PR before submitting\n[X] feature request\n[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question\n```\n\n**Background**\nWhen a user navigates to a route that requires authentication and they are not authenticated, there's no way to use a `CanActivate` guard to render a 404 component and still update the url to what the user was trying to access. \n\n**Current behavior**\nLet's say a user navigates to `/protected` and is not authenticated. Currently, in the `CanActivate` guard, in order to render my 404 component, I would use `router.navigate(['404'], { skipLocationChange: true }); return false;`. However, after rendering the 404 component, the url in the address bar is the previous url before the navigation since the navigation was aborted.\n\n**Expected behavior**\nIn `CanActivate` guard, there should be a way to render the 404 route while still updating the url in the address bar to `/protected`.\n\n**Minimal reproduction of the problem with instructions**\n```ts\n@Injectable()\nexport class LoggedInGuard implements CanActivate {\n constructor(private router: Router, private userService: UserService) {}\n\n canActivate() {\n if (this.userService.isLoggedIn) return true;\n\n this.router.navigate(['/404'], { skipLocationChange: true });\n return false;\n }\n}\n```\nThis code will render a 404 page, but not update the url. I would like it to render 404 and update the url in the address bar to what the user was trying to access.\n\n**What is the motivation / use case for changing the behavior?**\nGithub does this, for example, if you go to https://github.com/simonxca/webpack-guide, it will render a 404 page even though the repo exists because you're not authenticated. The motivation is to prevent users from being able to distinguish between a protected from a non-existent page for security.\n\n**Please tell us about your environment:**\nMac OSX Sierra, Node 6.10.3\n\n* **Angular version:** 4.1.X\n\n* **Browser:** all\n \n* **Language:** all","author":{"url":"https://github.com/simon-xiong-ca","@type":"Person","name":"simon-xiong-ca"},"datePublished":"2017-05-24T22:52:48Z","url":"https://github.com/angular/angular/issues/17004"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:d8125658-dd67-8350-5d3c-0676e65366f8 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AA5E:3DD488:9B5407:C66F69:6B00F139 |
| html-safe-nonce | cdf7ebbbbd37ccd8c5f3b0ee9bdc72c86078655702ed1e2bbbc125e2ba7c3fc0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTVFOjNERDQ4ODo5QjU0MDc6QzY2RjY5OjZCMDBGMTM5IiwidmlzaXRvcl9pZCI6IjEyNzc4MDk0NDAxNTM3MjcyOTAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 2c00d8ad0a0089ab5c16850183f90e3994668499f8074cabb6199d6fd2a080c7 |
| hovercard-subject-tag | issue:231193854 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/17004/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b4257c29338fefaf45e909343a42acef98bff9970b38249e46cc909a651770af/angular/angular/issues/17004 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b4257c29338fefaf45e909343a42acef98bff9970b38249e46cc909a651770af/angular/angular/issues/17004 |
| og:image:alt | I'm submitting a ... (check one with "x") [ ] bug report => search github for a similar issue or PR before submitting [X] feature request [ ] support request => Please do not submit support request... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | simon-xiong-ca |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 9983dd5da06943f833c971dd5b6bc988e2858be5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width