|
1 | | -import { useCallback, useEffect, useState } from "react"; |
| 1 | +import { useCallback, useEffect, useRef, useState } from "react"; |
2 | 2 | import { router, Slot, useGlobalSearchParams, usePathname } from "expo-router"; |
3 | 3 | import * as SplashScreen from "expo-splash-screen"; |
| 4 | +import AsyncStorage from "@react-native-async-storage/async-storage"; |
4 | 5 |
|
5 | 6 | import { McpContextProvider } from "@/client"; |
6 | 7 | import { useAlerts } from "@/components/Alerts"; |
7 | 8 | import { SettingsProvider } from "@/hooks/useSettings"; |
8 | 9 |
|
| 10 | +const REDIRECT_COUNT_KEY = "oauth_redirect_count"; |
| 11 | +const MAX_REDIRECTS = 3; |
| 12 | + |
| 13 | +const getRedirectCount = async (): Promise<number> => { |
| 14 | + const count = await AsyncStorage.getItem(REDIRECT_COUNT_KEY); |
| 15 | + return count ? parseInt(count, 10) : 0; |
| 16 | +}; |
| 17 | + |
| 18 | +const incrementRedirectCount = async (): Promise<number> => { |
| 19 | + const newCount = (await getRedirectCount()) + 1; |
| 20 | + await AsyncStorage.setItem(REDIRECT_COUNT_KEY, newCount.toString()); |
| 21 | + return newCount; |
| 22 | +}; |
| 23 | + |
| 24 | +const resetRedirectCount = async (): Promise<void> => { |
| 25 | + await AsyncStorage.removeItem(REDIRECT_COUNT_KEY); |
| 26 | +}; |
| 27 | + |
9 | 28 | export default function ProtectedLayout() { |
10 | 29 | const params = useGlobalSearchParams<{ code?: string; error?: string }>(); |
11 | 30 | const { showAlert } = useAlerts(); |
12 | 31 |
|
13 | 32 | const [idleMcp, setIdleMcp] = useState(false); |
14 | 33 | const [idleSettings, setIdleSettings] = useState(false); |
| 34 | + const isHandlingError = useRef(false); |
15 | 35 |
|
16 | 36 | const mcpCallback = useCallback( |
17 | 37 | (error?: Error) => { |
18 | 38 | setIdleMcp(true); |
| 39 | + router.setParams({ code: undefined }); // Always clear code param |
19 | 40 |
|
20 | | - if (!error) router.setParams({ code: undefined }); |
21 | | - else |
22 | | - showAlert({ |
23 | | - type: "error", |
24 | | - title: "MCP Error", |
25 | | - message: error.message, |
26 | | - timeout: 5000, |
27 | | - }); |
| 41 | + if (!error) { |
| 42 | + // Success - reset redirect count |
| 43 | + resetRedirectCount(); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + // Prevent concurrent error handling |
| 48 | + if (isHandlingError.current) return; |
| 49 | + isHandlingError.current = true; |
| 50 | + |
| 51 | + // Check if this is an auth-related error requiring re-authentication |
| 52 | + const errorMessage = error.message.toLowerCase(); |
| 53 | + const isAuthError = |
| 54 | + errorMessage.includes("invalid_grant") || |
| 55 | + errorMessage.includes("invalid grant") || |
| 56 | + errorMessage.includes("invalid token") || |
| 57 | + errorMessage.includes("authorization code") || |
| 58 | + errorMessage.includes("oauth client") || |
| 59 | + errorMessage.includes("unauthorized"); |
| 60 | + |
| 61 | + if (isAuthError) { |
| 62 | + // Auto-redirect with loop protection |
| 63 | + (async () => { |
| 64 | + try { |
| 65 | + const redirectCount = await incrementRedirectCount(); |
| 66 | + if (redirectCount > MAX_REDIRECTS) { |
| 67 | + await resetRedirectCount(); |
| 68 | + showAlert({ |
| 69 | + type: "error", |
| 70 | + title: "Authentication Failed", |
| 71 | + message: |
| 72 | + "Unable to authenticate. Please try again later or contact support.", |
| 73 | + timeout: 10000, |
| 74 | + }); |
| 75 | + isHandlingError.current = false; |
| 76 | + return; |
| 77 | + } |
| 78 | + router.replace("/authorize"); |
| 79 | + } catch { |
| 80 | + isHandlingError.current = false; |
| 81 | + } |
| 82 | + })(); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // For other errors, show alert |
| 87 | + showAlert({ |
| 88 | + type: "error", |
| 89 | + title: "Connection Error", |
| 90 | + message: error.message, |
| 91 | + timeout: 5000, |
| 92 | + }); |
| 93 | + isHandlingError.current = false; |
28 | 94 | }, |
29 | 95 | [showAlert], |
30 | 96 | ); |
|
0 commit comments