diff options
Diffstat (limited to 'internal/handlers')
-rw-r--r-- | internal/handlers/auth.go | 56 | ||||
-rw-r--r-- | internal/handlers/main.go | 46 |
2 files changed, 77 insertions, 25 deletions
diff --git a/internal/handlers/auth.go b/internal/handlers/auth.go index e7eca50..0287960 100644 --- a/internal/handlers/auth.go +++ b/internal/handlers/auth.go @@ -11,6 +11,8 @@ import ( "net/http" "strings" "time" + + "golang.org/x/crypto/bcrypt" ) func abortWithError(w http.ResponseWriter, msg string) { @@ -18,7 +20,7 @@ func abortWithError(w http.ResponseWriter, msg string) { tmpl.ExecuteTemplate(w, "error", msg) } -func (h *Handlers) HandleLogin(w http.ResponseWriter, r *http.Request) { +func (h *Handlers) HandleSignup(w http.ResponseWriter, r *http.Request) { r.ParseForm() username := r.PostFormValue("username") if username == "" { @@ -34,7 +36,24 @@ func (h *Handlers) HandleLogin(w http.ResponseWriter, r *http.Request) { abortWithError(w, msg) return } + // TODO: make sure username does not exists cleanName := utils.RemoveSpacesFromStr(username) + hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), 8) + // create user in db + now := time.Now() + nextMidnight := time.Date(now.Year(), now.Month(), now.Day(), + 0, 0, 0, 0, time.UTC).Add(time.Hour * 24) + newUser := &models.UserScore{ + Username: cleanName, Password: string(hashedPassword), + BurnTime: nextMidnight, CreatedAt: now, + } + if err := h.repo.DBUserScoreCreate(newUser); err != nil { + msg := "failed to create user" + h.log.Error(msg, "user", newUser) + abortWithError(w, msg) + return + } + // TODO: login user cookie, err := h.makeCookie(cleanName, r.RemoteAddr) if err != nil { h.log.Error("failed to login", "error", err) @@ -47,12 +66,33 @@ func (h *Handlers) HandleLogin(w http.ResponseWriter, r *http.Request) { if err != nil { panic(err) } + tmpl.ExecuteTemplate(w, "main", newUser) +} + +func (h *Handlers) HandleLogin(w http.ResponseWriter, r *http.Request) { + r.ParseForm() + username := r.PostFormValue("username") + if username == "" { + msg := "username not provided" + h.log.Error(msg) + abortWithError(w, msg) + return + } + password := r.PostFormValue("password") + if password == "" { + msg := "password not provided" + h.log.Error(msg) + abortWithError(w, msg) + return + } + cleanName := utils.RemoveSpacesFromStr(username) + tmpl, err := template.ParseGlob("components/*.html") + if err != nil { + panic(err) + } userScore, err := h.repo.DBUserScoreGet(cleanName) if err != nil { h.log.Warn("got db err", "err", err) - if err := h.repo.DBUserScoreCreate(&us); err != nil { - panic(err) - } tmpl.ExecuteTemplate(w, "main", nil) return } @@ -60,6 +100,14 @@ func (h *Handlers) HandleLogin(w http.ResponseWriter, r *http.Request) { if err != nil { panic(err) } + cookie, err := h.makeCookie(cleanName, r.RemoteAddr) + if err != nil { + h.log.Error("failed to login", "error", err) + abortWithError(w, err.Error()) + return + } + http.SetCookie(w, cookie) + // http.Redirect(w, r, "/", 302) tmpl.ExecuteTemplate(w, "main", userScore) } diff --git a/internal/handlers/main.go b/internal/handlers/main.go index aa9db4f..e87c74f 100644 --- a/internal/handlers/main.go +++ b/internal/handlers/main.go @@ -39,13 +39,6 @@ func NewHandlers( return h } -// FIXME: global userscore for test -var us = models.UserScore{ - Username: "test", - BurnTime: time.Now().Add(time.Duration(24) * time.Hour), - CreatedAt: time.Now(), -} - func (h *Handlers) Ping(w http.ResponseWriter, r *http.Request) { h.log.Info("got ping request") w.Write([]byte("pong")) @@ -70,10 +63,7 @@ func (h *Handlers) MainPage(w http.ResponseWriter, r *http.Request) { userScore, err := h.repo.DBUserScoreGet(username) if err != nil { h.log.Warn("got db err", "err", err) - if err := h.repo.DBUserScoreCreate(&us); err != nil { - panic(err) - } - tmpl.ExecuteTemplate(w, "main", us) + tmpl.ExecuteTemplate(w, "main", nil) return } userScore.Actions, err = h.repo.DBActionList(username) @@ -120,8 +110,10 @@ func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) { Repeatable: repeat, CreatedAt: time.Now(), } - // TODO: get username from ctx - userScore, err := h.repo.DBUserScoreGet("test") + // get username from ctx + username := r.Context().Value("username").(string) + h.log.Info("got username from ctx", "username", username) + userScore, err := h.repo.DBUserScoreGet(username) if err != nil { panic(err) } @@ -135,11 +127,11 @@ func (h *Handlers) HandleForm(w http.ResponseWriter, r *http.Request) { func (h *Handlers) UserScoreWithActionsByUsername( username string, ) (*models.UserScore, error) { - userScore, err := h.repo.DBUserScoreGet("test") + userScore, err := h.repo.DBUserScoreGet(username) if err != nil { return nil, err } - list, err := h.repo.DBActionList("test") + list, err := h.repo.DBActionList(username) if err != nil { return nil, err } @@ -149,18 +141,30 @@ func (h *Handlers) UserScoreWithActionsByUsername( func (h *Handlers) HandleDoneAction(w http.ResponseWriter, r *http.Request) { r.ParseForm() - h.log.Info("got done request", "payload", r.PostForm) actionName := r.PostFormValue("name") - h.log.Info("got postform request", "name", actionName) + username := r.Context().Value("username").(string) + h.log.Info("got postform request", "name", actionName, + "username", username) + userScore, err := h.UserScoreWithActionsByUsername(username) + if err != nil { + panic(err) + } + // get action by name + action, err := h.repo.DBActionGetByName(actionName) + magnitude := int8(action.Magnitude) + if action.Type == models.ActionTypeMinus { + magnitude *= -1 + } + // change counter of user score + userScore.Score += magnitude + // disable action if repetable if err := h.repo.DBActionDone(actionName); err != nil { panic(err) } - userScore, err := h.UserScoreWithActionsByUsername("test") - if err != nil { + // update score in db + if err := h.repo.DBUserScoreUpdate(userScore); err != nil { panic(err) } - // change counter of user score - // get action by name tmpl := template.Must(template.ParseGlob("components/*.html")) tmpl.ExecuteTemplate(w, "main", userScore) } |