loopbackio / loopbackio/loopback-next
Allow middleware to redirect without the controller being involved.
Personne n'a encore pris cette issue.
- Langage dominant
- TypeScript
- Étoiles
- 5.1k
- Forks
- 1.1k
- Merge moyen
- 2 j 21 h
- PR mergées (30 j)
- 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.
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Commencez par examiner packages/authentication/src/providers/auth-action.provider.ts autour des lignes référencées, puis suivez la manière dont les résultats du middleware et l’appel du contrôleur sont gérés. Comparez les approches proposées basées sur le binding et RedirectRoute ; les critères d’acceptation de l’issue sont toujours TBD, l’équipe doit donc définir le comportement attendu avant que l’implémentation puisse être considérée comme terminée.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- api, backend-api-design
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- À clarifier
- Accessibilité débutants
- 25/100