const errors = require('restify-errors'); const User = require('../models/user'); module.exports = function (server, auth) { const { passport } = auth; server.post('/signup', auth.basic, (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) { return 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/confirm/:token([A-Za-z0-9_]+\.{3})', (req, res, next) => { const { token } = req.params; if (!token) { return next( new errors.InvalidContentError('A confirmation token was not provided.'), ); } User.verifyTokenAndConfirmRegistration(token, (err, user, info) => { if (err) { console.error(err); return next( new errors.InvalidContentError(err), ); } if (!user) { console.error(err); res.send({ success: false, info: `Account registration confirmation failed. ${info}`, }); return next(); } res.send({ success: true, info: 'New account registration confirmed.', ...user.toAuthJSON() }); 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(); }); }); };