Sign in
Your own form on your own page, checked by your form library, with each step taken to Synq through your server.
Built on @synqauth/react over @synqauth/nextjs’s route mount, so it needs a server of yours. In a single-page app, createSynqClient from @synqauth/js signs in: signIn(), then handleCallback() where Synq sends the browser back. @synqauth/js in the docs
src/examples/sign-in/OwnUi.tsx'use client'
import { yupResolver } from '@hookform/resolvers/yup'
import { type SignInProblem, useAuth, useSignIn } from '@synqauth/react'
import { type ReactNode, useEffect } from 'react'
import { useForm } from 'react-hook-form'
import * as yup from 'yup'
// Sign-in on your own page with your own form: useSignIn() takes each step
// through the route mount, and your form library checks what was typed
// first (react-hook-form with yup here). `problem.code` says what Synq
// refused; branch on it, never on the English.
const credentials = yup.object({
email: yup.string().trim().email('Enter a valid email address').required('Enter your email'),
password: yup.string().required('Enter your password'),
})
type Credentials = yup.InferType<typeof credentials>
const codeSchema = yup.object({
code: yup
.string()
.trim()
.matches(/^\d{6}$/, 'The code is 6 digits')
.required('Enter the code'),
})
type Code = yup.InferType<typeof codeSchema>
const WORDS: Partial<Record<SignInProblem['code'], string>> = {
wrong_credentials: 'That email and password don’t match.',
wrong_code: 'That code isn’t right.',
locked: 'Too many tries: wait a little.',
}
export function SignInOwnUi() {
const { isSignedIn, userId, signOut } = useAuth()
const signIn = useSignIn({ returnTo: '/build/sign-in?variant=own-ui' })
const { handover, continueOnHostedPage } = signIn
// Only the hosted page can go on (a token gate, say): go there.
useEffect(() => {
if (handover) continueOnHostedPage()
}, [handover, continueOnHostedPage])
if (isSignedIn) {
return (
<p>
Signed in as <code>{userId}</code>{' '}
<button type="button" onClick={() => void signOut()}>
Sign out
</button>
</p>
)
}
const problem = signIn.problem && (
<p role="alert">{WORDS[signIn.problem.code] ?? signIn.problem.message}</p>
)
if (signIn.step === 'code' || signIn.step === 'verify-email') {
return <CodeForm signIn={signIn} problem={problem} />
}
return <CredentialsForm signIn={signIn} problem={problem} />
}
type SignIn = ReturnType<typeof useSignIn>
function CredentialsForm({ signIn, problem }: { signIn: SignIn; problem: ReactNode }) {
const {
register,
handleSubmit,
getValues,
trigger,
formState: { errors, isValid },
} = useForm<Credentials>({
resolver: yupResolver(credentials),
mode: 'onChange',
defaultValues: { email: '', password: '' },
})
const emailMeACode = async () => {
if (await trigger('email')) await signIn.emailCode.send(getValues('email'))
}
return (
<form
onSubmit={(event) => void handleSubmit((values) => signIn.password.signIn(values))(event)}
>
<label>
Email <input type="email" autoComplete="username" {...register('email')} />
</label>
{errors.email && <p role="alert">{errors.email.message}</p>}
<label>
Password <input type="password" autoComplete="current-password" {...register('password')} />
</label>
{errors.password && <p role="alert">{errors.password.message}</p>}
{problem}
<button type="submit" disabled={!isValid || signIn.pending}>
Sign in
</button>{' '}
<button type="button" disabled={signIn.pending} onClick={() => void emailMeACode()}>
Email me a code instead
</button>
</form>
)
}
function CodeForm({ signIn, problem }: { signIn: SignIn; problem: ReactNode }) {
const verifying = signIn.step === 'verify-email'
const {
register,
handleSubmit,
formState: { errors, isValid },
} = useForm<Code>({
resolver: yupResolver(codeSchema),
mode: 'onChange',
defaultValues: { code: '' },
})
const submit = (values: Code) =>
verifying ? signIn.verifyEmail.verify(values.code) : signIn.emailCode.verify(values.code)
return (
<form onSubmit={(event) => void handleSubmit(submit)(event)}>
<p>We sent a code to {signIn.email}.</p>
<label>
Code <input inputMode="numeric" autoComplete="one-time-code" {...register('code')} />
</label>
{errors.code && <p role="alert">{errors.code.message}</p>}
{problem}
<button type="submit" disabled={!isValid || signIn.pending}>
Verify
</button>{' '}
<button
type="button"
onClick={() => void (verifying ? signIn.verifyEmail.resend() : signIn.emailCode.resend())}
>
Send another code
</button>{' '}
{verifying && signIn.canSkip ? (
<button type="button" onClick={() => void signIn.verifyEmail.skip()}>
Skip for now
</button>
) : (
<button type="button" onClick={() => void signIn.startOver()}>
Start over
</button>
)}
</form>
)
}