Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active September 30, 2019 23:19
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 harrisonmalone/7adbc7d1e55cebc109242539974d2fbf to your computer and use it in GitHub Desktop.
Save harrisonmalone/7adbc7d1e55cebc109242539974d2fbf to your computer and use it in GitHub Desktop.
const express = require('express');
const users = require('./users');
const app = express();
const authenticate = ({ username, password }) => {
const foundUser = users.find((user) => {
return user.username === username
})
if (foundUser) {
if (foundUser.password === password) {
return foundUser
}
}
}
const checkUser = (req, res, next) => {
const user = authenticate(req.body)
if (user) {
req.role = user.role
next()
} else {
return res.status(403).send('not authorized!')
}
}
app.post('/login', express.json(), checkUser, (req, res) => {
res.send('successfully accessing this endpoint!')
})
app.listen(5000, () => console.log('app listening on port 5000'))
const users = [
{
username: "harrison",
password: "password",
role: "admin"
},
{
username: "anhar",
password: "password",
role: "card game expert"
},
{
username: "lav",
password: "password",
role: "cricket expert"
},
{
username: "leah",
password: "password",
role: "meetups expert"
}
]
module.exports = users
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment