golang-template/internal/handlers/signup.go
2026-08-15 23:57:52 +03:00

42 lines
887 B
Go

package handlers
import (
"encoding/json"
"net/http"
)
type SignUpRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
func (h *Handler) SignUpHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
var req SignUpRequest
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&req)
if err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
if req.Username == "" || req.Password == "" {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
user, err := h.users.FindByUsername(req.Username)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
}
if user != nil {
http.Error(w, "Username already taken", http.StatusBadRequest)
}
}