Docs

Use Clerk with RedwoodJS

Learn how to use Clerk to quickly and easily add secure authentication and user management to your RedwoodJS application.

terminal
npm create redwood-app my-redwood-project --typescript
terminal
yarn create redwood-app my-redwood-project --typescript
terminal
pnpm create redwood-app my-redwood-project --typescript

Set environment variables

Below is an example of an .env.local file.

Pro tip! If you're signed into the Clerk Dashboard, your Secret Key should become visible by clicking on the eye icon. Otherwise, you can find your keys on the API keys page in the Clerk Dashboard.

.env.local
CLERK_PUBLISHABLE_KEY=YOUR_PUBLISHABLE_KEY
CLERK_SECRET_KEY=YOUR_SECRET_KEY
redwood.toml
[web]
  includeEnvironmentVariables = ['CLERK_PUBLISHABLE_KEY']

Set up Redwood auth

The next step is to run a Redwood CLI command to install the required packages and generate some boilerplate code:

my-redwood-project
yarn rw setup auth clerk --force

Note

The --force flag is needed to overwrite the existing dbAuth logic, that is set by default.

You can now access Clerk functions through the Redwood useAuth() hook, which is exported from src/web/auth.tsx, or you can use the Clerk components directly.

Protecting your pages

Below is an example of using the useAuth() hook to verify if the user is authenticated. This will open a modal for your user to sign in to their account.

index.tsx
import { useAuth } from '../../auth'

const HomePage = () => {
  const { currentUser, isAuthenticated, logIn, logOut } = useAuth()

  return (
    <>
      {isAuthenticated ? (
        <>
          <button onClick={logOut}>Log out</button>
          <h1>Hello {currentUser.firstName}</h1>
        </>
      ) : (
        <button onClick={logIn}>Log in</button>
      )}
    </>
  )
}

export default HomePage
index.tsx
import { SignInButton, UserButton } from '@clerk/clerk-react'
import { useAuth } from '../../auth'

const HomePage = () => {
  const { currentUser, isAuthenticated } = useAuth()

  return (
    <>
      {isAuthenticated ? (
        <>
          <UserButton />
          <h1>Hello {currentUser.firstName}</h1>
        </>
      ) : (
        <SignInButton mode="modal" />
      )}
    </>
  )
}

Next steps

Now that you have an application integrated with Clerk, you will want to read the following documentation:

Customization & Localization

Learn how to customize and localize the Clerk components.

Authentication Components

Learn more about all our authentication components.

Client Side Helpers

Learn more about our client-side helpers and how to use them.

Feedback

What did you think of this content?

Last updated on