+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()
+ })
+ })
+}
+