Learn how to use Clerk to quickly and easily add secure authentication and user management to your Remix application. This guide assumes that you are using Remix v2 or later.
Note
The next major version of the Clerk Remix SDK is coming soon! If you're just starting a project, consider trying out the Core 2 beta instead.
Once you have a Remix application ready, you need to install Clerk's Remix SDK. This gives you access to our prebuilt components and hooks for Remix applications.
Pro tip! If you are signed into your Clerk Dashboard, your secret key should become visible by clicking on the eye icon. Otherwise, you can find your keys in the Clerk Dashboard on the API Keys page.
To configure Clerk in your Remix application, you will need to update your root loader. This will enable you to have access to authentication state in any Remix routes.
If you need to load in additonal data, you can pass your loader directly to the rootAuthLoader.
Clerk provides a ClerkApp wrapper to provide the authentication state to your React tree. This helper works with Remix SSR out-of-the-box and follows the "higher-order component" paradigm.
In addition to the Account Portal pages, Clerk also offers a set of prebuilt components that you can use instead to embed sign-in, sign-up, and other user management functions into your Remix application. We are going to use the <SignIn /> and <SignUp /> components by utilizing the Remix optional catch-all route.
The functionality of the components are controlled by the instance settings you specify in your Clerk Dashboard.
Next, add environment variables for the signIn, signUp, afterSignUp, and afterSignIn paths:
These values control the behavior of the components when you sign in or sign up and when you click on the respective links at the bottom of each component.
Clerk offers Control Components that allow you to protect your pages. These components are used to control the visibility of your pages based on the user's authentication state.
To protect your routes, you can use the the loader to check for the userId singleton. If it doesn't exist, redirect your user back to the sign-in page.
Clerk provides a set of hooks and helpers that you can use to access the active session and user data in your Remix application. Here are examples of how to use these helpers.
The useAuth hook is a convenient way to access the current auth state. This hook provides the minimal information needed for data-loading and helper methods to manage the current active session.
The useUser hook is a convenient way to access the current user data where you need it. This hook provides the user data and helper methods to manage the current active session.
The getAuth() helper allows you to access the Authobject, including the current userId. You can use this helper to protect your loader function or get data for the initial render of the route.
The getAuth() helper allows you to access the Authobject. You can use the returns from getAuth() to protect the action function, update the user or perform mutations on data in your database.