-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.js
More file actions
29 lines (26 loc) · 837 Bytes
/
middleware.js
File metadata and controls
29 lines (26 loc) · 837 Bytes
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
const jwt = require('jsonwebtoken');
var fs = require('fs');
const path = require("path");
const withAuth = function(req, res, next) {
let token = req.headers['x-access-token'] || req.headers['authorization'] || req.cookies.auth;
if (!token) {
res.status(401).json({message: "Un-Authorized."})
} else {
if (token.startsWith('Bearer ')) {
token = token.slice(7, token.length);
}
var verifyOptions = {
expiresIn: "24h",
algorithms: "RS256"
};
// PUBLIC key
var publicKey = fs.readFileSync(path.resolve(__dirname, './keys/'+process.env["PUBLIC_KEY_NAME"]), 'utf8');
try {
var verified = jwt.verify(token, publicKey, verifyOptions);
}catch(err) {
return res.status(401).json({message: "Un-Authorized."})
}
next();
}
}
module.exports = withAuth;