SHARE
TWEET

Untitled

a guest Jul 16th, 2019 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const bodyParser = require('body-parser');
  3.  
  4. const app = express();
  5.  
  6. // parse application/json
  7. app.use(bodyParser.json());
  8.  
  9. // parse application/x-www-form-urlencoded
  10. app.use(bodyParser.urlencoded({ extended: false }));
  11.  
  12. const port = 3001;
  13.  
  14. let users = [
  15.   {
  16.     id: 1,
  17.     email: 'johndoe',
  18.     password: 'password',
  19.     firstName: 'John',
  20.     lastName: 'Doe',
  21.   },
  22.   {
  23.     id: 2,
  24.     email: 'macmiller',
  25.     password: 'password',
  26.     firstName: 'mac',
  27.     lastName: 'Miller',
  28.   },
  29. ];
  30.  
  31. app.get('/', (req, res) => res.send('Hello World!'));
  32. app.post('/login', (req, res) => {
  33.   console.log(req.body);
  34.   // get parameters from post request
  35.   let params = req.body;
  36.  
  37.   // find if any user matches login credentials
  38.   let filteredUsers = users.filter((user) => {
  39.     return user.email === params.email && user.password === params.password;
  40.   });
  41.  
  42.   if (filteredUsers.length) {
  43.     // if login details are valid return user details and fake jwt token
  44.     let user = filteredUsers[0];
  45.     let responseJson = {
  46.       id: user.id,
  47.       email: user.email,
  48.       firstName: user.firstName,
  49.       lastName: user.lastName,
  50.       token: 'fake-jwt-token',
  51.     };
  52.     res.status(200).json(responseJson);
  53.   } else {
  54.     res.status(401).json({ message: 'Wrong Password or email' });
  55.   }
  56. });
  57.  
  58. app.post('/register', (req, res) => {
  59.   console.log(req.body);
  60.   // get parameters from post request
  61.   let params = req.body;
  62.   let user = {
  63.     id: Math.floor(Math.random() * 11),
  64.     email: params.email,
  65.     password: params.password,
  66.     firstName: params.firstName,
  67.     lastName: params.lastName,
  68.   };
  69.  
  70.  
  71.   try {
  72.     users.push(user);
  73.     console.log(users)
  74.     res
  75.       .status(200)
  76.       .json({
  77.         user: {
  78.           id: users.id,
  79.           email: users.email,
  80.           firstName: users.firstName,
  81.           lastName: users.lastName,
  82.           token: 'fake-jwt-token',
  83.         },
  84.       });
  85.   } catch {
  86.     res.status(401).json({ message: 'Wrong Password or email' });
  87.   }
  88. });
  89.  
  90. app.get('/users', (req, res) => {
  91.   if (req.headers && req.headers.authorization === 'Bearer fake-jwt-token') {
  92.     res.status(200).json(users);
  93.   } else {
  94.     // return 401 not authorised if token is null or invalid
  95.     res.status(401).json({ message: 'Unauthorized' });
  96.   }
  97. });
  98.  
  99. app.listen(port, () => console.log(`Example app listening on port ${port}!`));
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
 
Top