-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
36 lines (29 loc) · 898 Bytes
/
auth.js
File metadata and controls
36 lines (29 loc) · 898 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
30
31
32
33
34
35
36
const jwt = require('jsonwebtoken');
const jwksClient = require('jwks-rsa');
// define client - connected to Auth0 account
// putting return value of jsksClient in client
// from jsonWebToken docs
// jwks-rsa has to be added seperately
const client = jwksClient({
jwksUri: process.env.JWKS_URI
});
// from: https://www.npmjs.com/package/jsonwebtoken
function getKey(header, callback) {
client.getSigningKey(header.kid, function (err, key) {
const signingKey = key.publicKey || key.rsaPublicKey;
callback(null, signingKey);
});
}
// verify user on route
function verifyUser(req, callback) {
try {
// define token from req header
const token = req.headers.authorization.split(' ')[1];
// console.log(token);
// from jsonWebToken docs
jwt.verify(token, getKey, {}, callback);
} catch (e) {
callback('Not Authorized');
}
}
module.exports = verifyUser;