Conditions | 9 |
Paths | 1 |
Total Lines | 27 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | const Redis = require('./libraries/redis') |
||
8 | async function checkToken() { |
||
9 | let token = (typeof (ctx.request.headers.token) == 'undefined' || !ctx.request.headers.token) ? |
||
10 | ctx.cookies.get('token') : ctx.request.headers.token |
||
11 | let uid = (typeof (ctx.request.headers.uid) == 'undefined' || !ctx.request.headers.uid) ? |
||
12 | ctx.cookies.get('uid') : ctx.request.headers.uid |
||
13 | |||
14 | if (!token || !uid) { |
||
15 | console.log('token: ' + token) |
||
|
|||
16 | console.log('uid: ' + uid) |
||
17 | throw new ApiError('auth.error', 'token missing') |
||
18 | } |
||
19 | |||
20 | sessionKey = Constant.WECHAT_SESSION + token |
||
21 | session = await Redis.get(sessionKey) |
||
22 | session = JSON.parse(session) |
||
23 | if (!session) { |
||
24 | throw new ApiError('auth.error', 'token error') |
||
25 | } |
||
26 | |||
27 | if (session.uid == uid) { |
||
28 | ctx.uid = uid |
||
29 | return true |
||
30 | } else { |
||
31 | throw new ApiError('auth.error', 'no permission') |
||
32 | } |
||
33 | |||
34 | } |
||
35 | |||
53 |