package main import ( "errors" "fmt" "net/http" ) func (app *Config) Authenticate(w http.ResponseWriter, r *http.Request) { var requestPayload struct { Email string `json:"email"` Password string `json:"password"` } err := app.readJSON(w, r, &requestPayload) if err != nil { app.errorJSON(w, err, http.StatusBadRequest) return } user, err := app.Models.User.GetByEmail(requestPayload.Email) if err != nil { // user not found app.errorJSON(w, errors.New("invalid credentials"), http.StatusBadRequest) return } valid, err := user.PasswordMatches(requestPayload.Password) if err != nil || !valid { app.errorJSON(w, errors.New("invalid credentials"), http.StatusBadRequest) return } payload := jsonResponse{ Error: false, Message: fmt.Sprintf("%s %s is authorized to log in.", user.FirstName, user.LastName), Data: user, } app.writeJSON(w, http.StatusAccepted, payload) }