-
Notifications
You must be signed in to change notification settings - Fork 3
/
createJWT.js
54 lines (43 loc) · 1.08 KB
/
createJWT.js
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
48
49
50
51
52
53
54
const jwt = require("jsonwebtoken");
require("dotenv").config();
exports.createToken = function (id) {
return _createToken(id);
};
_createToken = function (id) {
try {
const expiration = new Date();
const user = { id: id };
const accessToken = jwt.sign(user, process.env.ACCESS_TOKEN_SECRET);
// In order to exoire with a value other than the default, use the
// following
/*
const accessToken= jwt.sign(user,process.env.ACCESS_TOKEN_SECRET,
{ expiresIn: '30m'} );
'24h'
'365d'
*/
var ret = { accessToken: accessToken };
} catch (e) {
var ret = { error: e.message };
}
return ret;
};
exports.isExpired = function (token) {
var isError = jwt.verify(
token,
process.env.ACCESS_TOKEN_SECRET,
(err, verifiedJwt) => {
if (err) {
return true;
} else {
return false;
}
}
);
return isError;
};
exports.refresh = function (token) {
var ud = jwt.decode(token, { complete: true });
var id = ud.payload.id;
return _createToken(id);
};