Error message if signup failed
This commit is contained in:
parent
1b37c6e755
commit
ab1ebdd5dd
|
@ -1,11 +1,11 @@
|
|||
extends ../../../templates/base.pug
|
||||
block content
|
||||
if registeringFailed
|
||||
.alert There was an error.
|
||||
|
||||
.row
|
||||
.col
|
||||
.col
|
||||
if signupFailed !== undefined
|
||||
.alert.alert-danger #{signupFailed}
|
||||
|
||||
form(method="POST", action=getUrl("signupTarget"))
|
||||
.form-group
|
||||
input.form-control(type='text', name='username', placeholder='Username', autofocus='')
|
||||
|
|
|
@ -5,6 +5,12 @@ const mail = require('mail');
|
|||
const pug = require('pug');
|
||||
const config = require('settings/config');
|
||||
|
||||
SIGNUP_ERRORS = {
|
||||
USERNAME_ALREADY_EXISTS: "The username you want is already used.",
|
||||
EMAIL_ALREADY_EXISTS: "The email you want is already used.",
|
||||
UNKOWN_ERROR: "Something weird happened, i'm sorry :'(",
|
||||
};
|
||||
|
||||
module.exports.redirectIfNotLogged = function(redirectionPage, toDecorate) {
|
||||
return function(req, res, render) {
|
||||
if (req.session.user === undefined) {
|
||||
|
@ -28,6 +34,11 @@ module.exports.login = function(req, res, render) {
|
|||
}
|
||||
|
||||
module.exports.signup = function(req, res, render) {
|
||||
if (req.session.signupFailed !== undefined) {
|
||||
res.locals.signupFailed = req.session.signupFailed;
|
||||
req.session.signupFailed = undefined;
|
||||
req.session.save();
|
||||
}
|
||||
render('signup.pug');
|
||||
}
|
||||
|
||||
|
@ -70,6 +81,21 @@ module.exports.signupTarget = function(req, res, render) {
|
|||
user.save((err) => {
|
||||
|
||||
if (err !== undefined) {
|
||||
|
||||
let error = SIGNUP_ERRORS.UNKOWN_ERROR;
|
||||
switch (err.constraint) {
|
||||
case 'auth_user_email_key':
|
||||
error = SIGNUP_ERRORS.EMAIL_ALREADY_EXISTS;
|
||||
break;
|
||||
case 'auth_user_username_key':
|
||||
error = SIGNUP_ERRORS.USERNAME_ALREADY_EXISTS;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
req.session.signupFailed = error;
|
||||
req.session.save();
|
||||
|
||||
return res.redirect(getUrl('signup'));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue