|
| 1 | +const express = require('express'); |
| 2 | +const bodyParser = require('body-parser'); |
| 3 | +const passport = require('passport'); |
| 4 | + |
| 5 | +const {User} = require('./models'); |
| 6 | + |
| 7 | +const router = express.Router(); |
| 8 | + |
| 9 | +const jsonParser = bodyParser.json(); |
| 10 | + |
| 11 | +// Post to register a new user |
| 12 | +router.post('/', jsonParser, (req, res) => { |
| 13 | + const requiredFields = ['username', 'password']; |
| 14 | + const missingField = requiredFields.find(field => !(field in req.body)); |
| 15 | + |
| 16 | + if (missingField) { |
| 17 | + return res.status(422).json({ |
| 18 | + code: 422, |
| 19 | + reason: 'ValidationError', |
| 20 | + message: 'Missing field', |
| 21 | + location: missingField |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + const stringFields = ['username', 'password', 'firstName', 'lastName']; |
| 26 | + const nonStringField = stringFields.find(field => |
| 27 | + (field in req.body) && typeof req.body[field] !== 'string' |
| 28 | + ); |
| 29 | + |
| 30 | + if (nonStringField) { |
| 31 | + return res.status(422).json({ |
| 32 | + code: 422, |
| 33 | + reason: 'ValidationError', |
| 34 | + message: 'Incorrect field type: expected string', |
| 35 | + location: nonStringField |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + const nonEmptyFields = ['username', 'password']; |
| 40 | + const emptyField = nonEmptyFields.find(field => req.body[field].trim() === ''); |
| 41 | + |
| 42 | + if (emptyField) { |
| 43 | + return res.status(422).json({ |
| 44 | + code: 422, |
| 45 | + reason: 'ValidationError', |
| 46 | + message: 'Incorrect field length', |
| 47 | + location: emptyField |
| 48 | + }); |
| 49 | + } |
| 50 | + |
| 51 | + let {username, password, firstName, lastName} = req.body; |
| 52 | + |
| 53 | + return User |
| 54 | + .find({username}) |
| 55 | + .count() |
| 56 | + .then(count => { |
| 57 | + if (count > 0) { |
| 58 | + // There is an existing user with the same username |
| 59 | + return Promise.reject({ |
| 60 | + code: 422, |
| 61 | + reason: 'ValidationError', |
| 62 | + message: 'Username already taken', |
| 63 | + location: 'username' |
| 64 | + }); |
| 65 | + } |
| 66 | + // If there is no existing user, hash the password |
| 67 | + return User.hashPassword(password) |
| 68 | + }) |
| 69 | + .then(hash => { |
| 70 | + return User |
| 71 | + .create({ |
| 72 | + username, |
| 73 | + password: hash, |
| 74 | + firstName, |
| 75 | + lastName |
| 76 | + }) |
| 77 | + }) |
| 78 | + .then(user => { |
| 79 | + return res.status(201).json(user.apiRepr()); |
| 80 | + }) |
| 81 | + .catch(err => { |
| 82 | + // Forward validation errors on to the client, otherwise give a 500 |
| 83 | + // error because something unexpected has happened |
| 84 | + if (err.reason === 'ValidationError') { |
| 85 | + return res.status(err.code).json(err); |
| 86 | + } |
| 87 | + res.status(500).json({code: 500, message: 'Internal server error'}); |
| 88 | + }); |
| 89 | +}); |
| 90 | + |
| 91 | +// Never expose all your users like below in a prod application |
| 92 | +// we're just doing this so we have a quick way to see |
| 93 | +// if we're creating users. keep in mind, you can also |
| 94 | +// verify this in the Mongo shell. |
| 95 | +router.get('/', (req, res) => { |
| 96 | + return User |
| 97 | + .find() |
| 98 | + .then(users => res.json(users.map(user => user.apiRepr()))) |
| 99 | + .catch(err => res.status(500).json({message: 'Internal server error'})); |
| 100 | +}); |
| 101 | + |
| 102 | +module.exports = {router}; |
0 commit comments