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`?
This commit is contained in:
tamsin johnson 2024-02-07 11:22:47 -08:00
parent 4f2dc018da
commit c1bb129b87
3 changed files with 65 additions and 3 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -0,0 +1,22 @@
{{define "title"}}User Login{{end}}
{{define "main"}}
<form action='/user/login' method='POST'>
<div>
<label>Username:</label>
{{with .Form.FieldErrors.username}}
<label class='error'>{{.}}</label>
{{end}}
<input type='text' name='username' value='{{.Form.Username}}'>
</div>
<div>
<label>Password:</label>
{{with .Form.FieldErrors.password}}
<label class='error'>{{.}}</label>
{{end}}
<input type='text' name='password' value='{{.Form.Password}}'>
</div>
<div>
<input type='submit' value='Login'>
</div>
{{end}}