krinlove commited on
Commit
097ab65
·
verified ·
1 Parent(s): 3f1196f

Create verify.js

Browse files
Files changed (1) hide show
  1. verify.js +37 -0
verify.js ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const manager = require('../lib/manager')
2
+ const verify = async (req, res, next) => {
3
+ const apiKey = req.headers['x-api-key']
4
+ if (!apiKey) {
5
+ return res.status(401).json({ message: 'Unauthorized' })
6
+ }
7
+
8
+ if (apiKey === process.env.AUTH_TOKEN) {
9
+ try {
10
+ req.account = await manager.getAccount()
11
+ if (!req.account) {
12
+ return res.status(503).json({
13
+ error: {
14
+ message: '服务暂时不可用,无法获取有效账户',
15
+ type: 'service_unavailable',
16
+ code: 'account_unavailable'
17
+ }
18
+ })
19
+ }
20
+ // console.log(`身份校验成功,使用账号=> ${JSON.stringify(req.account)}`)
21
+ next()
22
+ } catch (error) {
23
+ console.error('获取账户时出错:', error)
24
+ return res.status(503).json({
25
+ error: {
26
+ message: '服务暂时不可用',
27
+ type: 'service_unavailable',
28
+ code: 'internal_error'
29
+ }
30
+ })
31
+ }
32
+ } else {
33
+ return res.status(401).json({ message: 'Unauthorized' })
34
+ }
35
+ }
36
+
37
+ module.exports = verify