-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
42 lines (34 loc) · 810 Bytes
/
util.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
var jwt = require('jsonwebtoken');
var util = {};
util.successTrue = function(data,message){
return {
success:true,
message:message,
errors:null,
data:data
};
};
util.successFalse = function(err, message){
if(!err&&!message) message = 'data not found';
return {
success:false,
message:message,
errors:(err)? err.stack: null,
data:null
};
};
// middlewares
util.isLoggedin = function(req,res,next){
var token = req.headers['x-access-token'];
if (!token) return res.json(util.successFalse(null,'token is required!'));
else {
jwt.verify(token, process.env.JWT_SECRET, function(err, decoded) {
if(err) return res.json(util.successFalse(err));
else{
req.decoded = decoded;
next();
}
});
}
};
module.exports = util;