Repository navigation
Expand file tree
/
Copy pathauth.go
More file actions
150 lines (127 loc) · 3.67 KB
/
Copy pathauth.go
File metadata and controls
150 lines (127 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package api
import (
"encoding/json"
"net/http"
"staticsend/pkg/auth"
"staticsend/pkg/database"
"staticsend/pkg/models"
)
// AuthHandler handles authentication requests
type AuthHandler struct {
DB *database.Database
SecretKey []byte
}
// LoginRequest represents a login request
type LoginRequest struct {
Email string `json:"email"`
Password string `json:"password"`
}
// RegisterRequest represents a registration request
type RegisterRequest struct {
Email string `json:"email"`
Password string `json:"password"`
}
// AuthResponse represents an authentication response
type AuthResponse struct {
Token string `json:"token"`
User *models.User `json:"user"`
}
// ErrorResponse represents an error response
type ErrorResponse struct {
Error string `json:"error"`
}
// Register handles user registration
func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
var req RegisterRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
// Validate input
if req.Email == "" || req.Password == "" {
http.Error(w, "Email and password are required", http.StatusBadRequest)
return
}
// Check if user already exists
exists, err := models.UserExists(h.DB.Connection, req.Email)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
if exists {
http.Error(w, "User already exists", http.StatusConflict)
return
}
// Hash password
passwordHash, err := auth.HashPassword(req.Password)
if err != nil {
http.Error(w, "Failed to process password", http.StatusInternalServerError)
return
}
// Create user
user, err := models.CreateUser(h.DB.Connection, req.Email, passwordHash)
if err != nil {
http.Error(w, "Failed to create user", http.StatusInternalServerError)
return
}
// Generate JWT token
token, err := auth.GenerateToken(user, h.SecretKey)
if err != nil {
http.Error(w, "Failed to generate token", http.StatusInternalServerError)
return
}
// Return response
response := AuthResponse{
Token: token,
User: user,
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(response)
}
// Login handles user authentication
func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
var req LoginRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
// Validate input
if req.Email == "" || req.Password == "" {
http.Error(w, "Email and password are required", http.StatusBadRequest)
return
}
// Get user by email
user, err := models.GetUserByEmail(h.DB.Connection, req.Email)
if err != nil {
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
if user == nil {
http.Error(w, "Invalid email or password", http.StatusUnauthorized)
return
}
// Check password
if err := auth.CheckPassword(req.Password, user.PasswordHash); err != nil {
http.Error(w, "Invalid email or password", http.StatusUnauthorized)
return
}
// Generate JWT token
token, err := auth.GenerateToken(user, h.SecretKey)
if err != nil {
http.Error(w, "Failed to generate token", http.StatusInternalServerError)
return
}
// Return response
response := AuthResponse{
Token: token,
User: user,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// HealthCheck handles health check requests
func (h *AuthHandler) HealthCheck(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}