Files
Eventment-API/routes/signup.js
2019-08-17 02:47:31 -04:00

107 lines
2.4 KiB
JavaScript

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) {
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', auth.basic, (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', auth.basic, (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', auth.basic, (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();
});
});
};