})
}
+function authenticatePromiseIfNeeded (req: express.Request, res: express.Response) {
+ return new Promise(resolve => {
+ // Already authenticated? (or tried to)
+ if (res.locals.oauth && res.locals.oauth.token.User) return resolve()
+
+ if (res.locals.authenticated === false) return res.sendStatus(401)
+
+ authenticate(req, res, () => {
+ return resolve()
+ })
+ })
+}
+
function optionalAuthenticate (req: express.Request, res: express.Response, next: express.NextFunction) {
if (req.header('authorization')) return authenticate(req, res, next)
+ res.locals.authenticated = false
+
return next()
}
if (err) {
return res.status(err.status)
.json({
- error: 'Authentication failed.',
+ error: err.message,
code: err.name
})
.end()
export {
authenticate,
+ authenticatePromiseIfNeeded,
optionalAuthenticate,
token
}