learnifymedhub commited on
Commit
458780b
·
verified ·
1 Parent(s): d5a9df4

Update internal/auth/handler/auth_handler.go

Browse files
internal/auth/handler/auth_handler.go CHANGED
@@ -22,6 +22,16 @@ import (
22
 
23
  )
24
 
 
 
 
 
 
 
 
 
 
 
25
  func Login(w http.ResponseWriter, r *http.Request) {
26
  log.Println("Login handler triggered")
27
  state, _ := utils.SecureRandom(32)
@@ -37,7 +47,6 @@ func Login(w http.ResponseWriter, r *http.Request) {
37
  Path: "/",
38
  MaxAge: 300,
39
  })
40
- log.Println("Cookies set 1")
41
 
42
  http.SetCookie(w, &http.Cookie{
43
  Name: "pkce_verifier",
@@ -48,8 +57,6 @@ func Login(w http.ResponseWriter, r *http.Request) {
48
  Path: "/",
49
  MaxAge: 300,
50
  })
51
- log.Println("Cookies set 2")
52
-
53
  authURL := fmt.Sprintf("%s/realms/%s/protocol/openid-connect/auth?client_id=%s&response_type=code&scope=openid profile email&redirect_uri=%s&state=%s&code_challenge=%s&code_challenge_method=S256",
54
  os.Getenv("KEYCLOAK_URL"),
55
  os.Getenv("KEYCLOAK_REALM"),
@@ -59,9 +66,17 @@ func Login(w http.ResponseWriter, r *http.Request) {
59
  codeChallenge,
60
  )
61
 
62
- log.Println("redirection failed: ")
63
-
64
  http.Redirect(w, r, authURL, http.StatusTemporaryRedirect)
 
 
 
 
 
 
 
 
 
 
65
  }
66
 
67
  func Callback(w http.ResponseWriter, r *http.Request) {
 
22
 
23
  )
24
 
25
+ type LoggingResponseWriter struct {
26
+ http.ResponseWriter
27
+ StatusCode int
28
+ }
29
+
30
+ func (rw *LoggingResponseWriter) WriteHeader(statusCode int) {
31
+ rw.StatusCode = statusCode
32
+ rw.ResponseWriter.WriteHeader(statusCode)
33
+ }
34
+
35
  func Login(w http.ResponseWriter, r *http.Request) {
36
  log.Println("Login handler triggered")
37
  state, _ := utils.SecureRandom(32)
 
47
  Path: "/",
48
  MaxAge: 300,
49
  })
 
50
 
51
  http.SetCookie(w, &http.Cookie{
52
  Name: "pkce_verifier",
 
57
  Path: "/",
58
  MaxAge: 300,
59
  })
 
 
60
  authURL := fmt.Sprintf("%s/realms/%s/protocol/openid-connect/auth?client_id=%s&response_type=code&scope=openid profile email&redirect_uri=%s&state=%s&code_challenge=%s&code_challenge_method=S256",
61
  os.Getenv("KEYCLOAK_URL"),
62
  os.Getenv("KEYCLOAK_REALM"),
 
66
  codeChallenge,
67
  )
68
 
 
 
69
  http.Redirect(w, r, authURL, http.StatusTemporaryRedirect)
70
+
71
+ log.Printf("Redirecting to URL: %s", authURL)
72
+
73
+ // Perform the redirect
74
+ http.Redirect(loggingRW, r, authURL, http.StatusTemporaryRedirect)
75
+
76
+ // Check if redirect status code was 3xx (redirection)
77
+ if loggingRW.StatusCode != http.StatusTemporaryRedirect {
78
+ log.Printf("Redirection failed with status code: %d", loggingRW.StatusCode)
79
+ }
80
  }
81
 
82
  func Callback(w http.ResponseWriter, r *http.Request) {