Add JWT authentication

- Create register and login use cases
- Implement JWT service for token generation and validation
- Add authentication middleware to protect habit endpoints
- Create auth HTTP handlers (register, login)
- Update habit handlers to extract userID from JWT token
- Register/login endpoints: POST /auth/register, POST /auth/login
- Habit endpoints now require Bearer token in Authorization header
- Tested: register -> create habit -> list habits works correctly
This commit is contained in:
2025-11-26 01:05:08 +01:00
parent 3e8883d878
commit b2894fca70
8 changed files with 375 additions and 4 deletions
+11 -2
View File
@@ -37,7 +37,11 @@ func (h *HabitHandlers) CreateHabit(w http.ResponseWriter, r *http.Request) {
return
}
userID := "user-123"
userID, ok := GetUserIDFromContext(r.Context())
if !ok {
respondError(w, http.StatusUnauthorized, "User not authenticated")
return
}
cmd := commands.CreateHabitCommand{
UserID: userID,
@@ -65,7 +69,12 @@ func (h *HabitHandlers) CreateHabit(w http.ResponseWriter, r *http.Request) {
}
func (h *HabitHandlers) GetTodaysHabits(w http.ResponseWriter, r *http.Request) {
userID := "user-123"
userID, ok := GetUserIDFromContext(r.Context())
if !ok {
respondError(w, http.StatusUnauthorized, "User not authenticated")
return
}
timezone := "UTC"
query := queries.GetTodaysHabitsQuery{