|
| 1 | +import { SocialConnections } from '@/registry/blocks/social-connections'; |
| 2 | +import { Button } from '@/registry/ui/button'; |
| 3 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/registry/ui/card'; |
| 4 | +import { Input } from '@/registry/ui/input'; |
| 5 | +import { Label } from '@/registry/ui/label'; |
| 6 | +import { Separator } from '@/registry/ui/separator'; |
| 7 | +import { Text } from '@/registry/ui/text'; |
| 8 | +import { useSignIn } from '@clerk/clerk-expo'; |
| 9 | +import * as React from 'react'; |
| 10 | +import { Pressable, type TextInput, View } from 'react-native'; |
| 11 | + |
| 12 | +export function SignInForm() { |
| 13 | + const { signIn, setActive, isLoaded } = useSignIn(); |
| 14 | + const [email, setEmail] = React.useState(''); |
| 15 | + const [password, setPassword] = React.useState(''); |
| 16 | + const passwordInputRef = React.useRef<TextInput>(null); |
| 17 | + const [error, setError] = React.useState<{ email?: string; password?: string }>({}); |
| 18 | + |
| 19 | + async function onSubmit() { |
| 20 | + if (!isLoaded) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + // Start the sign-in process using the email and password provided |
| 25 | + try { |
| 26 | + const signInAttempt = await signIn.create({ |
| 27 | + identifier: email, |
| 28 | + password, |
| 29 | + }); |
| 30 | + |
| 31 | + // If sign-in process is complete, set the created session as active |
| 32 | + // and redirect the user |
| 33 | + if (signInAttempt.status === 'complete') { |
| 34 | + setError({ email: '', password: '' }); |
| 35 | + await setActive({ session: signInAttempt.createdSessionId }); |
| 36 | + // TODO: If your app does not use `Stack.Protected`, navigate to your protected screen |
| 37 | + return; |
| 38 | + } |
| 39 | + // TODO: Handle other statuses |
| 40 | + console.error(JSON.stringify(signInAttempt, null, 2)); |
| 41 | + } catch (err) { |
| 42 | + // See https://go.clerk.com/mRUDrIe for more info on error handling |
| 43 | + if (err instanceof Error) { |
| 44 | + const isEmailMessage = |
| 45 | + err.message.toLowerCase().includes('identifier') || |
| 46 | + err.message.toLowerCase().includes('email'); |
| 47 | + setError(isEmailMessage ? { email: err.message } : { password: err.message }); |
| 48 | + return; |
| 49 | + } |
| 50 | + console.error(JSON.stringify(err, null, 2)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + function onEmailSubmitEditing() { |
| 55 | + passwordInputRef.current?.focus(); |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <View className="gap-6"> |
| 60 | + <Card className="border-border/0 sm:border-border shadow-none sm:shadow-sm sm:shadow-black/5"> |
| 61 | + <CardHeader> |
| 62 | + <CardTitle className="text-center text-xl sm:text-left">Sign in to your app</CardTitle> |
| 63 | + <CardDescription className="text-center sm:text-left"> |
| 64 | + Welcome back! Please sign in to continue |
| 65 | + </CardDescription> |
| 66 | + </CardHeader> |
| 67 | + <CardContent className="gap-6"> |
| 68 | + <View className="gap-6"> |
| 69 | + <View className="gap-1.5"> |
| 70 | + <Label htmlFor="email">Email</Label> |
| 71 | + <Input |
| 72 | + id="email" |
| 73 | + |
| 74 | + keyboardType="email-address" |
| 75 | + autoComplete="email" |
| 76 | + autoCapitalize="none" |
| 77 | + onChangeText={setEmail} |
| 78 | + onSubmitEditing={onEmailSubmitEditing} |
| 79 | + returnKeyType="next" |
| 80 | + submitBehavior="submit" |
| 81 | + /> |
| 82 | + {error.email ? ( |
| 83 | + <Text className="text-destructive text-sm font-medium">{error.email}</Text> |
| 84 | + ) : null} |
| 85 | + </View> |
| 86 | + <View className="gap-1.5"> |
| 87 | + <View className="flex-row items-center"> |
| 88 | + <Label htmlFor="password">Password</Label> |
| 89 | + <Button |
| 90 | + variant="link" |
| 91 | + size="sm" |
| 92 | + className="web:h-fit ml-auto h-4 px-1 py-0 sm:h-4" |
| 93 | + onPress={() => { |
| 94 | + // TODO: Navigate to forgot password screen |
| 95 | + }}> |
| 96 | + <Text className="font-normal leading-4">Forgot your password?</Text> |
| 97 | + </Button> |
| 98 | + </View> |
| 99 | + <Input |
| 100 | + ref={passwordInputRef} |
| 101 | + id="password" |
| 102 | + secureTextEntry |
| 103 | + onChangeText={setPassword} |
| 104 | + returnKeyType="send" |
| 105 | + onSubmitEditing={onSubmit} |
| 106 | + /> |
| 107 | + {error.password ? ( |
| 108 | + <Text className="text-destructive text-sm font-medium">{error.password}</Text> |
| 109 | + ) : null} |
| 110 | + </View> |
| 111 | + <Button className="w-full" onPress={onSubmit}> |
| 112 | + <Text>Continue</Text> |
| 113 | + </Button> |
| 114 | + </View> |
| 115 | + <Text className="text-center text-sm"> |
| 116 | + Don't have an account?{' '} |
| 117 | + <Pressable |
| 118 | + onPress={() => { |
| 119 | + // TODO: Navigate to sign up screen |
| 120 | + }}> |
| 121 | + <Text className="text-sm underline underline-offset-4">Sign up</Text> |
| 122 | + </Pressable> |
| 123 | + </Text> |
| 124 | + <View className="flex-row items-center"> |
| 125 | + <Separator className="flex-1" /> |
| 126 | + <Text className="text-muted-foreground px-4 text-sm">or</Text> |
| 127 | + <Separator className="flex-1" /> |
| 128 | + </View> |
| 129 | + <SocialConnections /> |
| 130 | + </CardContent> |
| 131 | + </Card> |
| 132 | + </View> |
| 133 | + ); |
| 134 | +} |
0 commit comments