forked from andyjgelfo/CerealBoxd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateJWT.js
66 lines (56 loc) · 1.52 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
55
56
57
58
59
60
61
62
63
64
65
66
const jwt = require("jsonwebtoken");
require("dotenv").config();
exports.createToken = function ( fn, ln, username, email, confirm, id )
{
return _createToken( fn, ln, username, email, confirm, id );
}
_createToken = function ( fn, ln, username, email, confirm, id )
{
try
{
const expiration = new Date();
const user = {userId:username,firstName:fn,lastName:ln, email:email, confirmed:confirm, 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 userId = ud.payload.username;
var firstName = ud.payload.firstName;
var lastName = ud.payload.lastName;
var email = ud.payload.email;
var confirmed = ud.payload.confirmed;
var id = ud.payload.id;
return _createToken( firstName, lastName, userId, email, confirmed, id );
}