Error message if signup failed

This commit is contained in:
Thomas Forgione 2017-09-29 10:47:12 +02:00
parent 1b37c6e755
commit ab1ebdd5dd
No known key found for this signature in database
GPG Key ID: C75CD416BD1FFCE1
2 changed files with 29 additions and 3 deletions

View File

@ -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='')

View File

@ -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'));
}