Mobilens 中文
Code input style

Demo code 123456

Every screen in this flow

Email step

Code step

Done

When
Way out
Next
Behaviour

    Usage

    import { EmailOtpSignIn, type EmailOtpApi } from '@/components/mobilens/EmailOtpSignIn';
    
    // Map your backend's answers onto the result shapes. Network errors can simply throw: they show as offline.
    const api: EmailOtpApi = {
      async sendCode(email) {
        const res = await fetch('https://api.example.com/auth/code', {
          method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email }),
        });
        if (res.status === 429) {
          const retry = res.headers.get('Retry-After');
          const seconds = retry && /^\d+$/.test(retry) ? Number(retry) : (Date.parse(retry ?? '') - Date.now()) / 1000;
          return { status: 'rate-limited', retryInSeconds: Number.isFinite(seconds) ? Math.max(0, Math.ceil(seconds)) : 60 };
        }
        if (!res.ok) return { status: 'failed' };
        return { status: 'sent', expiresInSeconds: 600, resendInSeconds: 30 };
      },
      async verifyCode(email, code) {
        const res = await fetch('https://api.example.com/auth/verify', {
          method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email, code }),
        });
        if (res.ok) return { status: 'ok' };
        const body = await res.json().catch(() => null);
        if (body?.reason === 'expired') return { status: 'expired' };
        if (body?.reason === 'locked') return { status: 'locked', retryInSeconds: body.retryInSeconds ?? 900 };
        if (body?.reason === 'wrong' && Number.isInteger(body.attemptsLeft)) return { status: 'wrong', attemptsLeft: body.attemptsLeft };
        return { status: 'failed' };
      },
    };
    
    // With Expo Router: goHome = () => router.replace('/')
    export function SignInScreen({ goHome }: { goHome: () => void }) {
      return <EmailOtpSignIn api={api} otpVariant="segmented" onSignedIn={goHome} />;
    }
    

    API

    PropTypeDefaultDescription
    title? string 'Sign in' Heading shown on the email entry step.
    primaryLabel? string 'Send code' Label of the button that requests the first code.
    labels? Partial<EmailOtpSignInLabels> – The words the screen shows. Pass a partial set to change only some of them.
    otpVariant? OtpInputVariant 'segmented' Display the verification code in separate cells or one outlined field.
    api EmailOtpApi – sendCode and verifyCode, connected to your backend.
    onSignedIn (email: string) => void – Called once the code is accepted. Navigate into the app from here.
    timeoutMs? number 15000 How long to wait for the backend before showing the timeout state, in ms.
    autoFocus? boolean true Focus the field and bring up the keyboard when the screen opens.
    initialState? SignInState { status: 'idle' Where to start. Defaults to an empty email step; pass a saved state to resume, or any state in tests.
    onStateChange? (state: SignInState) => void – Called with every state the screen enters, for analytics or logging.

    Controls in this flow