Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require("express");
- const router = express.Router();
- const { from, EMPTY } = require("rxjs");
- const { map, mergeMap } = require("rxjs/operators");
- const bcrypt = require("bcrypt");
- const saltRounds = 12;
- const mongo = require("mongodb");
- const MongoClient = mongo.MongoClient;
- const url = "mongodb://localhost:27017";
- const conn$ = from(MongoClient.connect(url));
- // Mongo aux methods
- const finduserByUsername = (collection, username) =>
- collection.find({ username }).toArray();
- const insertUser = (collection, user) => collection.insertOne(user);
- // Pipe methods
- const retrieveUser = map(array => array[0]);
- const detectNewUser = map(userData => {
- if (userData) {
- throw new Error("User already exists");
- } else {
- return EMPTY;
- }
- });
- const hashPassword = (password, saltRounds) =>
- mergeMap(_ => from(bcrypt.hash(password, saltRounds)));
- const addPasswordHashToUser = userObj =>
- map(hash => ({ ...userObj, passwordHash: hash }));
- const comparePassword = pwd =>
- mergeMap(user => from(bcrypt.compare(pwd, user.passwordHash)));
- const addUser = collection =>
- mergeMap(user => from(insertUser(collection, user)));
- // Client connection
- conn$.subscribe(client => {
- const authstrongDB = client.db("authstrong");
- const usersColl = authstrongDB.collection("users");
- router.post("/authenticate", (req, res) => {
- const username = req.body.username || null;
- const password = req.body.password || null;
- const validUsername = username.match(/^[a-zA-Z]+$/);
- if (!!validUsername && password) {
- const auth$ = from(finduserByUsername(usersColl, username)).pipe(
- retrieveUser,
- comparePassword(password)
- );
- const next = authValid =>
- authValid
- ? res.status(200).send("Welcome to WestWorld.")
- : res.status(401).send("Wrong email or password.");
- const error = err => {
- // integrate a mechanism to log errors
- res.status(500).send("Wrong email or password.");
- };
- auth$.subscribe(next, error);
- } else {
- res.status(401).send("Wrong email or password.");
- }
- });
- router.post("/create", (req, res) => {
- const username = req.body.username || null;
- const password = req.body.password || null;
- const firstName = req.body.firstName || null;
- const lastName = req.body.lastName || null;
- const nickname = req.body.nickname || null;
- const status = req.body.status || null;
- const validUsername = username.match(/^[a-zA-Z]+$/);
- if (!!validUsername && password && firstName && lastName && status) {
- const createUser$ = from(usersColl.findOne({ username })).pipe(
- detectNewUser,
- hashPassword(password, saltRounds),
- addPasswordHashToUser({
- username,
- firstName,
- lastName,
- nickname,
- status
- }),
- addUser(usersColl)
- );
- const next = commandResult => res.status(200).send(`User created.`);
- const error = err => {
- console.log(err.message);
- res.status(500).send("Error: Invalid form data.");
- };
- createUser$.subscribe(next, error);
- } else {
- res.status(500).send("Error: Invalid form data.");
- }
- });
- });
- module.exports = router;
Add Comment
Please, Sign In to add comment