Skip to content

Commit 6aeedf2

Browse files
jsanthapanva
authored andcommitted
feat: option to set interactionResult without redirecting to resume right away
Instead of calling `provider.interactionFinished` there's an option to call `provider.interactionResult` with the same arguments, this will store the results and return the url you're expected to redirect to. Closes #350
1 parent 0b69703 commit 6aeedf2

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

docs/configuration.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,27 @@ router.post('/interaction/:grant', async (ctx, next) => {
320320
}
321321
```
322322

323+
**`#provider.interactionResult`**
324+
Unlike `#provider.interactionFinished` authorization request resume uri is returned instead of
325+
immediate http redirect. It should be used when custom response handling is needed e.g. making AJAX
326+
login where redirect information is expected to be available in the response.
327+
328+
```js
329+
// with express
330+
expressApp.post('/interaction/:grant/login', async (req, res) => {
331+
const redirectTo = await provider.interactionResult(req, res, results);
332+
333+
res.send({ redirectTo });
334+
});
335+
336+
// with koa
337+
router.post('/interaction/:grant', async (ctx, next) => {
338+
const redirectTo = await provider.interactionResult(ctx.req, ctx.res, results);
339+
340+
res.send({ redirectTo });
341+
});
342+
```
343+
323344
**`#provider.setProviderSession`**
324345
Sometimes interactions need to be interrupted before finishing and need to be picked up later,
325346
or a session just needs to be established from outside the regular authorization request.

lib/provider.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,21 +197,43 @@ class Provider extends events.EventEmitter {
197197
return [mountPath, routerUrl].join('');
198198
}
199199

200-
async interactionFinished(req, res, result) {
200+
/**
201+
* @name interactionResult
202+
* @api public
203+
*/
204+
async interactionResult(req, res, result) {
201205
const interaction = await getInteraction.call(this, req, res);
202206
interaction.result = result;
203207
await interaction.save(interaction.exp - epochTime());
204208

209+
return interaction.returnTo;
210+
}
211+
212+
/**
213+
* @name interactionFinished
214+
* @api public
215+
*/
216+
async interactionFinished(req, res, result) {
217+
const returnTo = await this.interactionResult(req, res, result);
218+
205219
res.statusCode = 302; // eslint-disable-line no-param-reassign
206-
res.setHeader('Location', interaction.returnTo);
220+
res.setHeader('Location', returnTo);
207221
res.setHeader('Content-Length', '0');
208222
res.end();
209223
}
210224

225+
/**
226+
* @name interactionDetails
227+
* @api public
228+
*/
211229
async interactionDetails(req) {
212230
return getInteraction.call(this, req);
213231
}
214232

233+
/**
234+
* @name setProviderSession
235+
* @api public
236+
*/
215237
async setProviderSession(req, res, {
216238
account,
217239
ts = epochTime(),

0 commit comments

Comments
 (0)