ch11.4
This commit is contained in:
@@ -26,6 +26,12 @@ type userSignupForm struct {
|
|||||||
validator.Validator `form:"-"`
|
validator.Validator `form:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type userLoginForm struct {
|
||||||
|
Email string `form:"email"`
|
||||||
|
Password string `form:"password"`
|
||||||
|
validator.Validator `form:"-"`
|
||||||
|
}
|
||||||
|
|
||||||
func (app *application) render(w http.ResponseWriter, status int, page string, data *templateData) {
|
func (app *application) render(w http.ResponseWriter, status int, page string, data *templateData) {
|
||||||
ts, ok := app.templateCache[page]
|
ts, ok := app.templateCache[page]
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -164,7 +170,47 @@ func (app *application) userSignupPost(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (app *application) userLoginPost(w http.ResponseWriter, r *http.Request) {
|
func (app *application) userLoginPost(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintln(w, "Login the user...")
|
var form userLoginForm
|
||||||
|
|
||||||
|
err := app.decodePostForm(r, &form)
|
||||||
|
if err != nil {
|
||||||
|
app.clientError(w, http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
form.CheckField(validator.NotBlank(form.Email), "email", "This field cannot be blank")
|
||||||
|
form.CheckField(validator.Matches(form.Email, validator.EmailRX), "email", "This field must be a valid email address")
|
||||||
|
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, http.StatusUnprocessableEntity, "login.tmpl", data)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id, err := app.users.Authenticate(form.Email, form.Password)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, models.ErrInvalidCredentials) {
|
||||||
|
form.AddNonFieldError("Email or password is incorrect")
|
||||||
|
|
||||||
|
data := app.newTemplateData(r)
|
||||||
|
data.Form = form
|
||||||
|
app.render(w, http.StatusUnprocessableEntity, "login.tmpl", data)
|
||||||
|
} else {
|
||||||
|
app.serverError(w, err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = app.sessionManager.RenewToken(r.Context())
|
||||||
|
if err != nil {
|
||||||
|
app.serverError(w, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
app.sessionManager.Put(r.Context(), "authenticatedUserID", id)
|
||||||
|
|
||||||
|
http.Redirect(w, r, "/snippet/create", http.StatusSeeOther)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *application) userLogoutPost(w http.ResponseWriter, r *http.Request) {
|
func (app *application) userLogoutPost(w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -178,5 +224,7 @@ func (app *application) userSignup(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (app *application) userLogin(w http.ResponseWriter, r *http.Request) {
|
func (app *application) userLogin(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintln(w, "Display a HTML for log in a user...")
|
data := app.newTemplateData(r)
|
||||||
|
data.Form = userLoginForm{}
|
||||||
|
app.render(w, http.StatusOK, "login.tmpl", data)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,27 @@ func (m *UserModel) Insert(name, email, password string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *UserModel) Authenticate(email, password string) (int, error) {
|
func (m *UserModel) Authenticate(email, password string) (int, error) {
|
||||||
return 0, nil
|
var id int
|
||||||
|
var hashedPassword []byte
|
||||||
|
|
||||||
|
stmt := `SELECT id, hashed_password FROM users WHERE email = ?`
|
||||||
|
|
||||||
|
err := m.DB.QueryRow(stmt, email).Scan(&id, &hashedPassword)
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
|
return 0, ErrInvalidCredentials
|
||||||
|
}
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
err = bcrypt.CompareHashAndPassword(hashedPassword, []byte(password))
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
|
||||||
|
return 0, ErrInvalidCredentials
|
||||||
|
}
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return id, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *UserModel) Exists(id int) (bool, error) {
|
func (m *UserModel) Exists(id int) (bool, error) {
|
||||||
|
|||||||
@@ -7,11 +7,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Validator struct {
|
type Validator struct {
|
||||||
FieldErrors map[string]string
|
NonFieldErrors []string
|
||||||
|
FieldErrors map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Validator) Valid() bool {
|
func (v *Validator) Valid() bool {
|
||||||
return len(v.FieldErrors) == 0
|
return len(v.FieldErrors) == 0 && len(v.NonFieldErrors) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *Validator) AddFieldError(key, message string) {
|
func (v *Validator) AddFieldError(key, message string) {
|
||||||
@@ -23,6 +24,10 @@ func (v *Validator) AddFieldError(key, message string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v *Validator) AddNonFieldError(message string) {
|
||||||
|
v.NonFieldErrors = append(v.NonFieldErrors, message)
|
||||||
|
}
|
||||||
|
|
||||||
func (v *Validator) CheckField(ok bool, key, message string) {
|
func (v *Validator) CheckField(ok bool, key, message string) {
|
||||||
if !ok {
|
if !ok {
|
||||||
v.AddFieldError(key, message)
|
v.AddFieldError(key, message)
|
||||||
|
|||||||
26
ui/html/pages/login.tmpl
Normal file
26
ui/html/pages/login.tmpl
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
{{define "title"}}Login{{end}}
|
||||||
|
|
||||||
|
{{define "main"}}
|
||||||
|
<form action='/user/login' method='POST' novalidate>
|
||||||
|
{{range .Form.NonFieldErrors}}
|
||||||
|
<div class='error'>{{.}}</div>
|
||||||
|
{{end}}
|
||||||
|
<div>
|
||||||
|
<label>Email:</label>
|
||||||
|
{{with .Form.FieldErrors.email}}
|
||||||
|
<label class='error'>{{.}}</label>
|
||||||
|
{{end}}
|
||||||
|
<input type='email' name='email' value='{{.Form.Email}}'>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label>Password:</label>
|
||||||
|
{{with .Form.FieldErrors.password}}
|
||||||
|
<label class='error'>{{.}}</label>
|
||||||
|
{{end}}
|
||||||
|
<input type='password' name='password'>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<input type='submit' value='Login'>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
{{end}}
|
||||||
Reference in New Issue
Block a user