11"use client"
22
33import { Suspense , useEffect , useState } from "react"
4- import { useSearchParams } from "next/navigation"
4+ import { useRouter , useSearchParams } from "next/navigation"
55import { useAuth } from "@/providers/auth-provider"
66import { useTurnkey } from "@turnkey/sdk-react"
77import { Loader } from "lucide-react"
@@ -14,6 +14,7 @@ function OAuthProcessCallback() {
1414
1515 const { authIframeClient } = useTurnkey ( )
1616 const { loginWithApple } = useAuth ( )
17+ const router = useRouter ( )
1718
1819 const [ storedToken , setStoredToken ] = useState < string | null > ( null ) // Store the token locally
1920 const [ hasLoggedIn , setHasLoggedIn ] = useState ( false ) // Track if loginWithOAuth has been called
@@ -33,11 +34,16 @@ function OAuthProcessCallback() {
3334 // Trigger loginWithOAuth when both token and iframePublicKey are available, but only once
3435 useEffect ( ( ) => {
3536 if ( storedToken && authIframeClient ?. iframePublicKey && ! hasLoggedIn ) {
36- // Call the OAuth login function with the stored token
37- loginWithApple ( storedToken )
37+ try {
38+ // Call the OAuth login function with the stored token
39+ loginWithApple ( storedToken )
3840
39- // Set flag to prevent further calls
40- setHasLoggedIn ( true )
41+ // Set flag to prevent further calls
42+ setHasLoggedIn ( true )
43+ } catch ( error ) {
44+ const msg = "Error logging in with Apple: " + error
45+ router . push ( `/?error=${ encodeURIComponent ( msg ) } ` )
46+ }
4147 }
4248 } , [
4349 storedToken ,
0 commit comments