Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- // you'll need to use `queryString` in your `gateKeeper` middleware function
- const queryString = require('query-string');
- const app = express();
- const USERS = [
- {id: 1,
- firstName: 'Joe',
- lastName: 'Schmoe',
- userName: 'joeschmoe@business.com',
- position: 'Sr. Engineer',
- isAdmin: true,
- // NEVER EVER EVER store passwords in plain text in real life. NEVER!!!!!!!!!!!
- password: 'password'
- },
- {id: 2,
- firstName: 'Sally',
- lastName: 'Student',
- userName: 'sallystudent@business.com',
- position: 'Jr. Engineer',
- isAdmin: true,
- // NEVER EVER EVER store passwords in plain text in real life. NEVER!!!!!!!!!!!
- password: 'password'
- },
- {id: 3,
- firstName: 'Lila',
- lastName: 'LeMonde',
- userName: 'lila@business.com',
- position: 'Growth Hacker',
- isAdmin: false,
- // NEVER EVER EVER store passwords in plain text in real life. NEVER!!!!!!!!!!!
- password: 'password'
- },
- {id: 4,
- firstName: 'Freddy',
- lastName: 'Fun',
- userName: 'freddy@business.com',
- position: 'Community Manager',
- isAdmin: false,
- // NEVER EVER EVER store passwords in plain text in real life. NEVER!!!!!!!!!!!
- password: 'password'
- }
- ];
- function gateKeeper(req, res, next) {
- const string = req.headers['x-username-and-password']
- const userQuery = queryString.parse(string)
- const matchedUser = USERS.find((user) => {
- return user.userName === userQuery.user
- })
- req.user = matchedUser
- next();
- }
- app.use(gateKeeper)
- app.get("/api/users/me", (req, res) => {
- if (req.user === undefined) {
- return res.status(403).json({message: 'Must supply valid user credentials'});
- }
- const {firstName, lastName, id, userName, position} = req.user;
- return res.json({firstName, lastName, id, userName, position});
- });
- app.listen(process.env.PORT, () => {
- console.log(`Your app is listening on port ${process.env.PORT}`);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement