From c1bb129b874383a3a0cc8002fd3e4f160dbd5a53 Mon Sep 17 00:00:00 2001 From: tamsin johnson Date: Wed, 7 Feb 2024 11:22:47 -0800 Subject: [PATCH] lets-go:11.0 user login without peeking just the bones here, not authenticating at all yet. going ahead with the chapter now because i'm curious about the preferred way to authenticate; are we going to put this on `UserModel` or some other `internal` locale, or just in `helpers.go`? --- snippetbox/cmd/web/handlers.go | 42 ++++++++++++++++++++++++++++- snippetbox/cmd/web/routes.go | 4 +-- snippetbox/ui/html/pages/login.tmpl | 22 +++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 snippetbox/ui/html/pages/login.tmpl diff --git a/snippetbox/cmd/web/handlers.go b/snippetbox/cmd/web/handlers.go index a7921c2..670579a 100644 --- a/snippetbox/cmd/web/handlers.go +++ b/snippetbox/cmd/web/handlers.go @@ -118,7 +118,7 @@ func (app *application) userSignup(w http.ResponseWriter, r *http.Request) { } -// userSignup ... +// userSignupPost ... func (app *application) userSignupPost(w http.ResponseWriter, r *http.Request) { var form userSignupForm @@ -148,3 +148,43 @@ func (app *application) userSignupPost(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/", http.StatusSeeOther) } + +type userLoginForm struct { + Username string `form:"username"` + Password string `form:"password"` + validator.Validator `form:"-"` +} + +// userLogin ... +func (app *application) userLogin(w http.ResponseWriter, r *http.Request) { + data := app.newTemplateData(r) + + data.Form = userLoginForm{} + + app.render(w, r, http.StatusOK, "login.tmpl", data) +} + +// userLoginPost ... +func (app *application) userLoginPost(w http.ResponseWriter, r *http.Request) { + var form userSignupForm + + err := app.decodePostForm(r, &form) + if err != nil { + app.clientError(w, http.StatusBadRequest) + return + } + + form.CheckField(validator.NotBlank(form.Username), "username", "This field cannot be blank") + form.CheckField(validator.NotBlank(form.Password), "password", "This field cannot be blank") + + if !form.Valid() { + data := app.newTemplateData(r) + data.Form = form + app.render(w, r, http.StatusUnprocessableEntity, "login.tmpl", data) + return + } + + app.sessionManager.Put(r.Context(), "flash", fmt.Sprintf("LOGGED IN USER %s! (not really)", form.Username)) + + http.Redirect(w, r, "/", http.StatusSeeOther) +} diff --git a/snippetbox/cmd/web/routes.go b/snippetbox/cmd/web/routes.go index a5fc868..fb4574d 100644 --- a/snippetbox/cmd/web/routes.go +++ b/snippetbox/cmd/web/routes.go @@ -31,8 +31,8 @@ func (app *application) routes() http.Handler { router.Handler(http.MethodPost, "/snippet/create", dynamic.ThenFunc(app.snippetCreatePost)) router.Handler(http.MethodGet, "/user/signup", dynamic.ThenFunc(app.userSignup)) router.Handler(http.MethodPost, "/user/signup", dynamic.ThenFunc(app.userSignupPost)) - // router.Handler(http.MethodGet, "/user/login", dynamic.ThenFunc(app.userLogin)) - // router.Handler(http.MethodPost, "/user/login", dynamic.ThenFunc(app.userLoginPost)) + router.Handler(http.MethodGet, "/user/login", dynamic.ThenFunc(app.userLogin)) + router.Handler(http.MethodPost, "/user/login", dynamic.ThenFunc(app.userLoginPost)) standard := alice.New(app.recoverPanic, app.logRequest, secureHeaders) diff --git a/snippetbox/ui/html/pages/login.tmpl b/snippetbox/ui/html/pages/login.tmpl new file mode 100644 index 0000000..e9b319b --- /dev/null +++ b/snippetbox/ui/html/pages/login.tmpl @@ -0,0 +1,22 @@ +{{define "title"}}User Login{{end}} + +{{define "main"}} +
+
+ + {{with .Form.FieldErrors.username}} + + {{end}} + +
+
+ + {{with .Form.FieldErrors.password}} + + {{end}} + +
+
+ +
+{{end}}