loopbackio / loopbackio/loopback-next
Allow middleware to redirect without the controller being involved.
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 5.1k
- Forks
- 1.1k
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 27
Description
## Suggestion
It would appear that today if some middleware logic wishes to send a redirect response, the controller must be aware of that and handle the redirection on behalf of the middleware. This seems a bit [incoherent](https://en.wikipedia.org/wiki/Cohesion_(computer_science)), as the controller is generally concerned with managing the resource defined by the REST endpoint, and not the needs of the middleware.
## Use Cases
Authentication. See [this example](https://loopback.io/doc/en/lb4/migration-auth-passport.html#configuring-authentication-endpoints-1). Why do I need special controller logic for `loginToThirdParty` there? If the middleware abstraction weren't leaking, I'd expect that I could just decorate any endpoint method of my controller with `@authenticate(STRATEGY_NAME)` and if the strategy requires redirection (e.g. OIDC), the user should be redirected to login/grant access, then redirected back to the endpoint in question. If I want to enable this behaviour today it would appear that I need this redirection logic for every single protected controller function that I write.
## Examples
One way to achieve this would be to follow the pattern that `AuthenticationActionProvider` uses [here](https://github.com/strongloop/loopback-next/blob/6a228264f3bb67af53fd74ebbf9439cab44fa286/packages/authentication/src/providers/auth-action.provider.ts#L86-L87), but instead of writing to setters that are bound to a binding specific to the Authentication namespace, write to some more generic binding that can be picked up and handled before the controller is ever called, subverting the need to call the controller entirely.
Another way might be to inspect the return value of the middleware. If it returns a `RedirectRoute`, redirect instead of calling the controller. I find this to be a cleaner option than injecting setters, but I can imagine this being more difficult to implement. As an aside, it'd be nice if controllers could do this as well, rather than requiring the `Response` object to be injected.
## Acceptance criteria
TBD - will be filled by the team.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing packages/authentication/src/providers/auth-action.provider.ts around the referenced lines, then trace how middleware results and controller invocation are handled. Compare the proposed binding-based and RedirectRoute approaches; the issue's acceptance criteria are still TBD, so the team must define the expected behavior before implementation can be considered done.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- api, backend-api-design
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100