/**
* @module flitter-auth/controllers/KeyAction
*/
const Controller = require('libflitter/controller/Controller')
/**
* Provides handler methods for flitter-auth's key actions.
* Key actions allow your application to dynamically generate
* one-time links that call methods on controllers and (optionally)
* can even automatically sign in a user for the request, then log
* them out. e.g. a password reset link could use a key action.
* @extends module:libflitter/controller/Controller~Controller
*/
class KeyAction extends Controller {
/**
* Defines the services required by this unit.
* @returns {Array<string>}
*/
static get services() {
return [...super.services, 'canon']
}
/**
* Handles a key action request by calling the configured
* controller handler method. Closes the key action afterward
* based on the configured settings.
* @param {express/request} req - the request
* @param {express/response} res - the response
* @param {function} next - the next function in the stack
* @returns {Promise<void>}
*/
async handle(req, res, next) {
if ( !req.key_action ) throw new Error('Missing required key action.')
const handler = await this.canon.get(req.key_action.handler)
if ( !handler ) throw new Error('Unable to find handler for key action.')
await handler(req, res, next)
await req.key_action.close(req)
}
}
module.exports = exports = KeyAction