-
-
Save harrisonmalone/7adbc7d1e55cebc109242539974d2fbf 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 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')) |
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 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