|
| 1 | +import { useAuth, useSession, useUser } from '@clerk/expo'; |
| 2 | +import { AuthView, type AuthViewMode, UserButton } from '@clerk/expo/native'; |
| 3 | +import { type ReactElement, useState } from 'react'; |
| 4 | +import { Button, Modal, StyleSheet, Text, View } from 'react-native'; |
| 5 | + |
| 6 | +export function Home({ authMode }: { authMode: AuthViewMode }): ReactElement { |
| 7 | + const { isSignedIn, signOut } = useAuth(); |
| 8 | + const { user } = useUser(); |
| 9 | + const { session } = useSession(); |
| 10 | + const [isAuthOpen, setIsAuthOpen] = useState(false); |
| 11 | + const closeAuth = (): void => setIsAuthOpen(false); |
| 12 | + |
| 13 | + const pending = session?.status === 'pending'; |
| 14 | + const account = pending ? null : user; |
| 15 | + const activeSession = pending ? null : session; |
| 16 | + const emailAddress = account?.primaryEmailAddress?.emailAddress; |
| 17 | + |
| 18 | + return ( |
| 19 | + <View style={styles.home}> |
| 20 | + {isSignedIn && <UserButton />} |
| 21 | + {(account || activeSession) && ( |
| 22 | + <View style={styles.summary}> |
| 23 | + {account && ( |
| 24 | + <Text |
| 25 | + testID='e2e.auth.signedIn' |
| 26 | + style={styles.heading} |
| 27 | + > |
| 28 | + {emailAddress ? `Signed in as ${emailAddress}` : 'Signed in'} |
| 29 | + </Text> |
| 30 | + )} |
| 31 | + {account && ( |
| 32 | + <View style={styles.detail}> |
| 33 | + <Text style={styles.detailText}>User ID</Text> |
| 34 | + <Text |
| 35 | + testID='e2e.auth.userId' |
| 36 | + style={styles.detailText} |
| 37 | + > |
| 38 | + {account.id} |
| 39 | + </Text> |
| 40 | + </View> |
| 41 | + )} |
| 42 | + {activeSession && ( |
| 43 | + <View style={styles.detail}> |
| 44 | + <Text style={styles.detailText}>Session ID</Text> |
| 45 | + <Text |
| 46 | + testID='e2e.auth.sessionId' |
| 47 | + style={styles.detailText} |
| 48 | + > |
| 49 | + {activeSession.id} |
| 50 | + </Text> |
| 51 | + </View> |
| 52 | + )} |
| 53 | + </View> |
| 54 | + )} |
| 55 | + {isSignedIn ? ( |
| 56 | + <Button |
| 57 | + testID='e2e.auth.signOut' |
| 58 | + title='Sign out' |
| 59 | + onPress={() => void signOut()} |
| 60 | + /> |
| 61 | + ) : ( |
| 62 | + <> |
| 63 | + <Text |
| 64 | + testID='e2e.auth.signedOut' |
| 65 | + style={styles.heading} |
| 66 | + > |
| 67 | + Signed out |
| 68 | + </Text> |
| 69 | + <Button |
| 70 | + testID='e2e.auth.signIn' |
| 71 | + title='Sign in' |
| 72 | + onPress={() => setIsAuthOpen(true)} |
| 73 | + /> |
| 74 | + </> |
| 75 | + )} |
| 76 | + |
| 77 | + <Modal |
| 78 | + animationType='slide' |
| 79 | + visible={isAuthOpen} |
| 80 | + presentationStyle='pageSheet' |
| 81 | + onRequestClose={closeAuth} |
| 82 | + > |
| 83 | + <AuthView |
| 84 | + mode={authMode} |
| 85 | + onDismiss={closeAuth} |
| 86 | + /> |
| 87 | + </Modal> |
| 88 | + </View> |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +const styles = StyleSheet.create({ |
| 93 | + detail: { |
| 94 | + flexDirection: 'row', |
| 95 | + gap: 4, |
| 96 | + }, |
| 97 | + detailText: { |
| 98 | + color: '#6B6B76', |
| 99 | + fontSize: 13, |
| 100 | + }, |
| 101 | + heading: { |
| 102 | + fontSize: 17, |
| 103 | + textAlign: 'center', |
| 104 | + }, |
| 105 | + home: { |
| 106 | + alignItems: 'center', |
| 107 | + flex: 1, |
| 108 | + gap: 24, |
| 109 | + justifyContent: 'center', |
| 110 | + padding: 24, |
| 111 | + }, |
| 112 | + summary: { |
| 113 | + alignItems: 'center', |
| 114 | + gap: 4, |
| 115 | + }, |
| 116 | +}); |
0 commit comments