@@ -13,20 +13,22 @@ import { tokenCache } from '@clerk/expo/token-cache';
1313import { type ReactElement , useCallback , useEffect , useRef , useState , useSyncExternalStore } from 'react' ;
1414import { ActivityIndicator , StyleSheet , Text , View } from 'react-native' ;
1515
16- import { AuthLogo } from '../screens/AuthLogo' ;
1716import { CustomSignIn } from '../screens/CustomSignIn' ;
1817import { CustomSignUp } from '../screens/CustomSignUp' ;
18+ import { type Destination , homeLinksFor } from '../screens/destinations' ;
1919import { EmbeddedProfile } from '../screens/EmbeddedProfile' ;
2020import { Home } from '../screens/Home' ;
2121import { NativeModules } from '../screens/NativeModules' ;
22- import { TokenCache } from '../screens/TokenCache' ;
23- import { publishableKeyFailure , type VerifyFailure , type VerifyLaunch , type VerifyScreen } from './launch' ;
22+ import { readClientTokenKept , TokenCache } from '../screens/TokenCache' ;
23+ import { publishableKeyFailure , type VerifyFailure , type VerifyLaunch } from './launch' ;
2424import { type VerifyState , verifyStateJson , type VerifyTicket } from './state' ;
2525
2626type HostProps = { launch : VerifyLaunch } ;
27+ type RoutedHostProps = HostProps & { clientTokenKeptAtLaunch : Promise < boolean > } ;
2728
2829export function VerifyHost ( { launch } : HostProps ) : ReactElement {
29- const failure = publishableKeyFailure ( launch . publishableKey ) ?? launch . screenFailure ;
30+ const [ clientTokenKeptAtLaunch ] = useState ( readClientTokenKept ) ;
31+ const failure = publishableKeyFailure ( launch . publishableKey ) ;
3032 if ( failure ) {
3133 return (
3234 < ConfigurationFailure
@@ -41,7 +43,10 @@ export function VerifyHost({ launch }: HostProps): ReactElement {
4143 publishableKey = { launch . publishableKey }
4244 tokenCache = { tokenCache }
4345 >
44- < RoutedHost launch = { launch } />
46+ < RoutedHost
47+ launch = { launch }
48+ clientTokenKeptAtLaunch = { clientTokenKeptAtLaunch }
49+ />
4550 </ ClerkProvider >
4651 ) ;
4752}
@@ -91,15 +96,16 @@ function useVerifyLog(state: VerifyState): void {
9196 } , [ json ] ) ;
9297}
9398
94- function RoutedHost ( { launch } : HostProps ) : ReactElement {
99+ function RoutedHost ( { launch, clientTokenKeptAtLaunch } : RoutedHostProps ) : ReactElement {
95100 const status = useClerkStatus ( ) ;
96101 const { isLoaded, isSignedIn, orgId } = useAuth ( ) ;
97102 const { user } = useUser ( ) ;
98103 const { session } = useSession ( ) ;
99104 const { signIn } = useSignIn ( ) ;
100105 const { signUp } = useSignUp ( ) ;
101106 const authView = useAuthViewState ( ) ;
102- const [ screenClosed , setScreenClosed ] = useState ( false ) ;
107+ const [ opened , setOpened ] = useState < Destination | null > ( null ) ;
108+ const goHome = useCallback ( ( ) => setOpened ( null ) , [ ] ) ;
103109 const [ ticket , setTicket ] = useState < VerifyTicket > ( launch . signInTicket ? 'pending' : 'none' ) ;
104110 const [ failure , setFailure ] = useState < VerifyFailure | null > ( null ) ;
105111 const ticketStarted = useRef ( false ) ;
@@ -139,30 +145,26 @@ function RoutedHost({ launch }: HostProps): ReactElement {
139145 ) ;
140146 } , [ isLoaded , launch . signInTicket , signIn ] ) ;
141147
142- const screen = ( ( ) : VerifyScreen | 'loading' => {
143- if ( launch . screen === 'tokenCache' ) {
144- return launch . screen ;
148+ const showsAuthView = opened === 'auth' || opened === 'nativeAuth' ;
149+ const finished = showsAuthView
150+ ? authView . isAuthFlowComplete
151+ : ! homeLinksFor ( Boolean ( isSignedIn ) ) . some ( link => link . opens === opened ) ;
152+ const left = opened !== null && finished ;
153+
154+ useEffect ( ( ) => {
155+ if ( left ) {
156+ goHome ( ) ;
145157 }
158+ } , [ goHome , left ] ) ;
159+
160+ const screen = ( ( ) : Destination | 'home' | 'loading' => {
146161 if ( ! isLoaded || ticket === 'pending' ) {
147162 return 'loading' ;
148163 }
149- switch ( launch . screen ) {
150- case 'auth' :
151- case 'nativeAuth' :
152- if ( authView . isAuthFlowComplete || screenClosed ) {
153- return 'home' ;
154- }
155- return authView . isLoaded ? launch . screen : 'loading' ;
156- case 'customSignIn' :
157- case 'customSignUp' :
158- return isSignedIn ? 'home' : launch . screen ;
159- case 'embeddedProfile' :
160- return isSignedIn && ! screenClosed ? launch . screen : 'home' ;
161- case 'home' :
162- case 'authLogo' :
163- case 'nativeModules' :
164- return launch . screen ;
164+ if ( opened === null || left ) {
165+ return 'home' ;
165166 }
167+ return showsAuthView && ! authView . isLoaded ? 'loading' : opened ;
166168 } ) ( ) ;
167169
168170 useVerifyLog ( {
@@ -192,7 +194,12 @@ function RoutedHost({ launch }: HostProps): ReactElement {
192194 case 'loading' :
193195 return < ActivityIndicator style = { styles . fill } /> ;
194196 case 'home' :
195- return < Home authMode = { launch . authMode } /> ;
197+ return (
198+ < Home
199+ authMode = { launch . authMode }
200+ onOpen = { setOpened }
201+ />
202+ ) ;
196203 case 'auth' :
197204 return (
198205 < AuthView
@@ -204,19 +211,17 @@ function RoutedHost({ launch }: HostProps): ReactElement {
204211 return (
205212 < AuthView
206213 mode = { launch . authMode }
207- onDismiss = { ( ) => setScreenClosed ( true ) }
214+ onDismiss = { goHome }
208215 />
209216 ) ;
210217 case 'customSignIn' :
211218 return < CustomSignIn /> ;
212219 case 'customSignUp' :
213220 return < CustomSignUp /> ;
214221 case 'tokenCache' :
215- return < TokenCache /> ;
216- case 'authLogo' :
217- return < AuthLogo authMode = { launch . authMode } /> ;
222+ return < TokenCache keptAtLaunch = { clientTokenKeptAtLaunch } /> ;
218223 case 'embeddedProfile' :
219- return < EmbeddedProfile onHostBack = { ( ) => setScreenClosed ( true ) } /> ;
224+ return < EmbeddedProfile onHostBack = { goHome } /> ;
220225 case 'nativeModules' :
221226 return < NativeModules /> ;
222227 }
0 commit comments