Skip to content

Commit

Permalink
feat: Add React Router docs (#1760)
Browse files Browse the repository at this point in the history
Co-authored-by: Brad Cornes <[email protected]>
Co-authored-by: victoria <[email protected]>
Co-authored-by: Alexis Aguilar <[email protected]>
  • Loading branch information
4 people authored Dec 12, 2024
1 parent a34d0fd commit bd1a2b5
Show file tree
Hide file tree
Showing 19 changed files with 944 additions and 386 deletions.
9 changes: 9 additions & 0 deletions docs/_partials/react-hooks.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- [`useUser()`](/docs/references/react/use-user){{ target: '_blank' }}
- [`useClerk()`](/docs/references/react/use-clerk){{ target: '_blank' }}
- [`useAuth()`](/docs/references/react/use-auth){{ target: '_blank' }}
- [`useSignIn()`](/docs/references/react/use-sign-in){{ target: '_blank' }}
- [`useSignUp()`](/docs/references/react/use-sign-up){{ target: '_blank' }}
- [`useSession()`](/docs/references/react/use-session){{ target: '_blank' }}
- [`useSessionList()`](/docs/references/react/use-session-list){{ target: '_blank' }}
- [`useOrganization()`](/docs/references/react/use-organization){{ target: '_blank' }}
- [`useOrganizationList()`](/docs/references/react/use-organization-list){{ target: '_blank' }}
6 changes: 6 additions & 0 deletions docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ Find all the guides and resources you need to develop with Clerk.
- [TanStack Start (Beta)](/docs/quickstarts/tanstack-start)
- Easily add secure and SSR-friendly authentication to your TanStack Start application with Clerk.
- ![](data:image/webp;base64,UklGRu4JAABXRUJQVlA4TOEJAAAvP8APEM8Hu9r2tM0n/1KSMjNze8UzdNWu0duOwMzMlfXTGnBt204b3SczDjPU0+Vr5j+nnZ6Zp2dm0vjJ0oUc2bZqZZ1z3rX/cXcdQphEQgoWB0TgMHOHa3JEASCA6mXbtl1bY02NHcC+gJtq6gS2j+Aa/Sf7/8nY7ADw+MR//od0UAdVqEIUojCFeOEKdZ2TC6d7an1+DTIlHUShDur0v2kPEIX1/ZF5rZCuYct/87xXJc3Jdcw4BHhRxdJq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w7+oL55wl5f5TNIGLyLleFUL/83tKKXWx69ZNp+TFS5d7VERMX/9lPrIbtNZadt10N2496ZeP/5f9OhJ9r6+E0YIUptxHjfhFP+7HCLS8ftUiBnY5Y8x/rdaihqEWi4hWkFfrbVKIdqZuPW6xHZ++wOiQbR8wLUL52tZjvZrQQdFcHtNyT3TH56+a9M9f8i8YYFKFxOyNQd8gYuc3SicSOphQbJmPPU+s1mTXwJD9+QuMaTeDITH7YrD+YwN/L68AKIP4g6YhNfD45gd8p5Qf9wD+MR+6Oj6rTmMGyLEhMddjkF9b5bLyW30bHAD5PJ1giFP8BuV96BmAwW8/zAo/5Vo+5uDP8tLC/NyshCWnFB3YePlPMmWtTZDr1RMUWvQUmJcbdGRZLTkm5sKS+hPLO9geHpGlotYoZWFybml1ZSubTslkvKGuprrKVZoK42Sd56PCnZXVpbnJAifm8HaMPkzNzrO4mYmC2mpOp5QkVotYA9V0oIIg633Pvd0qjE/McGJOsROH7cUFHg+zGZ1OeA2q3tUZRqqYw7WW3hsFD7gRF7cD85bpmUmKMXPzD7LGpMlNUEzq7BvVPD83E4g5HZw4uXKRUsLC/Zy16WQC4wJSSQV80oUSMjHLvGnf9gTLndw+myFXEJDqgEj/jdccCEnPgeAQUw/3AWQzaRVFKhynmTFOjxXHC4/2IykjInXGBD8CRlofHxbGi2OxMHTJHNhPOmdtNsMXjxXOhwgTfuJRKXTLA5Tv7GcaSAclEZR6nlTWJ/x7ZaGJ/7EDxvCFDoqJ9BN7BBceRw6pEEkLCzYJL3yOHhKWuPV5guJLNwfvD4eLFvgQNXLWbYKDB4OyT+yAhjd0NOK69UgkedvEzHQBosphjoR8DBuJQX531u3vjx0+yBcy0Nj0Ps/aLjQT7v3RI+RQt3+f2yuPHbHv6T4nsgEA)

---

- [React Router (Beta)](/docs/quickstarts/react-router)
- Easily add secure, edge- and SSR-friendly authentication to React Router with Clerk.
- {<svg viewBox="0 0 32 32"><path fill="#F44250" d="M25.6 15.568a3.196 3.196 0 0 1-3.2 3.19 3.193 3.193 0 0 0-3.2 3.189 3.196 3.196 0 0 1-3.2 3.19 3.193 3.193 0 0 1-2.263-.935 3.172 3.172 0 0 1-.937-2.255 3.193 3.193 0 0 1 3.2-3.19 3.196 3.196 0 0 0 3.2-3.189 3.194 3.194 0 0 0-3.2-3.19 3.193 3.193 0 0 1-3.2-3.189A3.193 3.193 0 0 1 16 6c1.767.001 3.2 1.429 3.2 3.19a3.193 3.193 0 0 0 3.2 3.19c1.768 0 3.2 1.427 3.2 3.19Z"/><path style={{ fill: 'var(--light, #121212) var(--dark, #fff)' }} d="M12.8 15.568a3.195 3.195 0 0 0-3.2-3.19 3.195 3.195 0 0 0-3.2 3.19 3.195 3.195 0 0 0 3.2 3.19c1.767 0 3.2-1.428 3.2-3.19ZM6.4 21.947a3.195 3.195 0 0 0-3.2-3.19 3.195 3.195 0 0 0-3.2 3.19 3.195 3.195 0 0 0 3.2 3.19c1.767 0 3.2-1.429 3.2-3.19ZM32 21.947a3.195 3.195 0 0 0-3.2-3.19 3.195 3.195 0 0 0-3.2 3.19 3.195 3.195 0 0 0 3.2 3.19c1.767 0 3.2-1.429 3.2-3.19Z"/></svg>}
</Cards>

## Explore by backend framework
Expand Down
66 changes: 55 additions & 11 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
"href": "/docs/quickstarts/nextjs",
"icon": "nextjs"
},
{
"title": "React Router",
"tag": "(Beta)",
"href": "/docs/quickstarts/react-router",
"icon": "react-router"
},
{
"title": "Remix",
"href": "/docs/quickstarts/remix",
Expand Down Expand Up @@ -1872,17 +1878,6 @@
"title": "Overview",
"href": "/docs/references/react/overview"
},
{
"title": "Guides",
"items": [
[
{
"title": "Add React Router",
"href": "/docs/references/react/add-react-router"
}
]
]
},
{
"title": "Client-side Helpers",
"items": [
Expand Down Expand Up @@ -2404,6 +2399,55 @@
]
]
},
{
"title": "React Router",
"collapse": true,
"icon": "react-router",
"tag": "(Beta)",
"items": [
[
{
"title": "Overview",
"href": "/docs/references/react-router/overview"
},
{
"title": "Guides",
"items": [
[
{
"title": "Read session and user data",
"href": "/docs/references/react-router/read-session-data"
},
{
"title": "Add custom sign up and sign in pages",
"href": "/docs/references/react-router/custom-signup-signin-pages"
},
{
"title": "Library mode",
"href": "/docs/references/react-router/library-mode"
}
]
]
},
{
"title": "General References",
"items": [
[
{
"title": "`rootAuthLoader()`",
"wrap": false,
"href": "/docs/references/react-router/root-auth-loader"
},
{
"title": "`getAuth()`",
"href": "/docs/references/react-router/get-auth"
}
]
]
}
]
]
},
{
"title": "Remix",
"collapse": true,
Expand Down
1 change: 1 addition & 0 deletions docs/manifest.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
"react",
"redwood",
"remix",
"react-router",
"rocket",
"route",
"ruby",
Expand Down
220 changes: 220 additions & 0 deletions docs/quickstarts/react-router.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
---
title: React Router Quickstart (Beta)
description: Learn how to use Clerk to quickly and easily add secure authentication and user management to your React Router application.
---

<TutorialHero
framework="react-router"
exampleRepo={[
{
title: "React Router Quickstart Repo",
link: "https://github.com/clerk/clerk-react-router-quickstart"

}
]}
beforeYouStart={[
{
title: "Set up a Clerk application",
link: "/docs/quickstarts/setup-clerk",
icon: "clerk",
},
{
title: "Create a React Router application",
link: "https://reactrouter.com/start/framework/installation",
icon: "react-router",
}
]}
>
- Install `@clerk/react-router`
- Set your Clerk API keys
- Configure `rootAuthLoader()`
- Add `<ClerkProvider>` and Clerk components
</TutorialHero>

Clerk's [React Router SDK](/docs/references/react-router/overview) provides prebuilt components, hooks, and stores to make it easy to integrate authentication and user management in your React Router app. This guide assumes that you're using [React Router v7 or later](https://api.reactrouter.com/v7).

<Steps>
### Install `@clerk/react-router`

Run the following command to install the SDK:

<CodeBlockTabs options={["npm", "yarn", "pnpm"]}>
```bash {{ filename: 'terminal' }}
npm install @clerk/react-router
```

```bash {{ filename: 'terminal' }}
yarn add @clerk/react-router
```

```bash {{ filename: 'terminal' }}
pnpm add @clerk/react-router
```
</CodeBlockTabs>

### Set your Clerk API keys

<SignedIn>
Add the following keys to your `.env` file. These keys can always be retrieved from the [**API keys**](https://dashboard.clerk.com/last-active?path=api-keys) page in the Clerk Dashboard.
</SignedIn>

<SignedOut>
1. In the Clerk Dashboard, navigate to the [**API keys**](https://dashboard.clerk.com/last-active?path=api-keys) page.
1. In the **Quick Copy** section, copy your Clerk Publishable and Secret Key.
1. Paste your keys into your `.env` file.

The final result should resemble the following:
</SignedOut>

```env {{ filename: '.env' }}
VITE_CLERK_PUBLISHABLE_KEY={{pub_key}}
CLERK_SECRET_KEY={{secret}}
```

### Configure `rootAuthLoader()`

The `rootAuthLoader()` function provides access to authentication state in any React Router route.

The following code shows how to add this function to your `root.tsx` file. If you're using [Clerk's React Router quickstart](https://github.com/clerk/clerk-react-router-quickstart) or the [React Router template](https://reactrouter.com/start/framework/installation), most of this code will already be present.

To load additional data or configure options, see the [`rootAuthLoader()`](/docs/references/react-router/root-auth-loader) reference.

```tsx {{ filename: 'app/root.tsx', mark: [1, [6, 8]], collapsible: true }}
import { rootAuthLoader } from '@clerk/react-router/ssr.server'
import { isRouteErrorResponse, Links, Meta, Outlet, Scripts, ScrollRestoration } from 'react-router'
import type { Route } from './+types/root'
import stylesheet from './app.css?url'

export async function loader(args: Route.LoaderArgs) {
return rootAuthLoader(args)
}

export const links: Route.LinksFunction = () => [
{ rel: 'preconnect', href: 'https://fonts.googleapis.com' },
{
rel: 'preconnect',
href: 'https://fonts.gstatic.com',
crossOrigin: 'anonymous',
},
{
rel: 'stylesheet',
href: 'https://fonts.googleapis.com/css2?family=Inter:ital,opsz,wght@0,14..32,100..900;1,14..32,100..900&display=swap',
},
{ rel: 'stylesheet', href: stylesheet },
]

export function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<Meta />
<Links />
</head>
<body>
{children}
<ScrollRestoration />
<Scripts />
</body>
</html>
)
}

export default function App() {
return <Outlet />
}

export function ErrorBoundary({ error }: Route.ErrorBoundaryProps) {
let message = 'Oops!'
let details = 'An unexpected error occurred.'
let stack: string | undefined

if (isRouteErrorResponse(error)) {
message = error.status === 404 ? '404' : 'Error'
details =
error.status === 404 ? 'The requested page could not be found.' : error.statusText || details
} else if (import.meta.env.DEV && error && error instanceof Error) {
details = error.message
stack = error.stack
}

return (
<main className="pt-16 p-4 container mx-auto">
<h1>{message}</h1>
<p>{details}</p>
{stack && (
<pre className="w-full p-4 overflow-x-auto">
<code>{stack}</code>
</pre>
)}
</main>
)
}
```

### Add `<ClerkProvider>` and Clerk components to your app

<Include src="_partials/clerk-provider" />

It's required to pass `loaderData` to the `<ClerkProvider>` component. This data is provided by the `rootAuthLoader()` function. It's also recommended to pass the `signUpFallbackRedirectUrl` and `signInFallbackRedirectUrl` props. These specify the fallback URL to redirect to after the user signs up or signs in, respectively, if there's no `redirect_url` in the path already.

You can control which content signed-in and signed-out users can see with Clerk's [prebuilt control components](/docs/components/overview#what-are-control-components).

The following example adds `<ClerkProvider>` and creates a header using the following Clerk components:

- [`<SignedIn>`](/docs/components/control/signed-in): Children of this component can only be seen while **signed in**.
- [`<SignedOut>`](/docs/components/control/signed-out): Children of this component can only be seen while **signed out**.
- [`<UserButton />`](/docs/components/user/user-button): Shows the signed-in user's avatar. Selecting it opens a dropdown menu with account management options.
- [`<SignInButton />`](/docs/components/unstyled/sign-in-button): An unstyled component that links to the sign-in page. In this example, since no props or [environment variables](/docs/deployments/clerk-environment-variables) are set for the sign-in URL, this component links to the [Account Portal sign-in page](/docs/customization/account-portal/overview#sign-in).

```tsx {{ filename: 'app/root.tsx' }}
// Other imports

import { ClerkProvider, SignedIn, SignedOut, UserButton, SignInButton } from '@clerk/react-router'

export default function App({ loaderData }: Route.ComponentProps) {
return (
<ClerkProvider
loaderData={loaderData}
signUpFallbackRedirectUrl="/"
signInFallbackRedirectUrl="/"
>
<header className="flex items-center justify-center py-8 px-4">
<SignedOut>
<SignInButton />
</SignedOut>
<SignedIn>
<UserButton />
</SignedIn>
</header>
<main>
<Outlet />
</main>
</ClerkProvider>
)
}

// Rest of the root.tsx code
```

### Create your first user

Run your project with the following command:

<CodeBlockTabs options={["npm", "yarn", "pnpm"]}>
```bash {{ filename: 'terminal' }}
npm run dev
```

```bash {{ filename: 'terminal' }}
yarn dev
```

```bash {{ filename: 'terminal' }}
pnpm dev
```
</CodeBlockTabs>

Visit your app's homepage at [`http://localhost:5173`](http://localhost:5173). Sign up to create your first user.
</Steps>
7 changes: 5 additions & 2 deletions docs/quickstarts/react.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ description: Add authentication and user management to your React app with Clerk

### Create a header with Clerk components

You can control which content signed-in and signed-out users can see with the [prebuilt control components](/docs/components/overview#what-are-control-components). Create a header using the following components:
You can control which content signed-in and signed-out users can see with the [prebuilt control components](/docs/components/overview#what-are-control-components). The following example creates a header using the following components:

- [`<SignedIn>`](/docs/components/control/signed-in): Children of this component can only be seen while **signed in**.
- [`<SignedOut>`](/docs/components/control/signed-out): Children of this component can only be seen while **signed out**.
Expand Down Expand Up @@ -195,7 +195,10 @@ description: Add authentication and user management to your React app with Clerk

## Next step: Add routing with React Router

React has many options for handling routing, and you are free to choose the option that suits you best. If you would like to learn how to integrate React Router's latest Data API-based router (also known as Data Router), see the [dedicated guide](/docs/references/react/add-react-router).
React Router can be integrated with Clerk in two ways:

- As a framework: Use Clerk's built-in [React Router integration](/docs/quickstarts/react-router)
- As a library: Manually integrate React Router into your Clerk application using [library mode](/docs/references/react-router/library-mode)

## More resources

Expand Down
4 changes: 2 additions & 2 deletions docs/references/nextjs/auth-object.mdx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
title: '`Auth` object'
title: Auth object
description: The Auth object contains information about the current user's session.
---

The `Auth` object contains important information like the current user's session ID, user ID, and organization ID. It also contains methods to check for permissions and retrieve the current user's session token. It's returned by the [`useAuth()`](/docs/references/react/use-auth) hook, the [`auth()`](/docs/references/nextjs/auth) and [`getAuth()`](/docs/references/nextjs/get-auth) helpers, and the `request` object in server contexts.
The `Auth` object contains important information like the current user's session ID, user ID, and organization ID. It also contains methods to check for permissions and retrieve the current user's session token. It's returned by the [`useAuth()`](/docs/references/react/use-auth) hook, the [`auth()`](/docs/references/nextjs/auth) and `getAuth()` helpers, and the `request` object in server contexts.

## `Auth` object properties

Expand Down
Loading

0 comments on commit bd1a2b5

Please sign in to comment.