Skip to content

Instantly share code, notes, and snippets.

@lindenmelvin
Created February 6, 2020 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lindenmelvin/0c11a5d4a13029561c20e5b2bf54bcb6 to your computer and use it in GitHub Desktop.
Save lindenmelvin/0c11a5d4a13029561c20e5b2bf54bcb6 to your computer and use it in GitHub Desktop.
const jwt = require('jsonwebtoken');
module.exports = {
authenticate: (email, password) => {
let user;
// Implement the credential validation however you'd like.
if (email.length && password.length) {
user = {
id: 1,
roles: ["admin"]
}
}
if (!user) throw new Error("Invalid credentials.");
return jwt.sign(user, process.env.JWT_SECRET);
},
validateToken: token => {
try {
const { id, roles } = jwt.verify(token, process.env.JWT_SECRET);
return { id, roles };
} catch (e) {
throw new Error('Authentication token is invalid.');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment