Better Auth can be easily integrated with Remix. This guide will show you how to integrate Better Auth with Remix.
You can follow the steps from installation to get started or you can follow this guide to make it the Remix-way.
If you have followed the installation steps, you can skip the first step.
Create a file named auth.server.ts
in one of these locations:
- Project root
lib/
folder
utils/
folder
You can also nest any of these folders under app/
folder. (e.g. app/lib/auth.server.ts
)
And in this file, import Better Auth and create your instance.
Make sure to export the auth instance with the variable name auth
or as a default
export.
We need to mount the handler to a API route. Create a resource route file api.auth.$.ts
inside app/routes/
directory. And add the following code:
You can change the path on your better-auth configuration but it's recommended to keep it as routes/api.auth.$.ts
Create a client instance. Here we are creating auth.client.ts
file inside the lib/
directory.
Once you have created the client, you can use it to sign up, sign in, and perform other actions.