Skip to main content
Docs

Build your own sign-in-or-up page for your React app with Clerk

Important

There are many routing libraries available for React, but Clerk Docs uses React Router as it's the most popular and well-supported routing library for React. If you're using a different routing library, use these guides as a starting point.

This guide shows you how to use the <SignIn /> component to build a custom page that allows users to sign in or sign up within a single flow.

To set up separate sign-in and sign-up pages, follow this guide, and then follow the custom sign-up page guideReact Icon.

Note

Just getting started with Clerk and React? See the quickstart tutorialReact Icon!

Build a sign-in-or-up page

The following example demonstrates how to render the <SignIn /> component on a dedicated page.

Warning

This example assumes you've added React Router to your application. If you haven't, see the dedicated tutorialReact Icon.

Create a src/sign-in.tsx file and add the following code:

src/sign-in.tsx
import { SignIn } from '@clerk/react'

export default function SignInPage() {
  return <SignIn />
}

Warning

Since this approach uses window.location.pathname instead of a routing library, the app will flicker on navigation between pages and steps. For production apps, consider using a routing library like React Router.

Update your src/App.tsx file to add the SignInPage() component and handle the /sign-in route.

src/App.tsx
import { Show, SignInButton, SignUpButton, UserButton, SignIn } from '@clerk/react'

function SignInPage() {
  return <SignIn path="/sign-in" />
}

export default function App() {
  const path = window.location.pathname

  if (path === '/sign-in' || path.startsWith('/sign-in/')) {
    return (
      <div>
        <SignInPage />
      </div>
    )
  }

  return (
    <header>
      <Show when="signed-out">
        <SignInButton />
        <SignUpButton />
      </Show>
      <Show when="signed-in">
        <UserButton />
      </Show>
    </header>
  )
}

Update your src/main.tsx to import the sign-in page, add a route for it, and configure <ClerkProvider> with the following props:

  • Set signInUrl to tell Clerk where the <SignIn /> component is being hosted.
  • Set signInFallbackRedirectUrl as a fallback URL in case users visit the /sign-in route directly.
  • Set signUpFallbackRedirectUrl as a fallback URL in case users select the 'Don't have an account? Sign up' link at the bottom of the component.

Learn more about these props and how to customize Clerk's redirect behavior in the dedicated guide.

src/main.tsx
import { StrictMode } from 'react'
import { createRoot } from 'react-dom/client'
import { BrowserRouter, Routes, Route, useNavigate } from 'react-router'
import { ClerkProvider } from '@clerk/react'
import './index.css'
import SignInPage from './sign-in.tsx'
import App from './App.tsx'

function RootLayout() {
  const navigate = useNavigate()

  return (
    <ClerkProvider
      routerPush={(to) => navigate(to)}
      routerReplace={(to) => navigate(to, { replace: true })}
      signInUrl="/sign-in"
      signInFallbackRedirectUrl="/"
      signUpFallbackRedirectUrl="/"
    >
      <Routes>
        <Route path="/" element={<App />} />
        {/* Must be a splat route (catch-all) route to handle nested paths */}
        <Route path="/sign-in/*" element={<SignInPage />} />
      </Routes>
    </ClerkProvider>
  )
}

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <BrowserRouter>
      <RootLayout />
    </BrowserRouter>
  </StrictMode>,
)

Update your <ClerkProvider> to add the signInUrl, signInFallbackRedirectUrl, and signUpFallbackRedirectUrl props.

  • Set signInUrl to tell Clerk where the <SignIn /> component is being hosted.
  • Set signInFallbackRedirectUrl as a fallback URL in case users visit the /sign-in route directly.
  • Set signUpFallbackRedirectUrl as a fallback URL in case users select the 'Don't have an account? Sign up' link at the bottom of the component.

Learn more about these props and how to customize Clerk's redirect behavior in the dedicated guide.

src/main.tsx
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.tsx'
import { ClerkProvider } from '@clerk/react'

const PUBLISHABLE_KEY = import.meta.env.VITE_CLERK_PUBLISHABLE_KEY

if (!PUBLISHABLE_KEY) {
  throw new Error('Add your Clerk Publishable Key to the .env file')
}

ReactDOM.createRoot(document.getElementById('root')!).render(
  <React.StrictMode>
    <ClerkProvider
      publishableKey={PUBLISHABLE_KEY}
      signInUrl="/sign-in"
      signInFallbackRedirectUrl="/"
      signUpFallbackRedirectUrl="/"
    >
      <App />
    </ClerkProvider>
  </React.StrictMode>,
)

Visit your new page

Run your project with the following command:

terminal
npm run dev
terminal
pnpm run dev
terminal
yarn dev
terminal
bun run dev

Visit your new custom page locally at localhost:5173/sign-in.

Next steps

Learn more about Clerk components, how to use them to create custom pages, and how to use Clerk's client-side helpers using the following guides.

Create a custom sign-up page

Learn how to add a custom sign-up page to your React app with Clerk's components.

Protect content and read user data

Learn how to use Clerk's hooks to protect content and read user data in your React app.

Prebuilt components

Learn how to quickly add authentication to your app using Clerk's suite of components.

Customization & localization

Learn how to customize and localize the Clerk components.

Clerk React SDK Reference

Learn about the Clerk React SDK and how to integrate it into your app.

Feedback

What did you think of this content?

Last updated on