Skip to content

Instantly share code, notes, and snippets.

@MonteLogic
Created September 1, 2024 02:49
Show Gist options
  • Select an option

  • Save MonteLogic/b9fbc1be10a74cfbe48632f8f034e320 to your computer and use it in GitHub Desktop.

Select an option

Save MonteLogic/b9fbc1be10a74cfbe48632f8f034e320 to your computer and use it in GitHub Desktop.
'use client';
import * as React from 'react';
import { useSignUp } from '@clerk/nextjs';
import { useRouter } from 'next/navigation';
export default function Page() {
const { isLoaded, signUp, setActive } = useSignUp();
const [firstName, setFirstName] = React.useState('');
const [lastName, setLastName] = React.useState('');
const router = useRouter();
// Get the token from the query parameter
const param = '__clerk_ticket';
const ticket = new URL(window.location.href).searchParams.get(param);
// If there is no invitation token, restrict access to the sign-up page
if (!ticket) {
return <p>You need an invitation to sign up for this application.</p>;
}
// Handle submission of the sign-up form
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!isLoaded) return;
try {
if (!ticket) return null;
// Optional: collect additional user information
const firstName = 'John';
const lastName = 'Doe';
// Create a new sign-up with the supplied invitation token.
// Make sure you're also passing the ticket strategy.
// After the below call, the user's email address will be
// automatically verified because of the invitation token.
const signUpAttempt = await signUp.create({
strategy: 'ticket',
ticket,
firstName,
lastName,
});
// This could be good for redirecting user to /onboarding.
// If verification was completed, set the session to active
// and redirect the user
if (signUpAttempt.status === 'complete') {
await setActive({ session: signUpAttempt.createdSessionId });
router.push('/');
} else {
// If the status is not complete, check why. User may need to
// complete further steps.
console.error(JSON.stringify(signUpAttempt, null, 2));
}
} catch (err: any) {
console.error(JSON.stringify(err, null, 2));
}
};
// Display the initial sign-up form to capture optional sign-up info
return (
<>
<h1>Sign up</h1>
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="firstName">Enter first name</label>
<input
id="firstName"
type="text"
name="firstName"
value={firstName}
onChange={(e) => setFirstName(e.target.value)}
/>
</div>
<div>
<label htmlFor="lastName">Enter last name</label>
<input
id="lastName"
type="text"
name="lastName"
value={lastName}
onChange={(e) => setLastName(e.target.value)}
/>
</div>
<div>
<button type="submit">Next</button>
</div>
</form>
</>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment