138 lines
3.4 KiB
Go
138 lines
3.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"go-weather/middleware"
|
|
"go-weather/models"
|
|
"go-weather/services"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type UserHandler struct {
|
|
userService *services.UserService
|
|
authService *services.AuthService
|
|
}
|
|
|
|
func NewUserHandler(us *services.UserService, as *services.AuthService) *UserHandler {
|
|
return &UserHandler{userService: us, authService: as}
|
|
}
|
|
|
|
func (h *UserHandler) Me(w http.ResponseWriter, r *http.Request) {
|
|
claims := middleware.GetClaims(r)
|
|
if claims == nil {
|
|
http.Error(w, "unauthorized", http.StatusUnauthorized)
|
|
}
|
|
|
|
user, err := h.userService.GetUserByID(claims.UserID)
|
|
if err != nil {
|
|
http.Error(w, "user not found", http.StatusNotFound)
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]interface{}{"user": user})
|
|
}
|
|
|
|
func (h *UserHandler) Register(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var req models.RegisterRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "invalid request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
req.Email = strings.TrimSpace(strings.ToLower(req.Email))
|
|
if req.Email == "" || req.Password == "" {
|
|
http.Error(w, "email and password are required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if len(req.Password) < 8 {
|
|
http.Error(w, "password must be at least 8 characters", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
exists, err := h.userService.EmailExists(req.Email)
|
|
if err != nil {
|
|
http.Error(w, "internal server error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if exists {
|
|
http.Error(w, "email already registered", http.StatusConflict)
|
|
return
|
|
}
|
|
|
|
user, err := h.userService.CreateUser(req.Email, req.Password)
|
|
if err != nil {
|
|
http.Error(w, "Failed to create user", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusCreated)
|
|
json.NewEncoder(w).Encode(user)
|
|
}
|
|
|
|
func (h *UserHandler) Login(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodPost {
|
|
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
var req models.LoginRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, "invalid request body", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
req.Email = strings.ToLower(strings.TrimSpace(req.Email))
|
|
user, err := h.userService.GetUserByEmail(req.Email)
|
|
|
|
if err != nil || user == nil {
|
|
http.Error(w, "invalid credentials", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
if !h.userService.ValidatePassword(user, req.Password) {
|
|
http.Error(w, "invalid credentials", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
token, err := h.authService.GenerateToken(user.ID, user.Email)
|
|
if err != nil {
|
|
http.Error(w, "failed to generate token", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: "token",
|
|
Value: token,
|
|
Path: "/",
|
|
HttpOnly: true,
|
|
Secure: true, // HTTPS only
|
|
SameSite: http.SameSiteStrictMode,
|
|
MaxAge: 86400, // 24 hours
|
|
})
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(models.LoginResponse{User: *user})
|
|
|
|
}
|
|
|
|
func (h *UserHandler) Logout(w http.ResponseWriter, r *http.Request) {
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: "token",
|
|
Value: "",
|
|
Path: "/",
|
|
HttpOnly: true,
|
|
Secure: true,
|
|
MaxAge: -1,
|
|
})
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|