useSignUp() and useSignIn()
Overview
If our Prebuilt Components don't meet your needs, you can build a fully custom sign-up or sign-in flow using the React hooks useSignUp()
and useSignIn()
respectively.
useSignUp()
The useSignUp()
hook gives you access to the SignUp
object and its available methods in order to build a custom sign-up flow. The SignUp
object will also contain the state of the sign-up attempt that is currently in progress, which gives you the ability to examine all the details and act accordingly.
Usage
Getting access to the SignUp
object from inside one of your components is easy. Note that the useSignUp()
hook can only be used inside a <ClerkProvider/> context.
The following example accesses the SignUp
object to check the current sign-up attempt's status.
1import { useSignUp } from '@clerk/clerk-react';23// Your component must be rendered as a4// descendant of <ClerkProvider />5export default function SignUpStep() {6const { isLoaded, signUp } = useSignUp();78if (!isLoaded) {9// Handle loading state10return null;11}1213return (14<div>15The current sign up attempt status16is {signUp.status}.17</div>18);19}
In a more involved example, we show an approach to create a custom form for registering users, in this case with a Password strategy.
1import { useSignUp } from '@clerk/clerk-react';23export default function SignUpForm() {4const { isLoaded, signUp } = useSignUp();56const handleSubmit = async (event) => {7const formData = new FormData(event.target);8const emailAddress = formData.get('email');9const password = formData.get('password');1011event.preventDefault();1213// Check response for next step14const response = await signUp.create({15emailAddress,16password17})18.then((result) => {19if (result.status === "complete") {20console.log(result);21setActive({session: result.createdSessionId});22}23else{24console.log(result);25}26})27.catch((err) => console.error("error", err.errors[0].longMessage));28};2930if (!isLoaded) {31// Handle loading state32return null;33}3435return (36<form onSubmit={handleSubmit}>37<div>38<label htmlFor="email">Email</label>39<input id="email" name="email" type="email" />40</div>41<div>42<label htmlFor="password">Password</label>43<input id="password" name="password" type="password" />44</div>45<button>Sign up</button>46</form>47);48}
useSignIn()
The useSignIn()
hook gives you access to the SignIn
object and its available methods in order to build a custom sign-in flow. The SignIn
object will also contain the state of the sign-in attempt that is currently in progress, which gives you the ability to example 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.
import { useSignIn } from '@clerk/clerk-react';// Your component must be rendered as a// descendant of <ClerkProvider />export default function SignInStep() {const { isLoaded, signIn } = useSignIn();if (!isLoaded) {// Handle loading statereturn null;}return (<div>The current sign in attempt statusis {signIn.status}.</div>);}
In a more involved example, we show an approach to create a custom form for signing in users, in this case with a Password strategy.
1import { useSignIn } from '@clerk/clerk-react';23export default function SignInForm() {4const { isLoaded, signIn } = useSignIn();56const handleSubmit = async (event) => {7const formData = new FormData(event.target);8const emailAddress = formData.get('email');9const password = formData.get('password');1011event.preventDefault();1213const response = await signIn.create({14identifier: emailAddress,15password,16})17.then((result) => {18if (result.status === "complete") {19console.log(result);20setActive({session: result.createdSessionId});21}22else{23console.log(result);24}25})26.catch((err) => console.error("error", err.errors[0].longMessage));27};2829if (!isLoaded) {30// Handle loading state31return null;32}3334return (35<form onSubmit={handleSubmit}>36<div>37<label htmlFor="email">Email</label>38<input id="email" name="email" type="email" />39</div>40<div>41<label htmlFor="password">Password</label>42<input id="password" name="password" type="password" />43</div>44<button>Sign in</button>45</form>46);47}