-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatics.js
48 lines (44 loc) · 1.31 KB
/
statics.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
var statics = {};
var jwt = require('jsonwebtoken');
var appSec = "Israeeltumharamebehnkochodon";
statics.appSecret = appSec;
statics.authenticateMiddleWare = function (req, res, next) {
var token = req.body.token || req.query.token || req.headers['x-access-token'];
if (token) {
jwt.verify(token, appSec, function(err, decoded) {
if (err) {
return res.json({ success: false, message: 'Failed to authenticate token.' });
} else {
req.decoded = decoded.user;
next();
}
});
} else {
return res.status(403).send({
success: false,
message: 'No token provided.'
});
}
};
statics.getToken = function(email){
return jwt.sign({user: email}, appSec, {
expiresIn : 60*60*24*100
});
};
statics.getDistanceFromLatLonInKm = function(lat1,lon1,lat2,lon2) {
var R = 6371; // Radius of the earth in km
var dLat = deg2rad(lat2-lat1); // deg2rad below
var dLon = deg2rad(lon2-lon1);
var a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c; // Distance in km
return d;
}
function deg2rad(deg) {
return deg * (Math.PI/180)
}
module.exports = statics;