# Sign up with phone verification

> A React Native sign-up flow with phone verification for iOS, Android and the web: a phone number field with country flags and dialling codes, then a six-digit SMS code with autofill, paste, a resend timer and an error for a wrong code. One file, built with Patine's PhoneField, InputOTP, Steps and Button.

# Sign up and verify

Two steps: a phone number, with the country's flag and dialling code, then the code texted to it. The keyboard offers the code straight from the message, a paste fills every box, and a wrong code says so under the boxes. The code in this demo is `246810`.

```tsx live device
import { useEffect, useState } from 'react';
import { ScrollView, StyleSheet, View } from 'react-native';
import { Alert, Button, Heading, InputOTP, Link, PhoneField, Steps, Text, useTheme } from 'patine-ui';
import { Icon } from '@patine-ui/icons';

const STEPS = [{ title: 'Phone' }, { title: 'Code' }, { title: 'Done' }];
const RESEND_AFTER = 30;
// The demo's server: this code is the one "texted".
const CODE = '246810';
const verify = (code: string) => new Promise<boolean>((resolve) => setTimeout(() => resolve(code === CODE), 700));

export default function SignUp() {
  const { colors } = useTheme();
  const [step, setStep] = useState(0);
  const [phone, setPhone] = useState('');
  const [valid, setValid] = useState(false);
  const [code, setCode] = useState('');
  const [checking, setChecking] = useState(false);
  const [wrong, setWrong] = useState(false);
  const [wait, setWait] = useState(RESEND_AFTER);

  useEffect(() => {
    if (step !== 1 || wait === 0) return;
    const timer = setTimeout(() => setWait(wait - 1), 1000);
    return () => clearTimeout(timer);
  }, [step, wait]);

  const sendCode = () => {
    setCode('');
    setWrong(false);
    setWait(RESEND_AFTER);
    setStep(1);
  };

  const check = async (entered: string) => {
    setChecking(true);
    const ok = await verify(entered);
    setChecking(false);
    setWrong(!ok);
    if (ok) setStep(2);
  };

  return (
    <ScrollView style={{ backgroundColor: colors.background }} contentContainerStyle={styles.screen} keyboardShouldPersistTaps="handled">
      <Steps steps={STEPS} value={step} />

      {step === 0 ? (
        <View style={styles.step}>
          <View style={styles.titles}>
            <Heading level={2}>What’s your number?</Heading>
            <Text color="textSecondary">We’ll text you a code to check it’s yours. Carrier rates may apply.</Text>
          </View>
          <PhoneField
            label="Phone number"
            defaultCountry="PT"
            value={phone}
            onValueChange={(value, details) => {
              setPhone(value);
              setValid(details.valid);
            }}
            helperText="Only used to sign in. Never shown on your profile."
          />
          <Button size="lg" disabled={!valid} onPress={sendCode}>
            Send code
          </Button>
        </View>
      ) : null}

      {step === 1 ? (
        <View style={styles.step}>
          <View style={styles.titles}>
            <Heading level={2}>Enter the code</Heading>
            <Text color="textSecondary">
              Sent to {phone}. <Link onPress={() => setStep(0)}>Change number</Link>
            </Text>
          </View>
          <InputOTP
            value={code}
            onValueChange={(next) => {
              setCode(next);
              setWrong(false);
            }}
            onComplete={check}
            error={wrong ? 'That code doesn’t match. Check the message and try again.' : undefined}
            disabled={checking}
            autoFocus
          />
          <Button size="lg" loading={checking} disabled={code.length < 6} onPress={() => check(code)}>
            Verify
          </Button>
          <Button variant="ghost" disabled={wait > 0} onPress={sendCode}>
            {wait > 0 ? `Resend code in 0:${String(wait).padStart(2, '0')}` : 'Resend code'}
          </Button>
        </View>
      ) : null}

      {step === 2 ? (
        <View style={styles.step}>
          <View style={[styles.done, { backgroundColor: colors.primaryMuted }]}>
            <Icon name="check" size={32} color="primary" />
          </View>
          <View style={styles.titles}>
            <Heading level={2}>You’re verified</Heading>
            <Text color="textSecondary">{phone} is now linked to your account.</Text>
          </View>
          <Alert title="Tip" description="Turn on two-step sign-in in Settings to ask for a code on new devices." />
          <Button size="lg" onPress={() => setStep(0)}>
            Continue
          </Button>
        </View>
      ) : null}
    </ScrollView>
  );
}

const styles = StyleSheet.create({
  screen: { flexGrow: 1, gap: 28, padding: 24 },
  step: { gap: 16 },
  titles: { gap: 6 },
  done: { width: 64, height: 64, borderRadius: 32, alignItems: 'center', justifyContent: 'center' },
});
```

## Components used

- [PhoneField](/docs/components/phone-field): the country's flag and dialling code, formatting as you type, and `details.valid` to enable the button
- [InputOTP](/docs/components/input-otp): six boxes with SMS autofill, paste, `onComplete` and an `error`
- [Steps](/docs/components/steps): where you are in the flow
- [Button](/docs/components/button): `loading` while the code is checked, `ghost` for the resend timer
- [Alert](/docs/components/alert), [Link](/docs/components/link), [Heading, Text](/docs/components/text), [Patine Icons](/docs/components/icons)

## How it works

- **`onComplete`** fires when the last box is filled, so a pasted or autofilled code is checked without a tap. The Verify button is there for people who want one.
- **The value is E.164.** `PhoneField` gives `+351912345678` whatever the reader typed or pasted, ready for your SMS provider.
- **The resend timer** is a `useEffect` that counts down only on the code step, and starts over each time a code is sent.
