Skip to content

Commit 28dcb81

Browse files
committed
refactor: 在统一的地方处理授权信息
1 parent f59b222 commit 28dcb81

File tree

7 files changed

+45
-1523
lines changed

7 files changed

+45
-1523
lines changed

apps/backend-mock/api/auth.cjs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@ module.exports = util => {
1717
return {
1818
api: {
1919
'/api/auth/codes'(req, res) {
20-
const token = req.get(`Authorization`)
21-
if (!token) {
22-
return res.status(401).json(useResponseError('UnauthorizedException', 'Unauthorized Exception'))
23-
}
24-
const username = Buffer.from(token, 'base64').toString('utf8');
25-
26-
const codes = MOCK_CODES.find((item) => item.username === username)?.codes ?? [];
20+
const codes = MOCK_CODES.find((item) => item.username === req.username)?.codes ?? [];
2721
res.json(useResponseSuccess(codes))
2822
},
2923
'post /api/auth/login'(req, res) {

apps/backend-mock/api/menu.cjs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,7 @@ module.exports = util => {
1717
return {
1818
api: {
1919
'/api/menu/all'(req, res){
20-
const token = req.get(`Authorization`)
21-
if (!token) {
22-
return res.status(401).json(useResponseError('UnauthorizedException', 'Unauthorized Exception'))
23-
}
24-
const username = Buffer.from(token, 'base64').toString('utf8');
25-
26-
const menus = MOCK_MENUS.find((item) => item.username === username)?.menus ?? [];
20+
const menus = MOCK_MENUS.find((item) => item.username === req.username)?.menus ?? [];
2721
res.json(useResponseSuccess(menus))
2822

2923
},

apps/backend-mock/api/user.cjs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@ module.exports = util => {
1515
return {
1616
api: {
1717
'/api/user/info'(req, res) {
18-
const token = req.get(`Authorization`)
19-
if (!token) {
20-
return res.status(401).json(useResponseError('UnauthorizedException', 'Unauthorized Exception'))
21-
}
22-
const username = Buffer.from(token, 'base64').toString('utf8');
23-
24-
const user = MOCK_USERS.find((item) => item.username === username);
18+
const user = MOCK_USERS.find((item) => item.username === req.username);
2519

2620
const { password: _pwd, ...userInfo } = user;
2721
res.json(useResponseSuccess(user))

apps/backend-mock/get-user.cjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
key: `get-user`,
3+
main() {
4+
return {
5+
useCreated(app) {
6+
app.use((req, res, next) => {
7+
try {
8+
const token = req.get(`Authorization`);
9+
const username = Buffer.from(token, 'base64').toString('utf8');
10+
req.username = username;
11+
} catch {
12+
// ...
13+
}
14+
next();
15+
});
16+
},
17+
};
18+
},
19+
};

apps/backend-mock/mm.config.cjs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {
33
useResponseSuccess,
44
useResponseError,
55
} = require(`./util.cjs`)
6+
const getUser = require(`./get-user.cjs`)
67

78
/**
89
* 配置说明请参考文档:
@@ -12,10 +13,32 @@ const {
1213
module.exports = util => {
1314
return {
1415
watch: [`./api/`],
16+
plugin: [getUser],
1517
proxy: {
1618
'/': `http://www.httpbin.org/`, // 后端接口主域
1719
},
1820
api: {
21+
'use /'(req, res, next){
22+
const noCheckList = [
23+
`/`,
24+
`/public`,
25+
`/favicon.ico`,
26+
`/api/test`,
27+
`/api/status`,
28+
`/api/auth/login`,
29+
]
30+
if( req.path === `/` || noCheckList.some(item => {
31+
return item.startsWith(req.path)
32+
})) {
33+
next()
34+
} else {
35+
const token = req.get(`Authorization`)
36+
if (!token) {
37+
return res.status(401).json(useResponseError('UnauthorizedException', 'Unauthorized Exception'))
38+
}
39+
next()
40+
}
41+
},
1942
// 跳转到接口列表
2043
'/api'(req, res){
2144
const url = `http://127.0.0.1:${globalThis.config.testPort}/#/apiStudio`

0 commit comments

Comments
 (0)