修复token验证

This commit is contained in:
hanhh 2021-06-30 12:20:42 +08:00
parent ef16622a7e
commit 0c3b3c206e
2 changed files with 11 additions and 2 deletions

View File

@ -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 '';
}

View File

@ -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();
}
}