Validate if the email address is already in use during registration.

This commit is contained in:
mikestefanello 2021-12-16 20:58:38 -05:00
parent b383be5dac
commit bd5bbab47c
2 changed files with 25 additions and 7 deletions

View file

@ -3,6 +3,7 @@ package routes
import (
"goweb/context"
"goweb/controller"
"goweb/ent/user"
"goweb/msg"
"github.com/labstack/echo/v4"
@ -42,8 +43,6 @@ func (r *Register) Post(c echo.Context) error {
return r.Get(c)
}
// TODO: Validation for dupe email addresses
// Parse the form values
form := new(RegisterForm)
if err := c.Bind(form); err != nil {
@ -57,6 +56,20 @@ func (r *Register) Post(c echo.Context) error {
return r.Get(c)
}
// Check if the email address is taken
exists, err := r.Container.ORM.User.
Query().
Where(user.Email(form.Email)).
Exist(c.Request().Context())
switch {
case err != nil:
return fail("unable to query to see if email is taken", err)
case exists:
msg.Warning(c, "A user with this email address already exists. Please log in.")
return r.Redirect(c, "login")
}
// Hash the password
pwHash, err := r.Container.Auth.HashPassword(form.Password)
if err != nil {
@ -77,6 +90,7 @@ func (r *Register) Post(c echo.Context) error {
c.Logger().Infof("user created: %s", u.Name)
// Log the user in
err = r.Container.Auth.Login(c, u.ID)
if err != nil {
c.Logger().Errorf("unable to log in: %v", err)