René's URL Explorer Experiment


Title: Configurable Guards & Resolvers · Issue #42171 · angular/angular · GitHub

Open Graph Title: Configurable Guards & Resolvers · Issue #42171 · angular/angular

X Title: Configurable Guards & Resolvers · Issue #42171 · angular/angular

Description: Feature Request Relevant Package This feature request is for @angular/router Description I want to use a guard or a resolver across the app in many places, but always with a slightly different configuration. E.g. a guard should redirect ...

Open Graph Description: Feature Request Relevant Package This feature request is for @angular/router Description I want to use a guard or a resolver across the app in many places, but always with a slightly different conf...

X Description: Feature Request Relevant Package This feature request is for @angular/router Description I want to use a guard or a resolver across the app in many places, but always with a slightly different conf...

Opengraph URL: https://github.com/angular/angular/issues/42171

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Configurable Guards & Resolvers","articleBody":"# Feature Request\r\n\r\n### Relevant Package\r\nThis feature request is for @angular/router\r\n\r\n\r\n### Description\r\nI want to use a guard or a resolver across the app in many places, but always with a slightly different configuration. E.g. a guard should redirect to different pages when some request fails or a resolver should load data from different endpoints.\r\n\r\nThe current solution is to either pass data in the route and let the guard/resolver access this data, or create a subclass of a guard / resolver for each specific configuration. However, there is no type-safety for route data and the data may be overwritten from a parent route when `paramsInheritanceStrategy` is active. Additionally, this can get really bloated when using several configured guards or resolvers:\r\n\r\n```ts\r\n// routes.ts\r\n{\r\n    path: 'page-a',\r\n    component: PageAComponent,\r\n    data: {\r\n        authGuard: {\r\n            failureRedirect: '/login'\r\n        }\r\n    },\r\n    canActivate: [ AuthGuard ],\r\n    resolve: {\r\n        foo: FooContentResolver,\r\n        bar: BarContentResolver\r\n    }\r\n},\r\n{\r\n    path: 'page-b',\r\n    component: PageBComponent,\r\n    data: {\r\n        authGuard: {\r\n            failureRedirect: '/signup'\r\n        }\r\n    },\r\n    canActivate: [ AuthGuard ]\r\n}\r\n```\r\n\r\n```ts\r\n// resolvers\r\n\r\n@Injectable()\r\nabstract class ContentResolver implements Resolve\u003cany> {\r\n    // ...\r\n}\r\n\r\n@Injectable()\r\nclass FooContentResolver extends ContentResolver {\r\n\r\n    readonly options: IContentResolverOptions = {\r\n        url: '/api/foo',\r\n        method: 'get'\r\n    };\r\n\r\n    // ...\r\n}\r\n\r\n@Injectable()\r\nclass BarContentResolver extends ContentResolver {\r\n\r\n    readonly options: IContentResolverOptions = {\r\n        url: '/api/bar',\r\n        method: 'get'\r\n    };\r\n\r\n    // ...\r\n}\r\n\r\n```\r\n### Describe the solution you'd like\r\n\r\nTo solve this issue, I'm imagining something similar to the `forRoot`/ `ModuleWithProviders` approach, that would let you pass options to some injectable (like here, a guard/resolver). So the `canActivate`, `canActivateChild` and `canLoad` attributes of a `Route` could also accept some kind of `InjectableWithProviders` object next to the `InjectionToken` / `string`.\r\n\r\n```ts\r\n// routes.ts\r\n{\r\n    path: 'page-a',\r\n    component: PageAComponent,\r\n    canActivate: [ AuthGuard.withOptions({ failureRedirect: '/login' }) ],\r\n    resolve: {\r\n        foo: ContentResolver.load({\r\n            url: '/api/foo',\r\n            method: 'get'\r\n        }),\r\n        bar: ContentResolver.load({\r\n            url: '/api/bar',\r\n            method: 'get'\r\n        })\r\n    }\r\n},\r\n{\r\n    path: 'page-b',\r\n    component: PageBComponent,\r\n    canActivate: [ AuthGuard.withOptions({ failureRedirect: '/signup' }) ]\r\n}\r\n```\r\n\r\n```ts\r\nconst AUTH_GUARD_OPTIONS = new InjectionToken\u003cIAuthGuardOptions>('auth-guard-options');\r\n\r\n@Injectable()\r\nclass AuthGuard implements CanActivate {\r\n\r\n    static withOptions(options: IAuthGuardOptions): InjectableWithProviders\u003cAuthGuard> {\r\n        return {\r\n            injectable: AuthGuard,\r\n            providers: [\r\n                { provide: AUTH_GUARD_OPTIONS, useValue: options }\r\n            ]\r\n        };\r\n    }\r\n\r\n    // ...\r\n\r\n}\r\n```\r\n\r\n```ts\r\nconst CONTENT_RESOLVER_OPTIONS = new InjectionToken\u003cIContentResolverOptions>('content-resolver-options');\r\n\r\n@Injectable()\r\nclass ContentResolver implements Resolve\u003cany> {\r\n\r\n    static load(options: IContentResolverOptions): InjectableWithProviders\u003cContentResolver> {\r\n        return {\r\n            injectable: ContentResolver,\r\n            providers: [\r\n                { provide: CONTENT_RESOLVER_OPTIONS, useValue: options }\r\n            ]\r\n        };\r\n    }\r\n\r\n    // ...\r\n\r\n}\r\n```\r\n\r\n### Describe alternatives you've considered\r\n\r\nAn alternative would be to use something similar to `MAT_DIALOG_DATA` used in `@angular/material`. We wouldn't need a fully flexible `InjectableWithProviders`, but every guard / resolver could inject a common token, e.g. `GUARD_OPTIONS`  / `RESOVLER_OPTIONS` that should be configurable in the route, e.g.:\r\n\r\n```ts\r\n// routes.ts\r\n{\r\n    path: 'page-a',\r\n    component: PageAComponent,\r\n    canActivate: [\r\n        { guard: AuthGuard, options: { failureRedirect: '/login' } }\r\n    ],\r\n    resolve: {\r\n        foo: {\r\n            resolver: ContentResolver,\r\n            options: {\r\n                url: '/api/foo',\r\n                method: 'get'\r\n            }\r\n        },\r\n        bar: {\r\n            resolver: ContentResolver,\r\n            options: {\r\n                url: '/api/bar',\r\n                method: 'get'\r\n            }\r\n        }\r\n    }\r\n},\r\n{\r\n    path: 'page-b',\r\n    component: PageBComponent,\r\n    canActivate: [\r\n        { guard: AuthGuard, options: { failureRedirect: '/signup' } }\r\n    ]\r\n}\r\n```\r\n\r\n\r\nI also tried to dynamically create a subclass in the static method and return it, but I assume this is not optimal for dependency discovery (and is currently failing with an error when using dependencies in the constructor of the guard: `Error: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.`):\r\n\r\n```ts\r\n@Injectable()\r\nclass AuthGuard implements CanActivate {\r\n\r\n    static withOptions(options: IAuthGuardOptions) {\r\n\r\n        @Injectable({ providedIn: 'root' })\r\n        class ConfiguredAuthGuard extends AuthGuard {\r\n            _options = options;\r\n        }\r\n\r\n        return ConfiguredAuthGuard\r\n    }\r\n\r\n    // ...\r\n\r\n}\r\n```\r\n\r\n### Related Issues\r\n#18247 seems related to this, but suggests some more substantial changes to routes & guards in general (which I would like to see as well!)","author":{"url":"https://github.com/homj","@type":"Person","name":"homj"},"datePublished":"2021-05-19T10:19:37Z","url":"https://github.com/angular/angular/issues/42171"}

