Created
February 6, 2020 20:55
-
-
Save lindenmelvin/0c11a5d4a13029561c20e5b2bf54bcb6 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
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