Clerk logo

Clerk Docs

Ctrl + K
Go to clerkstage.dev
Check out a preview of our new docs.

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.

1
import { useSignUp } from '@clerk/clerk-react';
2
3
// Your component must be rendered as a
4
// descendant of <ClerkProvider />
5
export default function SignUpStep() {
6
const { isLoaded, signUp } = useSignUp();
7
8
if (!isLoaded) {
9
// Handle loading state
10
return null;
11
}
12
13
return (
14
<div>
15
The current sign up attempt status
16
is {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.

1
import { useSignUp } from '@clerk/clerk-react';
2
3
export default function SignUpForm() {
4
const { isLoaded, signUp } = useSignUp();
5
6
const handleSubmit = async (event) => {
7
const formData = new FormData(event.target);
8
const emailAddress = formData.get('email');
9
const password = formData.get('password');
10
11
event.preventDefault();
12
13
// Check response for next step
14
const response = await signUp.create({
15
emailAddress,
16
password
17
})
18
.then((result) => {
19
if (result.status === "complete") {
20
console.log(result);
21
setActive({session: result.createdSessionId});
22
}
23
else{
24
console.log(result);
25
}
26
})
27
.catch((err) => console.error("error", err.errors[0].longMessage));
28
};
29
30
if (!isLoaded) {
31
// Handle loading state
32
return null;
33
}
34
35
return (
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 state
return null;
}
return (
<div>
The current sign in attempt status
is {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.

1
import { useSignIn } from '@clerk/clerk-react';
2
3
export default function SignInForm() {
4
const { isLoaded, signIn } = useSignIn();
5
6
const handleSubmit = async (event) => {
7
const formData = new FormData(event.target);
8
const emailAddress = formData.get('email');
9
const password = formData.get('password');
10
11
event.preventDefault();
12
13
const response = await signIn.create({
14
identifier: emailAddress,
15
password,
16
})
17
.then((result) => {
18
if (result.status === "complete") {
19
console.log(result);
20
setActive({session: result.createdSessionId});
21
}
22
else{
23
console.log(result);
24
}
25
})
26
.catch((err) => console.error("error", err.errors[0].longMessage));
27
};
28
29
if (!isLoaded) {
30
// Handle loading state
31
return null;
32
}
33
34
return (
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
}

Was this helpful?

Clerk © 2023