Mark-Lasfar commited on
Commit
6905ba2
·
1 Parent(s): 563cdf9

Update backend and server frontend for OAuth JSON response, client-side navigation, and add .gitignore

Browse files
Files changed (2) hide show
  1. api/auth.py +2 -17
  2. static/js/chat.js +9 -10
api/auth.py CHANGED
@@ -227,7 +227,8 @@ async def custom_oauth_callback(
227
 
228
  token = await generate_jwt_token(user, SECRET, 3600)
229
 
230
- # response.set_cookie(
 
231
  # key="fastapiusersauth",
232
  # value=token,
233
  # max_age=3600,
@@ -235,22 +236,6 @@ async def custom_oauth_callback(
235
  # samesite="lax",
236
  # secure=True,
237
  # )
238
-
239
- is_app = request.headers.get("X-Capacitor-App", False)
240
- if is_app:
241
- return JSONResponse(content={
242
- "message": "Google login successful",
243
- "access_token": token
244
- }, status_code=200)
245
- else:
246
- # إرجاع الـ token في الـ Authorization header
247
- response.headers["Authorization"] = f"Bearer {token}"
248
- return RedirectResponse(url="/chat", status_code=303)
249
-
250
- except Exception as e:
251
- logger.error(f"Error in Google OAuth callback: {str(e)}")
252
- return JSONResponse(content={"detail": str(e)}, status_code=400)
253
-
254
 
255
  is_app = request.headers.get("X-Capacitor-App", False)
256
  if is_app:
 
227
 
228
  token = await generate_jwt_token(user, SECRET, 3600)
229
 
230
+ # ما نضبطش cookie لأننا بنستخدم Bearer token
231
+ # response.set_cookie(
232
  # key="fastapiusersauth",
233
  # value=token,
234
  # max_age=3600,
 
236
  # samesite="lax",
237
  # secure=True,
238
  # )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
239
 
240
  is_app = request.headers.get("X-Capacitor-App", False)
241
  if is_app:
static/js/chat.js CHANGED
@@ -54,17 +54,15 @@ let abortController = null;
54
 
55
 
56
  async function checkAuth() {
57
- let token = localStorage.getItem('token');
58
- if (!token) {
59
- // حاول جيب الـ token من الـ response headers
60
- const response = await fetch('/chat', { method: 'GET' });
61
- token = response.headers.get('Authorization')?.replace('Bearer ', '');
62
- if (token) {
63
- console.log('Access token found in response headers, saving to localStorage');
64
- localStorage.setItem('token', token);
65
- }
66
  }
67
 
 
68
  if (!token && typeof Cookies !== 'undefined') {
69
  token = Cookies.get('fastapiusersauth');
70
  if (token) {
@@ -74,7 +72,7 @@ async function checkAuth() {
74
  }
75
 
76
  if (!token) {
77
- console.log('No auth token found in localStorage, headers, or cookie');
78
  return { authenticated: false, user: null };
79
  }
80
 
@@ -108,6 +106,7 @@ async function checkAuth() {
108
  }
109
  }
110
 
 
111
  async function handleSession() {
112
  const sessionId = sessionStorage.getItem('session_id');
113
  if (!sessionId) {
 
54
 
55
 
56
  async function checkAuth() {
57
+ const urlParams = new URLSearchParams(window.location.search);
58
+ const accessTokenFromUrl = urlParams.get('access_token');
59
+ if (accessTokenFromUrl) {
60
+ console.log('Access token found in URL, saving to localStorage');
61
+ localStorage.setItem('token', accessTokenFromUrl);
62
+ window.history.replaceState({}, document.title, '/chat');
 
 
 
63
  }
64
 
65
+ let token = localStorage.getItem('token');
66
  if (!token && typeof Cookies !== 'undefined') {
67
  token = Cookies.get('fastapiusersauth');
68
  if (token) {
 
72
  }
73
 
74
  if (!token) {
75
+ console.log('No auth token found in localStorage or cookie');
76
  return { authenticated: false, user: null };
77
  }
78
 
 
106
  }
107
  }
108
 
109
+
110
  async function handleSession() {
111
  const sessionId = sessionStorage.getItem('session_id');
112
  if (!sessionId) {