Guest User

Untitled

a guest
Jul 12th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. const express = require('express');
  2.  
  3. const queryString = require('query-string');
  4.  
  5.  
  6. const app = express();
  7.  
  8.  
  9. const USERS = [
  10. {id: 1,
  11. firstName: 'Joe',
  12. lastName: 'Schmoe',
  13. userName: 'joeschmoe@business.com',
  14. position: 'Sr. Engineer',
  15. isAdmin: true,
  16. // NEVER EVER EVER store passwords in plain text in real life. NEVER!!!!!!!!!!!
  17. password: 'password'
  18. },
  19. {id: 2,
  20. firstName: 'Sally',
  21. lastName: 'Student',
  22. userName: 'sallystudent@business.com',
  23. position: 'Jr. Engineer',
  24. isAdmin: true,
  25. // NEVER EVER EVER store passwords in plain text in real life. NEVER!!!!!!!!!!!
  26. password: 'password'
  27. },
  28. {id: 3,
  29. firstName: 'Lila',
  30. lastName: 'LeMonde',
  31. userName: 'lila@business.com',
  32. position: 'Growth Hacker',
  33. isAdmin: false,
  34. // NEVER EVER EVER store passwords in plain text in real life. NEVER!!!!!!!!!!!
  35. password: 'password'
  36. },
  37. {id: 4,
  38. firstName: 'Freddy',
  39. lastName: 'Fun',
  40. userName: 'freddy@business.com',
  41. position: 'Community Manager',
  42. isAdmin: false,
  43. // NEVER EVER EVER store passwords in plain text in real life. NEVER!!!!!!!!!!!
  44. password: 'password'
  45. }
  46. ];
  47.  
  48.  
  49.  
  50. function gateKeeper(req, res, next){
  51.  
  52. const {user, pass} = queryString.parse(req.get("x-username-and-password"));
  53.  
  54. req.user = USERS.find(usr => usr.userName === user && usr.password === pass);
  55.  
  56. next();
  57.  
  58. }
  59.  
  60. app.use(gateKeeper);
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. app.get("/api/users/me", (req, res) => {
  69. if (req.user === undefined) {
  70. return res.status(403).json({message: 'Must supply valid user credentials'});
  71. }
  72.  
  73. const {firstName, lastName, id, userName, position} = req.user;
  74. return res.json({firstName, lastName, id, userName, position});
  75. });
  76.  
  77. app.listen(process.env.PORT, () => {
  78. console.log(`Your app is listening on port ${process.env.PORT}`);
  79. });
Add Comment
Please, Sign In to add comment