handlers(echo): use c.Bind in sign{in,up}
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
leo 2023-05-15 23:57:38 +02:00
parent 15918d4cd7
commit 695039e882
Signed by: wanderer
SSH Key Fingerprint: SHA256:Dp8+iwKHSlrMEHzE3bJnPng70I7LEsa3IJXRH/U+idQ
3 changed files with 39 additions and 46 deletions

@ -42,28 +42,21 @@ func SigninPost(client *ent.Client) echo.HandlerFunc {
return func(c echo.Context) error {
addHeaders(c)
err := c.Request().ParseForm()
if err != nil {
return err
cu := new(userSignin)
if err := c.Bind(cu); err != nil {
return renderErrorPage(
c,
http.StatusBadRequest,
http.StatusText(http.StatusBadRequest),
err.Error(),
)
}
var username string
username := cu.Username
password := cu.Password
var password string
if uname := c.Request().FormValue("username"); uname != "" {
username = uname
log.Infof("authenticating user '%s' at /signin", username)
} else {
log.Info("username was not set, returning to /signin")
return c.Redirect(http.StatusFound, "/signin")
}
if passwd := c.Request().FormValue("password"); passwd != "" {
password = passwd
} else {
log.Info("password was not set, returning to /signin")
if username == "" || password == "" {
c.Logger().Error("username or password not set, returning to /signin")
return c.Redirect(http.StatusFound, "/signin")
}

@ -73,36 +73,24 @@ func SignupPost(client *ent.Client) echo.HandlerFunc {
return func(c echo.Context) error {
addHeaders(c)
err := c.Request().ParseForm()
if err != nil {
return err
cu := new(userSignup)
if err := c.Bind(cu); err != nil {
return renderErrorPage(
c,
http.StatusBadRequest,
http.StatusText(http.StatusBadRequest),
err.Error(),
)
}
var username string
var email string
uname := c.Request().FormValue("username")
if uname == "" {
c.Logger().Error("signup: username was not set, returning to /signup")
return c.Redirect(http.StatusSeeOther, "/signup")
if cu.Username == "" || cu.Email == "" || cu.Password == "" {
c.Logger().Error("username or email or password not set, returning to /singup")
return c.Redirect(http.StatusFound, "/singup")
}
username = uname
mail := c.Request().FormValue("email")
if mail == "" {
c.Logger().Error("signup: email not set")
return c.Redirect(http.StatusSeeOther, "/signup")
}
email = mail
passwd := c.Request().FormValue("password")
if passwd == "" {
log.Info("signup: password was not set, returning to /signup")
return c.Redirect(http.StatusSeeOther, "/signup")
}
username := cu.Username
email := cu.Email
passwd := cu.Password
ctx := context.WithValue(context.Background(), moduser.CtxKey{}, slogger)
@ -147,7 +135,7 @@ func SignupPost(client *ent.Client) echo.HandlerFunc {
)
}
log.Infof("successfully registered user '%s'", username)
log.Infof("successfully registered user '%s'", u.Username)
log.Debug("user details", "id", u.ID, "email", u.Email, "isAdmin", u.IsAdmin)
secure := c.Request().URL.Scheme == "https" //nolint:goconst

12
handlers/type.go Normal file

@ -0,0 +1,12 @@
package handlers
type userSignin struct {
Username string `form:"username" json:"username"`
Password string `form:"password" json:"password"`
}
type userSignup struct {
Username string `form:"username" json:"username"`
Email string `form:"email" json:"email"`
Password string `form:"password" json:"password"`
}