From ab1ebdd5dd6e39f040af5e5691843f29e9ec4fc0 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Fri, 29 Sep 2017 10:47:12 +0200 Subject: [PATCH] Error message if signup failed --- controllers/auth/templates/signup.pug | 6 +++--- controllers/auth/views.js | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/controllers/auth/templates/signup.pug b/controllers/auth/templates/signup.pug index 64adfc1..7adc148 100644 --- a/controllers/auth/templates/signup.pug +++ b/controllers/auth/templates/signup.pug @@ -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='') diff --git a/controllers/auth/views.js b/controllers/auth/views.js index 006dcd4..8048ec0 100644 --- a/controllers/auth/views.js +++ b/controllers/auth/views.js @@ -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')); }