Create your first plugin
In this guide, we’ll walk you through the steps of creating your first Better Auth plugin.
This guide assumes you have setup the basics of Better Auth and are ready to create your first plugin.
Plan your idea
Before beginning, you must know what plugin you intend to create.
In this guide, we’ll create a birthday plugin to keep track of user birth dates.
Server plugin first
Better Auth plugins operate as a pair: a server plugin and a client plugin. The server plugin forms the foundation of your authentication system, while the client plugin provides convenient frontend APIs to interact with your server implementation.
You can read more about server/client plugins in our documentation.
Creating the server plugin
Go ahead and find a suitable location create an your birthday plugin folder, with an index.ts
file within.
In the index.ts
file, we’ll export a function that represents our server plugin.
This will be what we will later add to our plugin list in the auth.ts
file.
Although this does nothing, you have technically just made yourself your first plugin, congratulations! 🎉
Defining a schema
In order to save each user’s birthday data, we must create a schema on top of the user
model.
By creating a schema here, this also allows Better Auth’s CLI to generate the schemas required to update your database.
You can learn more about plugin schemas here.
Authorization logic
For this example guide, we’ll setup authentication logic to check and ensure that the user who signs-up is older than 5. But the same concept could be applied for something like verifying users agreeing to the TOS or anything alike.
To do this, we’ll utilize Hooks, which allows us to run code before
or after
an action is performed.
In our case we want to match any requests going to the signup path:
And for our logic, we’ll write the following code to check the if user’s birthday makes them above 5 years old.
Authorized! 🔒
We’ve now successfully written code to ensure authorization for users above 5!
Client Plugin
We’re close to the finish line! 🏁
Now that we have created our server plugin, the next step is to develop our client plugin. Since there isn’t much frontend APIs going on for this plugin, there isn’t much to do!
First, let’s create our client.ts
file first:
Then, add the following code:
What we’ve done is allow the client plugin to infer the types defined by our schema from the server plugin.
And that’s it! This is all it takes for the birthday client plugin. 🎂
Initiate your plugin!
Both the client
and server
plugins are now ready, the last step is to import them to both your auth-client.ts
and your server.ts
files respectively to initiate the plugin.
Server initiation
Client initiation
Oh yeah, the schemas!
Don’t forget to add your birthday
field to your user
table model!
Or, use the generate
CLI command:
Wrapping Up
Congratulations! You’ve successfully created your first ever Better Auth plugin. We highly recommend you visit our plugins documentation to learn more information.
If you have a plugin you’d like to share with the community, feel free to let us know through our Discord server, or through a pull-request and we may add it to the community-plugins list!