mirror of
https://github.com/whyour/qinglong.git
synced 2026-06-30 20:35:09 +08:00
优化登录验证、退出逻辑
This commit is contained in:
@@ -4,6 +4,7 @@ import cors from 'cors';
|
||||
import routes from '../api';
|
||||
import config from '../config';
|
||||
import jwt from 'express-jwt';
|
||||
import fs from 'fs';
|
||||
|
||||
export default ({ app }: { app: Application }) => {
|
||||
app.enable('trust proxy');
|
||||
@@ -16,6 +17,22 @@ export default ({ app }: { app: Application }) => {
|
||||
path: ['/api/login'],
|
||||
}),
|
||||
);
|
||||
app.use((req, res, next) => {
|
||||
if (req.url && req.url.includes('/api/login')) {
|
||||
return next();
|
||||
}
|
||||
const data = fs.readFileSync(config.authConfigFile, 'utf8');
|
||||
const authHeader = req.headers.authorization;
|
||||
if (data) {
|
||||
const { token } = JSON.parse(data);
|
||||
if (token && authHeader.includes(token)) {
|
||||
return next();
|
||||
}
|
||||
}
|
||||
const err: any = new Error('UnauthorizedError');
|
||||
err['status'] = 401;
|
||||
next(err);
|
||||
});
|
||||
app.use(config.api.prefix, routes());
|
||||
|
||||
app.use((req, res, next) => {
|
||||
|
||||
Reference in New Issue
Block a user