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 { return func(c echo.Context) error {
addHeaders(c) addHeaders(c)
err := c.Request().ParseForm() cu := new(userSignin)
if err != nil { if err := c.Bind(cu); err != nil {
return err return renderErrorPage(
c,
http.StatusBadRequest,
http.StatusText(http.StatusBadRequest),
err.Error(),
)
} }
var username string username := cu.Username
password := cu.Password
var password string if username == "" || password == "" {
c.Logger().Error("username or password not set, returning to /signin")
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")
return c.Redirect(http.StatusFound, "/signin") return c.Redirect(http.StatusFound, "/signin")
} }

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