Kunal commited on
Commit
a03c760
·
1 Parent(s): dd5aacd

update auth.py for deployment

Browse files
Files changed (1) hide show
  1. src/auth.py +15 -4
src/auth.py CHANGED
@@ -20,12 +20,21 @@ PORT = int(os.getenv("MAL_PORT", 8000))
20
 
21
  class MALAuth:
22
  def __init__(self):
23
- self.code_verifier = secrets.token_urlsafe(64)
24
- self.code_challenge = self.code_verifier # Using 'plain' method
 
25
  self.auth_code = None
26
  self.error = None
27
 
28
  def start_oauth_flow(self):
 
 
 
 
 
 
 
 
29
  auth_url = (
30
  "https://myanimelist.net/v1/oauth2/authorize?"
31
  f"response_type=code&"
@@ -33,7 +42,9 @@ class MALAuth:
33
  f"code_challenge={self.code_challenge}&"
34
  f"redirect_uri={REDIRECT_URI}"
35
  )
36
- st.markdown(f"[Click here to authenticate with MyAnimeList]({auth_url})", unsafe_allow_html=True)
 
 
37
 
38
  # Streamlit Community Cloud: handle redirect via query params
39
  query_params = st.query_params
@@ -94,7 +105,7 @@ class MALAuth:
94
  "client_id": CLIENT_ID,
95
  "client_secret": CLIENT_SECRET,
96
  "code": auth_code,
97
- "code_verifier": self.code_verifier,
98
  "grant_type": "authorization_code",
99
  "redirect_uri": REDIRECT_URI
100
  }
 
20
 
21
  class MALAuth:
22
  def __init__(self):
23
+ # code_verifier and code_challenge will be set in start_oauth_flow
24
+ self.code_verifier = None
25
+ self.code_challenge = None
26
  self.auth_code = None
27
  self.error = None
28
 
29
  def start_oauth_flow(self):
30
+ # Only generate new code_verifier if not already in session_state
31
+ if "code_verifier" not in st.session_state:
32
+ st.session_state.code_verifier = secrets.token_urlsafe(64)
33
+ st.session_state.code_challenge = st.session_state.code_verifier # 'plain' method
34
+
35
+ self.code_verifier = st.session_state.code_verifier
36
+ self.code_challenge = st.session_state.code_challenge
37
+
38
  auth_url = (
39
  "https://myanimelist.net/v1/oauth2/authorize?"
40
  f"response_type=code&"
 
42
  f"code_challenge={self.code_challenge}&"
43
  f"redirect_uri={REDIRECT_URI}"
44
  )
45
+ # Use a button to open the auth link in the same tab
46
+ st.write('') # spacing
47
+ st.link_button("Click here to authenticate with MyAnimeList", auth_url)
48
 
49
  # Streamlit Community Cloud: handle redirect via query params
50
  query_params = st.query_params
 
105
  "client_id": CLIENT_ID,
106
  "client_secret": CLIENT_SECRET,
107
  "code": auth_code,
108
+ "code_verifier": self.code_verifier if self.code_verifier else st.session_state.get("code_verifier"),
109
  "grant_type": "authorization_code",
110
  "redirect_uri": REDIRECT_URI
111
  }