Spaces:
Running

axeronis commited on
Commit
2cd68f8
·
1 Parent(s): e67f01f

fix: pass auth token in OAuth redirect URL

Browse files
backend/app/api/oauth.py CHANGED
@@ -42,12 +42,13 @@ class OAuthInitResponse(BaseModel):
42
  @router.get("/meta/connect")
43
  async def initiate_meta_oauth(
44
  channel: str = Query(..., description="Channel: instagram or whatsapp"),
45
- current_user: User = Depends(get_current_user)
 
46
  ):
47
  """
48
  Step 1: Initiate OAuth flow for Instagram/WhatsApp
49
 
50
- Generates authorization URL and redirects user to Meta OAuth dialog
51
  """
52
  if not META_APP_ID:
53
  raise HTTPException(
@@ -58,8 +59,22 @@ async def initiate_meta_oauth(
58
  if channel not in ["instagram", "whatsapp"]:
59
  raise HTTPException(status_code=400, detail="Invalid channel")
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  # Generate state to prevent CSRF (contains user_id and channel)
62
- state = f"{current_user.id}:{channel}"
63
 
64
  # Required permissions
65
  if channel == "instagram":
@@ -79,7 +94,7 @@ async def initiate_meta_oauth(
79
 
80
  logger.info(
81
  "OAuth flow initiated",
82
- user_id=str(current_user.id),
83
  channel=channel
84
  )
85
 
 
42
  @router.get("/meta/connect")
43
  async def initiate_meta_oauth(
44
  channel: str = Query(..., description="Channel: instagram or whatsapp"),
45
+ token: Optional[str] = Query(None, description="Bearer token for auth"),
46
+ session: AsyncSession = Depends(get_async_session)
47
  ):
48
  """
49
  Step 1: Initiate OAuth flow for Instagram/WhatsApp
50
 
51
+ Accepts token via query param since this is accessed via browser redirect
52
  """
53
  if not META_APP_ID:
54
  raise HTTPException(
 
59
  if channel not in ["instagram", "whatsapp"]:
60
  raise HTTPException(status_code=400, detail="Invalid channel")
61
 
62
+ # Verify token and get user
63
+ if not token:
64
+ raise HTTPException(status_code=401, detail="Token required")
65
+
66
+ from app.core.security import decode_access_token
67
+ try:
68
+ payload = decode_access_token(token)
69
+ user_id = payload.get("sub")
70
+ if not user_id:
71
+ raise HTTPException(status_code=401, detail="Invalid token")
72
+ except Exception as e:
73
+ logger.error(f"Token verification failed: {e}")
74
+ raise HTTPException(status_code=401, detail="Invalid token")
75
+
76
  # Generate state to prevent CSRF (contains user_id and channel)
77
+ state = f"{user_id}:{channel}"
78
 
79
  # Required permissions
80
  if channel == "instagram":
 
94
 
95
  logger.info(
96
  "OAuth flow initiated",
97
+ user_id=user_id,
98
  channel=channel
99
  )
100
 
frontend/app/channels/page.tsx CHANGED
@@ -332,9 +332,14 @@ function WhatsAppConnectModal({ onClose, onSuccess }: { onClose: () => void, onS
332
  })
333
 
334
  const handleOAuthConnect = () => {
335
- // Redirect to backend OAuth endpoint
336
  const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'https://api.microfunnel.online'
337
- window.location.href = `${apiUrl}/api/v1/oauth/meta/connect?channel=whatsapp`
 
 
 
 
 
338
  }
339
 
340
  const handleSubmit = () => {
 
332
  })
333
 
334
  const handleOAuthConnect = () => {
335
+ // Redirect to backend OAuth endpoint with auth token
336
  const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'https://api.microfunnel.online'
337
+ const token = localStorage.getItem('access_token')
338
+ if (!token) {
339
+ toast.error('Ошибка', 'Требуется авторизация')
340
+ return
341
+ }
342
+ window.location.href = `${apiUrl}/api/v1/oauth/meta/connect?channel=whatsapp&token=${encodeURIComponent(token)}`
343
  }
344
 
345
  const handleSubmit = () => {