Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SettingsAuthRouteScreen.tsx

65 lines2.0 KB

1import { useAuth } from "@clerk/expo";
2import { AuthView, type UserProfileCustomPage, UserProfileView } from "@clerk/expo/native";
3import { StackActions, useNavigation } from "@react-navigation/native";
4import { useCallback, useEffect, useLayoutEffect, useRef } from "react";
5import { View } from "react-native";
6​
7import { hasCloudPublicConfig } from "../cloud/publicConfig";
8import { T3ConnectProfilePage } from "../cloud/T3ConnectProfilePage";
9​
10// Custom rows in Clerk's native profile. Mirrors the web UserButton pages.
11const USER_PROFILE_CUSTOM_PAGES = [
12 {
13 path: "t3-connect",
14 label: "T3 Connect",
15 icon: "globe",
16 content: <T3ConnectProfilePage />,
17 },
18] satisfies UserProfileCustomPage[];
19​
20export function SettingsAuthRouteScreen() {
21 const navigation = useNavigation();
22​
23 useLayoutEffect(() => {
24 if (!hasCloudPublicConfig()) {
25 navigation.dispatch(StackActions.replace("SettingsContent"));
26 }
27 }, [navigation]);
28​
29 return hasCloudPublicConfig() ? <ConfiguredSettingsAuthRouteScreen /> : null;
31​
32function ConfiguredSettingsAuthRouteScreen() {
33 const { isLoaded, isSignedIn } = useAuth({ treatPendingAsSignedOut: false });
34 const navigation = useNavigation();
35 const handleHostBack = useCallback(
36 () => navigation.dispatch(StackActions.popTo("SettingsContent")),
37 [navigation],
38 );
39 const hasBeenSignedIn = useRef(isSignedIn);
40 if (isSignedIn) {
41 hasBeenSignedIn.current = true;
42 }
43​
44 useEffect(() => {
45 if (hasBeenSignedIn.current && isLoaded && isSignedIn === false) {
46 navigation.dispatch(StackActions.popTo("SettingsContent"));
47 }
48 }, [isLoaded, isSignedIn, navigation]);
49​
50 return (
51 <View collapsable={false} className="flex-1 overflow-hidden bg-sheet">
52 {isLoaded ? (
53 hasBeenSignedIn.current ? (
54 <UserProfileView
55 customPages={USER_PROFILE_CUSTOM_PAGES}
56 isDismissible={false}
57 onHostBack={handleHostBack}
58 />
59 ) : (
60 <AuthView isDismissible={false} onHostBack={handleHostBack} />
61 )
62 ) : null}
63 </View>
64 );