add auth to backend and frontend

This commit is contained in:
2026-02-24 10:42:36 -05:00
parent 11eb192e2a
commit 0d5cc4d21f
14 changed files with 336 additions and 35 deletions
+3 -5
View File
@@ -4,7 +4,6 @@ import (
"context"
"go-weather/services"
"net/http"
"strings"
)
type contextKey string
@@ -14,14 +13,13 @@ const ClaimsKey contextKey = "claims"
func AuthMiddleware(authService *services.AuthService) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
cookie, err := r.Cookie("token")
if err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
claims, err := authService.ValidateToken(tokenString)
claims, err := authService.ValidateToken(cookie.Value)
if err != nil {
http.Error(w, "invalid token", http.StatusUnauthorized)
return