import { isClerkAPIResponseError, useSignUp } from '@clerk/clerk-expo' import { ClerkAPIError } from '@clerk/types' import { router } from 'expo-router' import { useState } from 'react' import { StyleSheet, Text, TextInput, TouchableOpacity, View } from 'react-native' export default function SignUpScreen() { const { signUp, setActive, isLoaded } = useSignUp() const [pendingVerification, setPendingVerification] = useState(false) const [code, setCode] = useState('') const [errors, setErrors] = useState([]) const [fname, setFname] = useState('') const [lname, setLname] = useState('') const [email, setUsername] = useState('') const [password, setPassword] = useState('') async function onSignUpPress() { if (!isLoaded) return setErrors([]) try { // Start Auth await signUp.create({ firstName: fname, lastName: lname, emailAddress: email, password }) // Set confirmation await signUp.prepareEmailAddressVerification() setPendingVerification(true) } catch (e) { if (isClerkAPIResponseError(e)) setErrors(e.errors) console.log(JSON.stringify(e)) } } async function onVerifyPress() { if (!isLoaded) return setErrors([]) try { // Use the code the user provided to attempt verification const signUpAttempt = await signUp.attemptEmailAddressVerification({ code }) // If verification was completed, set the session to active // and redirect the user if (signUpAttempt.status === 'complete') { await createUser(signUpAttempt.createdUserId) await setActive({ session: signUpAttempt.createdSessionId }) } else { // If the status is not complete, check why. User may need to // complete further steps. console.error(JSON.stringify(signUpAttempt, null, 2)) } } catch (e: any) { // See https://clerk.com/docs/custom-flows/error-handling // for more info on error handling console.error(JSON.stringify(e, null, 2)) setErrors(e.errors) } } async function createUser(clerkUserID: string | null) { fetch('http://localhost:3000/api/v1/users', { method: 'POST', headers: { Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json' }, body: JSON.stringify({ name: `${fname} ${lname}`, email, clerkUserID }) }) } if (pendingVerification) { return ( Enter the verification code we sent to {email} setCode(code)} /> Verify {errors.map((error) => ( {error.longMessage} ))} ) } return ( <> Create an account First name Last name Email Password { onSignUpPress() }} > Sign Up Already have an account? { router.navigate('/sign-in') }} > Sign In {' '} {errors.map((error) => ( {error.longMessage} ))} ) } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#25292e', justifyContent: 'center', alignItems: 'center' }, text: { color: '#fff' }, textCenter: { color: '#ffffff', alignSelf: 'center', marginBottom: 12 }, textlabel: { color: '#fff', marginBottom: 3 }, input: { backgroundColor: '#fff', borderRadius: 5, padding: 10, marginBottom: 15 }, button: { backgroundColor: '#1e90ff', padding: 12, borderRadius: 5, alignItems: 'center', marginBottom: 15 }, signIn: { alignItems: 'center' } })