-
Notifications
You must be signed in to change notification settings - Fork 4
/
createJWT.js
168 lines (136 loc) · 3.72 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const jwt = require("jsonwebtoken");
require("dotenv").config();
exports.createToken = function ( fn, ln, id )
{
return _createToken( fn, ln, id );
}
_createToken = function ( fn, ln, id )
{
try
{
const expiration = new Date();
const user = {userId:id,firstName:fn,lastName:ln};
// console.log("In createToken: id: "+ id)
// const accessToken = jwt.sign( user, process.env.ACCESS_TOKEN_SECRET);
//
const accessToken= jwt.sign(user,process.env.ACCESS_TOKEN_SECRET,
{ expiresIn: '60m'} );
//
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.id;
var firstName = ud.payload.firstName;
var lastName = ud.payload.lastName;
return _createToken( firstName, lastName, userId );
}
exports.createTokenPassword = function ( email, pass,code )
{
return _createTokenPassword( email, pass,code );
}
_createTokenPassword = function ( email, pass ,code)
{
try
{
const expiration = new Date();
const user = {email:email,password:pass, code:code};
// const accessToken = jwt.sign( user, process.env.ACCESS_TOKEN_SECRET);
//
const accessToken= jwt.sign(user,process.env.ACCESS_TOKEN_SECRET,
{ expiresIn: '60m'} );
//
var ret = {accessToken:accessToken};
}
catch(e)
{
var ret = {error:e.message};
}
return ret;
}
exports.createTokenVerify = function ( email,pass, code )
{
return _createTokenVerify( email,pass, code );
}
_createTokenVerify = function ( email ,pass, code)
{
try
{
console.log("IN the _createTokenVerify\n");
const expiration = new Date();
const user = {email:email, password:pass, code:code};
// const accessToken = jwt.sign( user, process.env.ACCESS_TOKEN_SECRET);
//
const accessToken= jwt.sign(user,process.env.ACCESS_TOKEN_SECRET,
{ expiresIn: '60m'} );
//
var ret = {accessToken:accessToken};
}
catch(e)
{
var ret = {error:e.message};
}
return ret;
}
exports.refreshPassword = function( token )
{
var ud = jwt.decode(token,{complete:true});
var email = ud.payload.email;
var pass = ud.payload.password;
var code = ud.payload.code;
return _createTokenPassword(email,pass, code);
}
exports.refreshEmail = function( token )
{
var ud = jwt.decode(token,{complete:true});
var email = ud.payload.email;
var code = ud.payload.code;
return _createTokenEmail(email,code);
}
exports.createRegisterToken = function ( comp, fn, ln, us,em,ph,pass,id)
{
return _createRegisterToken( comp, fn, ln, us,em,ph,pass,id);
}
_createRegisterToken = function ( comp, fn, ln, us,em,ph,pass,id )
{
try
{
const expiration = new Date();
const user = {userId:id,CompanyName: comp, FirstName:fn,LastName:ln, Email:em,Phone:ph, Login:us, Password:pass};
console.log("Created a token for the register info");
// const accessToken = jwt.sign( user, process.env.ACCESS_TOKEN_SECRET);
//
const accessToken= jwt.sign(user,process.env.ACCESS_TOKEN_SECRET,
{ expiresIn: '60m'} );
//
var ret = {accessToken:accessToken};
}
catch(e)
{
var ret = {error:e.message};
}
return ret;
}