Skip to content

Commit e679591

Browse files
committed
fix: skip auth check for options method
1 parent adb9f5b commit e679591

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

apps/question-service/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
FIREBASE_CREDENTIAL_PATH=cs3219-g24-firebase-adminsdk-9cm7h-b1675603ab.json
2+
3+
# Secret for creating JWT signature
4+
JWT_SECRET=you-can-replace-this-with-your-own-secret

apps/question-service/middleware/basic-access-control.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
package middleware
22

33
import (
4-
"github.com/golang-jwt/jwt/v4"
4+
"log"
55
"net/http"
66
"os"
77
"strings"
8+
9+
"github.com/golang-jwt/jwt/v4"
810
)
911

1012
func VerifyJWT(next http.Handler) http.Handler {
1113
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
14+
// Skip JWT Verification for OPTIONS Requests
15+
if r.Method == "OPTIONS" {
16+
next.ServeHTTP(w, r)
17+
return
18+
}
19+
1220
// Get the token from the Authorization header
1321
authHeader := r.Header.Get("Authorization")
1422
if authHeader == "" {
@@ -17,7 +25,12 @@ func VerifyJWT(next http.Handler) http.Handler {
1725
}
1826

1927
// Split the header to get the token
20-
tokenString := strings.Split(authHeader, " ")[1]
28+
parts := strings.Split(authHeader, " ")
29+
if len(parts) != 2 || parts[0] != "Bearer" {
30+
http.Error(w, "Invalid authorization header format", http.StatusUnauthorized)
31+
return
32+
}
33+
tokenString := parts[1]
2134

2235
// Retrieve the JWT secret from environment variables
2336
jwtSecret := []byte(os.Getenv("JWT_SECRET"))
@@ -36,6 +49,7 @@ func VerifyJWT(next http.Handler) http.Handler {
3649

3750
if err != nil || !token.Valid {
3851
http.Error(w, "Invalid token", http.StatusUnauthorized)
52+
log.Printf("Token parse error: %v", err)
3953
return
4054
}
4155

0 commit comments

Comments
 (0)