Mobilens English

邮箱验证码登录

验证码输入样式

演示验证码 123456

这个流程的所有画面

邮箱这一步

验证码这一步

完成

何时出现
怎么离开
下一步
行为规则

    用法

    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

    参数类型默认值说明
    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.

    这个流程用到的控件