react-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:024a5b07-ce57-594d-730b-0f2390ecf2f5
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idE716:2454CC:A8B7F8:D7C188:6B00F604
html-safe-noncefd4a9ee964e038587015210976947a39500f254592e7d3d6cb722579884d8b7c
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFNzE2OjI0NTRDQzpBOEI3Rjg6RDdDMTg4OjZCMDBGNjA0IiwidmlzaXRvcl9pZCI6IjY1MDM1MjE5NzA1NDAzMTIwNjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmace59f38dad6b0b69504b5ead94876a98d3d5fcced526650042eb66a8c026473d8
hovercard-subject-tagissue:895261815
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/42171/issue_layout
twitter:imagehttps://opengraph.githubassets.com/5024a7069b7ab217b20a2a1253ec11f67f977ae25cd2465e3d74e5af84327823/angular/angular/issues/42171
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/5024a7069b7ab217b20a2a1253ec11f67f977ae25cd2465e3d74e5af84327823/angular/angular/issues/42171
og:image:altFeature Request Relevant Package This feature request is for @angular/router Description I want to use a guard or a resolver across the app in many places, but always with a slightly different conf...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernamehomj
hostnamegithub.com
expected-hostnamegithub.com
None4ef498cbabcbe3c50befc7a5b743e7072ee770b4daf0e15e54801180d1fa91fc
turbo-cache-controlno-preview
go-importgithub.com/angular/angular git https://github.com/angular/angular.git
octolytics-dimension-user_id139426
octolytics-dimension-user_loginangular
octolytics-dimension-repository_id24195339
octolytics-dimension-repository_nwoangular/angular
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id24195339
octolytics-dimension-repository_network_root_nwoangular/angular
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release9983dd5da06943f833c971dd5b6bc988e2858be5
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/angular/angular/issues/42171#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fangular%2Fangular%2Fissues%2F42171
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fangular%2Fangular%2Fissues%2F42171
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=angular%2Fangular
Reloadhttps://github.com/angular/angular/issues/42171
Reloadhttps://github.com/angular/angular/issues/42171
Reloadhttps://github.com/angular/angular/issues/42171
angularhttps://github.com/angular
angularhttps://github.com/angular/angular
Codehttps://github.com/angular/angular
Issues835 (835)https://github.com/angular/angular/issues
Pull requests141 (141)https://github.com/angular/angular/pulls
Discussionshttps://github.com/angular/angular/discussions
Actionshttps://github.com/angular/angular/actions
Projectshttps://github.com/angular/angular/projects
Security and quality40 (40)https://github.com/angular/angular/security
Insightshttps://github.com/angular/angular/insights
https://github.com/angular/angular
New issuehttps://github.com/login?return_to=https://github.com/angular/angular/issues/42171
New issuehttps://github.com/login?return_to=https://github.com/angular/angular/issues/42171
Configurable Guards & Resolvershttps://github.com/angular/angular/issues/42171#top
https://github.com/atscott
https://github.com/alxhub
area: routerhttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22area%3A%20router%22
featureLabel used to distinguish feature request from other issueshttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22feature%22
feature: under considerationFeature request for which voting has completed and the request is now under considerationhttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22feature%3A%20under%20consideration%22
Backloghttps://github.com/angular/angular/milestone/82
https://github.com/homj
homjhttps://github.com/homj
on May 19, 2021https://github.com/angular/angular/issues/42171#issue-895261815
#18247https://github.com/angular/angular/issues/18247
gkalpakhttps://github.com/gkalpak
area: routerhttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22area%3A%20router%22
on May 19, 2021https://github.com/angular/angular/issues/42171#event-4767657230
ngbothttps://github.com/apps/ngbot
needsTriagehttps://github.com/angular/angular/milestone/83
on May 19, 2021https://github.com/angular/angular/issues/42171#event-4767657393
atscotthttps://github.com/atscott
featureLabel used to distinguish feature request from other issueshttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22feature%22
on May 19, 2021https://github.com/angular/angular/issues/42171#event-4769493166
ngbothttps://github.com/apps/ngbot
needsTriagehttps://github.com/angular/angular/milestone/83
Backloghttps://github.com/angular/angular/milestone/82
on May 19, 2021https://github.com/angular/angular/issues/42171#event-4769493262
https://github.com/apps/angular-robot
angular-robothttps://github.com/apps/angular-robot
on Jun 5, 2021https://github.com/angular/angular/issues/42171#issuecomment-855248556
documentationhttps://github.com/angular/angular/blob/e29eb8abfbfdfb793e08ac88e14b7ba6d2ff2136/docs/GITHUB_PROCESS.md#feature-request-process
angular-robothttps://github.com/apps/angular-robot
votes requiredhttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22votes%20required%22
on Jun 5, 2021https://github.com/angular/angular/issues/42171#event-4846769417
https://github.com/sg-gdarnell
sg-gdarnellhttps://github.com/sg-gdarnell
on Jun 16, 2021https://github.com/angular/angular/issues/42171#issuecomment-862665900
https://stackblitz.com/edit/configurable-resolver?file=src%2Fapp%2Fapp.module.tshttps://stackblitz.com/edit/configurable-resolver?file=src%2Fapp%2Fapp.module.ts
angular-robothttps://github.com/apps/angular-robot
feature: under considerationFeature request for which voting has completed and the request is now under considerationhttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22feature%3A%20under%20consideration%22
on Jun 26, 2021https://github.com/angular/angular/issues/42171#event-4942970413
https://github.com/SchnWalter
SchnWalterhttps://github.com/SchnWalter
on Jul 2, 2021https://github.com/angular/angular/issues/42171#issuecomment-873225616
angular/packages/router/src/operators/resolve_data.tshttps://github.com/angular/angular/blob/e99af184abef53feecff121675ee64a7cf7c51ff/packages/router/src/operators/resolve_data.ts#L66-L69
e99af18https://github.com/angular/angular/commit/e99af184abef53feecff121675ee64a7cf7c51ff
alxhubhttps://github.com/alxhub
alxhubhttps://github.com/alxhub
atscotthttps://github.com/atscott
on Aug 19, 2021https://github.com/angular/angular/issues/42171#event-5184020917
https://github.com/atscott
atscotthttps://github.com/atscott
on Sep 20, 2022https://github.com/angular/angular/issues/42171#issuecomment-1252683749
0abb67ahttps://github.com/angular/angular/commit/0abb67af59a92a2b29082a259aa9f4ea3fbaab7d
atscotthttps://github.com/atscott
completedhttps://github.com/angular/angular/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
on Sep 20, 2022https://github.com/angular/angular/issues/42171#event-7424351874
https://github.com/apps/angular-automatic-lock-bot
angular-automatic-lock-bothttps://github.com/apps/angular-automatic-lock-bot
on Oct 21, 2022https://github.com/angular/angular/issues/42171#issuecomment-1287168993
automatic conversation locking policyhttps://github.com/angular/angular/blob/8f24bc9443b3872fe095d9f7f77b308a361a13b4/docs/GITHUB_PROCESS.md#conversation-locking
angular-automatic-lock-bothttps://github.com/apps/angular-automatic-lock-bot
on Oct 21, 2022https://github.com/angular/angular/issues/42171#event-7642019408
Sign up for freehttps://github.com/signup?return_to=https://github.com/angular/angular/issues/42171
Sign in to commenthttps://github.com/login?return_to=https://github.com/angular/angular/issues/42171
alxhubhttps://github.com/alxhub
atscotthttps://github.com/atscott
area: routerhttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22area%3A%20router%22
featureLabel used to distinguish feature request from other issueshttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22feature%22
feature: under considerationFeature request for which voting has completed and the request is now under considerationhttps://github.com/angular/angular/issues?q=state%3Aopen%20label%3A%22feature%3A%20under%20consideration%22
BacklogNo due datehttps://github.com/angular/angular/milestone/82
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.