Handle user creation upon registration.

This commit is contained in:
mikestefanello 2021-12-11 13:03:10 -05:00
parent 79d2db3c1f
commit 8657380530
13 changed files with 201 additions and 46 deletions

View file

@ -1,8 +1,11 @@
package controllers
import (
"goweb/ent/user"
"goweb/msg"
"golang.org/x/crypto/bcrypt"
"github.com/labstack/echo/v4"
)
@ -20,6 +23,27 @@ func (l *Login) Get(c echo.Context) error {
}
func (l *Login) Post(c echo.Context) error {
msg.Danger(c, "Invalid credentials. Please try again.")
name := c.FormValue("username")
pw := c.FormValue("password")
if name == "" || pw == "" {
msg.Warning(c, "All fields are required.")
return l.Get(c)
}
u, err := l.Container.Ent.User.
Query().
Where(user.Username(name)).
First(c.Request().Context())
if err != nil {
c.Logger().Errorf("error querying user during login: %v", err)
} else {
err = bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(pw))
if err != nil {
msg.Danger(c, "Invalid credentials. Please try again.")
}
}
return l.Get(c)
}