Created
January 30, 2019 00:50
-
-
Save THEozmic/5868457a1b0c7a5fa145a34a9b8b6543 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jwt from 'jsonwebtoken' | |
import jwksClient from 'jwks-rsa' | |
export const respondWith = ({ statusCode, response = {} }) => { | |
return { | |
statusCode, | |
headers: { | |
"Content-Type": "application/json", | |
"Access-Control-Allow-Origin": "*", | |
"Access-Control-Allow-Headers": "*", | |
"Access-Control-Allow-Methods": "*", | |
}, | |
body: JSON.stringify(response) | |
} | |
} | |
export const verifyToken = async (token) => { | |
try { | |
const client = jwksClient({ | |
jwksUri: 'https://micro-blog.auth0.com/.well-known/jwks.json' | |
}); | |
const getKey = (header, callback) => { | |
client.getSigningKey(header.kid, (err, key) => { | |
const signingKey = key.publicKey || key.rsaPublicKey | |
callback(null, signingKey) | |
}); | |
} | |
return new Promise((resolve, reject) => { | |
jwt.verify(token, getKey, { algorithms: ['RS256'] }, (err, decoded) => { | |
if (err) return reject(err) | |
return resolve(decoded) | |
}) | |
}) | |
} catch (e) { | |
throw 'error occurred: ' + e.message | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment