+ .catch(err => logger.error('Cannot get access token.', { err }))
+}
+
+function authenticatePromiseIfNeeded (req: express.Request, res: express.Response, authenticateInQuery = false) {
+ return new Promise<void>(resolve => {
+ // Already authenticated? (or tried to)
+ if (res.locals.oauth?.token.User) return resolve()
+
+ if (res.locals.authenticated === false) return res.sendStatus(HttpStatusCode.UNAUTHORIZED_401)
+
+ authenticate(req, res, () => resolve(), authenticateInQuery)