-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthModule.js
More file actions
49 lines (32 loc) · 909 Bytes
/
AuthModule.js
File metadata and controls
49 lines (32 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Created by 13944 on 2017/6/18.
*/
import moment from 'moment';
import jwt from 'jwt-simple';
import {AppConfig,tokenExpiresHours} from './Config/AppConfig';
export const tokenList={};
export function getToken(userAccount) {
const iss=`${userAccount}#${Math.random()}`;
tokenList[userAccount]=iss;
return jwt.encode({
iss,
exp:AppConfig.tokenExpires
},AppConfig.secretKey);
}
export function verifyToken(token){
const result=jwt.decode(token,AppConfig.secretKey);
const userAccount=result.iss.split('#')[0];
console.info(tokenList,result.iss);
if(tokenList[userAccount]!==result.iss){
throw new Error("token is not true");
}
if(result.exp<=new Date().getTime()){
throw new Error("token is overdue");
}
return userAccount;
}
export function delToken(token) {
const userAccount=verifyToken(token);
delete tokenList[userAccount];
return true;
}