SHARE
TWEET

Untitled

a guest Jun 15th, 2017 488 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const queryString = require('query-string');
  3. const app = express();
  4.  
  5. const USERS = [
  6.   {id: 1,
  7.    firstName: 'Joe',
  8.    lastName: 'Schmoe',
  9.    userName: 'joeschmoe@business.com',
  10.    position: 'Sr. Engineer',
  11.    isAdmin: true,
  12.    // NEVER EVER EVER store passwords in plain text in real life. NEVER!!!!!!!!!!!
  13.    password: 'password'
  14.   },
  15.   {id: 2,
  16.    firstName: 'Sally',
  17.    lastName: 'Student',
  18.    userName: 'sallystudent@business.com',
  19.    position: 'Jr. Engineer',
  20.    isAdmin: true,
  21.    // NEVER EVER EVER store passwords in plain text in real life. NEVER!!!!!!!!!!!
  22.    password: 'password'
  23.   },
  24.   {id: 3,
  25.    firstName: 'Lila',
  26.    lastName: 'LeMonde',
  27.    userName: 'lila@business.com',
  28.    position: 'Growth Hacker',
  29.    isAdmin: false,
  30.    // NEVER EVER EVER store passwords in plain text in real life. NEVER!!!!!!!!!!!
  31.    password: 'password'
  32.   },
  33.   {id: 4,
  34.    firstName: 'Freddy',
  35.    lastName: 'Fun',
  36.    userName: 'freddy@business.com',
  37.    position: 'Community Manager',
  38.    isAdmin: false,
  39.    // NEVER EVER EVER store passwords in plain text in real life. NEVER!!!!!!!!!!!
  40.    password: 'password'
  41.   }
  42. ];
  43.  
  44. function gateKeeper(req, res, next) {
  45.   const {user, pass} = Object.assign(
  46.     {user: null, pass: null}, queryString.parse(req.get('x-username-and-password')));
  47.  
  48.   req.user = USERS.find(
  49.     (usr, index) => usr.userName === user && usr.password === pass);
  50.   next();
  51. }
  52.  
  53. app.use(gateKeeper);
  54.  
  55. app.get("/api/users/me", (req, res) => {
  56.     if (req.user === undefined) {
  57.     return res.status(403).json({message: 'Must supply valid user credentials'});
  58.   }
  59.  
  60.   const {firstName, lastName, id, userName, position} = req.user;
  61.   return res.json({firstName, lastName, id, userName, position});
  62. });
  63.  
  64. app.listen(process.env.PORT, () => {
  65.   console.log(`Your app is listening on port ${process.env.PORT}`);
  66. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top