-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjwt.js
45 lines (41 loc) · 876 Bytes
/
jwt.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
const jwt = require('jsonwebtoken');
var issueToken = (data,secret,time)=>{
if(!data || !secret || !time){
throw Error("Incomplete Details.");
}
return new Promise((resolve,reject)=>{
jwt.sign(data,secret,{expiresIn:time.toString()},function(err,token){
if(err){
reject(err);
}
resolve(token);
});
});
}
var verifyToken = (token,secret)=>{
if(!token || !secret){
throw Error("Incomplete Details.");
}
return new Promise((resolve,reject)=>{
jwt.verify(token,secret,function(err,data){
if(err){
reject(err);
}
resolve({msg:"Token Is Valid.",decoded:data});
});
});
}
var decodeToken = (token)=>{
if(!token){
throw Error("Incomplete Details.");
}
var decoded = jwt.decode(token,{complete:true});
return new Promise((resolve,reject)=>{
resolve(decoded);
});
}
module.exports = {
issueToken,
verifyToken,
decodeToken,
}