const errors = require('restify-errors'); const User = require('../models/user'); module.exports = function (server, auth) { const { passport } = auth; server.post('/signup', (req, res, next) => { const { body: { user = null } = {} } = req; let errors = {}; let errorCount = 0; if (!user) { errors.user = 'is required - can\'t make something from nothing...''; errorCount++; } if (errorCount) { return res.send(422, { errors }); } User.register(user, (err, user, info) => { if (err) { next(err); } if (info) { res.send(200, { success: false, nextSteps: 'Please fix the problems indicated and try again.' ...info }); return next(); } res.send(200, { success: true, nextSteps: 'Check your email for our confirmation email, you will not be able to login without confirming.' }); next(); }); }); server.get('/signup/validate/email/:email', (req, res, next) => { const email = decodeURI(req.params.email); User.findOne({ email }, (err, user) => { if (err) { return next(err); } res.send(200, { available: !!!user }); next(); }); }); server.get('/signup/validate/nom/:nom_de_bid', (req, res, next) => { const nomDeBid = decodeURI(req.params.nom_de_bid); User.findOne({ nomDeBid }, (err, user) => { if (err) { return next(err); } res.send(200, { available: !!!user }); next(); }); }); server.post('/signup/verify/resend', (req, res, next) => { const { body: { email = null } = {} } = req; User.resendVerificationEmail(email, (err, user, info) => { if (err) { next(err); } if (!user) { res.send(200, { success: false, nextSteps: 'There was no user located with the email address provided. Please try again.', }); return next(); } if (user && info.success) { res.send(200, { success: true, nextSteps: 'Check your email for our confirmation email, you will not be able to login without confirming.', }); return next(); } res.send(200, { success: false, nextSteps: 'There was a problem resending the verification email. Please try again later.', }); next(); }); }); };