diff --git a/back/config/util.ts b/back/config/util.ts index e8cc5b46..6d747449 100644 --- a/back/config/util.ts +++ b/back/config/util.ts @@ -111,3 +111,11 @@ export function createRandomString(min: number, max: number): string { return newArr.join(''); } + +export function getToken(req: any) { + const { authorization } = req.headers; + if (authorization && authorization.split(' ')[0] === 'Bearer') { + return authorization.split(' ')[1]; + } + return ''; +} diff --git a/back/loaders/express.ts b/back/loaders/express.ts index d5cd316a..fc882bce 100644 --- a/back/loaders/express.ts +++ b/back/loaders/express.ts @@ -5,6 +5,7 @@ import routes from '../api'; import config from '../config'; import jwt from 'express-jwt'; import fs from 'fs'; +import { getToken } from '../config/util'; export default ({ app }: { app: Application }) => { app.enable('trust proxy'); @@ -22,10 +23,10 @@ export default ({ app }: { app: Application }) => { return next(); } const data = fs.readFileSync(config.authConfigFile, 'utf8'); - const authHeader = req.headers.authorization; + const headerToken = getToken(req); if (data) { const { token } = JSON.parse(data); - if (token && authHeader.includes(token)) { + if (token && headerToken === token) { return next(); } }