useSignIn
Access the SignIn object inside of your components.
Overview
The useSignIn
hook gives you access to the SignIn object inside your components.
You can use the methods of the SignIn
object to create your own custom sign in flow, as an alternative to using Clerk's pre-built <SignIn/> component.
The SignIn
object will also contain the state of the sign in attempt that is currently in progress, giving you the chance to examine all the details and act accordingly.
Usage
Getting access to the SignIn object from inside one of your components is easy. Note that the useSignIn
hook can only be used inside a <ClerkProvider/> context.
The following example accesses the SignIn
object to check the current sign in attempt's status.
Typescript
For better type support, we highly recommend updating to at least Typescript 4.6. Applications using Typescript will benefit significantly from Typescript's new Control Flow Analysis for Dependent Parameters when using our hooks.
import { useSignIn } from "@clerk/clerk-react";// Your component must be rendered as a// descendant of <ClerkProvider />.function SignInStep() {const { isLoaded, signIn } = useSignIn();if (!isLoaded) {// handle loading statereturn null;}return (<div>The current sign in attempt statusis {signIn.status}.</div>);}
A more involved example follows below. In this example, we show an approach to create your own custom form for signing in your users.
We recommend using the <SignIn/> component instead of building your own custom sign in form. It gives you a ready-made form and handles each step of the sign in flow.
import { useSignIn } from "@clerk/clerk-react";// Your component must be rendered as a// descendant of <ClerkProvider />.function SignInForm() {const [email, setEmail] = useState('');const [password, setPassword] = useState('');const { isLoaded, signIn } = useSignIn();if (!isLoaded) {// handle loading statereturn null;}async function submit(e) {e.preventDefault();// Check the sign in response to// decide what to do next.await signIn.create({identifier: email,password,});}return (<form onSubmit={submit}><div><label htmlFor="email">Email</label><inputtype="email"value={email}onChange={e => setEmail(e.target.value)}/></div><div><label htmlFor="password">Password</label><inputtype="password"value={password}onChange={e => setPassword(e.target.value)}/></div><div><button>Sign in</button></div></form>);}