forked from N-Germek/spirits-n-spirits-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
45 lines (34 loc) · 1.36 KB
/
auth.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
'use strict';
const jwt = require('jsonwebtoken'); // auth
const jwksClient = require('jwks-rsa'); // auth
function verifyUser(request, response, next) {
function valid(err, user) {
request.user = user;
next();
}
try {
const token = request.headers.authorization.split(' ')[1];
// this console allows me to grab the token so I can use it to test it in ThunderClient
// make a request from the client-side, get my token back, then test it in ThunderClient
console.log("Token: ", token);
// we get .verify from jwt - it verifies the user
jwt.verify(token, getKey, {}, valid);
} catch (error) {
next('Not Authorized');
}
}
// =============== HELPER METHODS, pulled from the jsonwebtoken documentation =================== //
// https://www.npmjs.com/package/jsonwebtoken //
// Define a client, this is a connection to YOUR auth0 account, using the URL given in your dashboard
const client = jwksClient({
// this url comes from your app on the auth0 dashboard
jwksUri: process.env.JWKS_URI,
});
// Match the JWT's key to your Auth0 Account Key so we can validate it
function getKey(header, callback) {
client.getSigningKey(header.kid, function (err, key) {
const signingKey = key.publicKey || key.rsaPublicKey;
callback(null, signingKey);
});
}
module.exports = verifyUser;