]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/middlewares/auth.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / middlewares / auth.ts
index f38373624c1cccf006c3be35de067cdf7c552516..c5424be979b1703b7ba479a63c9925fa424c7871 100644 (file)
@@ -1,7 +1,7 @@
-import * as express from 'express'
+import express from 'express'
 import { Socket } from 'socket.io'
 import { getAccessToken } from '@server/lib/auth/oauth-model'
-import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
+import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
 import { logger } from '../helpers/logger'
 import { handleOAuthAuthenticate } from '../lib/auth/oauth'
 
@@ -16,11 +16,11 @@ function authenticate (req: express.Request, res: express.Response, next: expres
     .catch(err => {
       logger.warn('Cannot authenticate.', { err })
 
-      return res.status(err.status)
-        .json({
-          error: 'Token is invalid.',
-          code: err.name
-        })
+      return res.fail({
+        status: err.status,
+        message: 'Token is invalid',
+        type: err.name
+      })
     })
 }
 
@@ -52,7 +52,12 @@ function authenticatePromiseIfNeeded (req: express.Request, res: express.Respons
     // Already authenticated? (or tried to)
     if (res.locals.oauth?.token.User) return resolve()
 
-    if (res.locals.authenticated === false) return res.sendStatus(HttpStatusCode.UNAUTHORIZED_401)
+    if (res.locals.authenticated === false) {
+      return res.fail({
+        status: HttpStatusCode.UNAUTHORIZED_401,
+        message: 'Not authenticated'
+      })
+    }
 
     authenticate(req, res, () => resolve(), authenticateInQuery)
   })