Skip to content

Instantly share code, notes, and snippets.

@Maksclub
Created September 1, 2022 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maksclub/33ac87210fba30568500218438c854ff to your computer and use it in GitHub Desktop.
Save Maksclub/33ac87210fba30568500218438c854ff to your computer and use it in GitHub Desktop.
type LoginCreds struct {
Password string `json:"password"`
Email string `json:"email"`
}
func (h *Handler) UserLogin(w http.ResponseWriter, req *http.Request) {
input := LoginCreds{}
dec := json.NewDecoder(req.Body)
if err := dec.Decode(&input); err != nil {
http.Error(w, "email and password not satisfied", http.StatusBadRequest)
return
}
hashedPass := hasher.HashAndSalt(input.Password)
u := h.users.GetByInputCredentials(input.Email, hashedPass)
if u == nil {
w.WriteHeader(http.StatusUnauthorized)
http.Error(w, "user not found", http.StatusUnauthorized)
return
}
if !hasher.EqualPasswords(hashedPass, u.PasswordHash()) {
http.Error(w, "password bad", http.StatusUnauthorized)
return
}
if !u.Activated() {
http.Error(w, "user not activated", http.StatusUnauthorized)
return
}
h.writePairTokenResponse(w, u)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment