Guest User

Untitled

a guest
Jun 23rd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. const express = require("express");
  2. const router = express.Router();
  3.  
  4. const { from, EMPTY } = require("rxjs");
  5. const { map, mergeMap } = require("rxjs/operators");
  6.  
  7. const bcrypt = require("bcrypt");
  8. const saltRounds = 12;
  9.  
  10. const mongo = require("mongodb");
  11. const MongoClient = mongo.MongoClient;
  12. const url = "mongodb://localhost:27017";
  13.  
  14. const conn$ = from(MongoClient.connect(url));
  15.  
  16. // Mongo aux methods
  17.  
  18. const finduserByUsername = (collection, username) =>
  19. collection.find({ username }).toArray();
  20.  
  21. const insertUser = (collection, user) => collection.insertOne(user);
  22.  
  23. // Pipe methods
  24.  
  25. const retrieveUser = map(array => array[0]);
  26.  
  27. const detectNewUser = map(userData => {
  28. if (userData) {
  29. throw new Error("User already exists");
  30. } else {
  31. return EMPTY;
  32. }
  33. });
  34.  
  35. const hashPassword = (password, saltRounds) =>
  36. mergeMap(_ => from(bcrypt.hash(password, saltRounds)));
  37.  
  38. const addPasswordHashToUser = userObj =>
  39. map(hash => ({ ...userObj, passwordHash: hash }));
  40.  
  41. const comparePassword = pwd =>
  42. mergeMap(user => from(bcrypt.compare(pwd, user.passwordHash)));
  43.  
  44. const addUser = collection =>
  45. mergeMap(user => from(insertUser(collection, user)));
  46.  
  47. // Client connection
  48.  
  49. conn$.subscribe(client => {
  50. const authstrongDB = client.db("authstrong");
  51. const usersColl = authstrongDB.collection("users");
  52.  
  53. router.post("/authenticate", (req, res) => {
  54. const username = req.body.username || null;
  55. const password = req.body.password || null;
  56.  
  57. const validUsername = username.match(/^[a-zA-Z]+$/);
  58.  
  59. if (!!validUsername && password) {
  60. const auth$ = from(finduserByUsername(usersColl, username)).pipe(
  61. retrieveUser,
  62. comparePassword(password)
  63. );
  64.  
  65. const next = authValid =>
  66. authValid
  67. ? res.status(200).send("Welcome to WestWorld.")
  68. : res.status(401).send("Wrong email or password.");
  69.  
  70. const error = err => {
  71. // integrate a mechanism to log errors
  72. res.status(500).send("Wrong email or password.");
  73. };
  74.  
  75. auth$.subscribe(next, error);
  76. } else {
  77. res.status(401).send("Wrong email or password.");
  78. }
  79. });
  80.  
  81. router.post("/create", (req, res) => {
  82. const username = req.body.username || null;
  83. const password = req.body.password || null;
  84. const firstName = req.body.firstName || null;
  85. const lastName = req.body.lastName || null;
  86. const nickname = req.body.nickname || null;
  87. const status = req.body.status || null;
  88.  
  89. const validUsername = username.match(/^[a-zA-Z]+$/);
  90.  
  91. if (!!validUsername && password && firstName && lastName && status) {
  92. const createUser$ = from(usersColl.findOne({ username })).pipe(
  93. detectNewUser,
  94. hashPassword(password, saltRounds),
  95. addPasswordHashToUser({
  96. username,
  97. firstName,
  98. lastName,
  99. nickname,
  100. status
  101. }),
  102. addUser(usersColl)
  103. );
  104.  
  105. const next = commandResult => res.status(200).send(`User created.`);
  106.  
  107. const error = err => {
  108. console.log(err.message);
  109. res.status(500).send("Error: Invalid form data.");
  110. };
  111.  
  112. createUser$.subscribe(next, error);
  113. } else {
  114. res.status(500).send("Error: Invalid form data.");
  115. }
  116. });
  117. });
  118.  
  119. module.exports = router;
Add Comment
Please, Sign In to add comment