Syntax Video
8 Reasons To Try Better Auth
Intro
Adding authentication and authorization to your JavaScript and TypeScript apps should be easy. There should be no headaches. It should be customizable. You should be in control of the data, the user data. It should be free. You shouldn't have to pay anything for it. And with better off, I have gotten that exact experience. So throughout my years of building out applications with JavaScript and TypeScript, I've implemented O in a lot of different ways. In the early days, I was using Passport.js. I've also rolled my own off using various packages from npm for like hashing and creating JSON web tokens but kind of assembling to them together into my own system. Um I've also used uh next o and ojs and lucia o and I've also used thirdparty services that store the users in their own system and handle things like password resets and email verification. So, I've used services like Ozero and uh Firebase Off and Clerk. And throughout all of that, Better Off has been the best. It's in the name, but it's been the easiest to
integrate, easiest to customize. It's free. It's open source. It works in your own app. And in this video, I'm going to tell you eight reasons why I like Better Off and why you should maybe give it a try. That sounds good to you? Let's dive in. My name is CJ. Welcome to syntax. First reason to use better off is they support almost every single full stack framework and back-end framework in the JavaScript ecosystem. So in their docs you can see they support Astro Remix, Nex, Nux, Felkit, Solid Start, Tanstack Start. And then for just API backends, things like Hono and Fastify and Express and Alicia, Nitro and Nest.js and even they support Expo for like React Native.
Framework Support
So I've used it with Nex.js, Nux and Hono. Scott has used it with speltkit and in my experience it's almost exactly the same to set up for every one of these. The only thing that's different is how you set up those API endpoints depending on which framework you're integrating with. But the nice thing is I only had to learn better off once and then it's just a matter of going to the framework page to see how things are slightly different to integrate with that specific framework. So for example with Nex.js you create a catchall handler that just exports the two next.js handler and now you've got all those o endpoints set up. For Nux, very similar thing. You create a catch-all handler. For Hono, very similar. You just create a wildcard handler that defers to better off. Now, in terms of client side integration, it's just as easy. So, if you have a React frontend, there is a specific create o client that works with React. There's one that works with Vue, spelt, solid, and then of course vanilla.js if you don't want to use the framework specific client adapters. And on top of that, for front-end frameworks, they also expose hooks that you can use. So, for React, there's a use session hook. Same thing
for Vue, spelt, and solid. So if you've used any other O framework before, you'll feel right at home. Essentially, they have the stuff you need for the backend. They have the stuff you need for the front end. On top of that, they support pretty much every database you might be using. So MySQL, SQLite, Postgress, Microsoft SQL, uh other relational databases, and they also have adapters for Drizzle and Prisma. So you're pretty much covered if you're using any one of these as well as MongoDB. And if that doesn't cover you, then there's also community adapters. So you can see people have built adapters for convex and things like payload. And of course if your database isn't here, you could create your own database adapter. So no matter the backend tech or front-end tech or database that you're using, Better Oroth has you covered. The next thing I like about Better Off is they support email and password out of the box as well as any number of social providers that you might want to add. Now a lot of other do-it-yourself O frameworks kind of discourage email and password, but sometimes that's really the kind of O that you want in your app. Now, of course, if you're allowing users to sign up with their email, you want to verify
Email Verification + Password Reset
that they actually are in control of that email. Now, out of the box, there is no verification, but it's very simple to enable. So, right here inside of your Betteroth config, you can just have an email verification key and set the send verification email. And this method can do literally whatever you want. You can talk to an SMTP server. You can just log the URL to the console. It's up to you.
But the main idea is this token is what's used by the better off SDK to actually verify that user's email in the database. But you can do literally whatever you want inside of that function. So I'll show you how I set this up with Nex.js. So I have this example Nex.js app where uh users can sign up and manage all of the buzzwords for their company. They can create organizations, invite members to the teams, all enabled via Better O plugins.
So, for example, if I want to sign up as a new user, I can enter in all my info, click sign up, and you'll see with the way that I've integrated this, I show the user a confirmation that says, "Check your email because we sent you a verification link." Now, for local development, I'm just using ethereal email, which if you haven't heard of is pretty slick. Essentially, it just gives you a temporary username and password, and any emails that get sent via that username and password show up on this website, ethereal. So, this just sent an email to test@acample.com, but it didn't really go there. It just went to my dashboard here. And then we have the link we can click to actually verify our email address. So, we do this. And once we're verified, we're instantly logged in to the dashboard. So, Better Off doesn't tie you to any specific email provider or anything like that. For my app, I'm just using the MPM package called Node Mailer. And this lets you talk to any SMTP server. Inside this function is where the real work happens.
So, I've got this method called send verification email. Essentially, I render out an email. So, I'm using a package called React email. And this just lets you use JSX to construct what the email should be. But whenever you render it out, it actually converts it into HTML that's going to work across a lot of different email clients. But that creates the HTML that's going to be sent. We also have plain text in case the user is reading this email via plain text. And then my send email function creates a transporter. And this is something provided by node mailer. So, node mailer is a package that lets you talk to SMTP servers. And here you can see I pass in my SMTP credentials. So, like I mentioned, when I'm running locally, we're just using Ethereal email, but whenever I'm in production, we could talk to an SMTP server that's provided by Resend or AWS SCES or any other number of email providers. But essentially, this just sends that email.
But the key point being the verification URL is in that email. And so, after we've created that transporter, we can just use the sendmail method, pass in all of our stuffs. We're good to go. One extra thing I like to do when I'm working in development is use this get test message URL provided by node mailer. So ethereal email is a service that is associated with node mailer. And this actually is what allows me to have that preview URL that I can just click on and instantly see the email. So when I'm testing locally, I can just click on that link pretty easily. But like I mentioned, you can do literally whatever you want inside of this function. The main idea here is the token which is actually embedded in the URL is what's used by the better off library to actually mark a user's email as verified. Now, another thing you likely want to do if you're having users login with email and password is also be able to reset their password. And the setup for that is almost exactly the same.
There's just this method called send reset password and we can do whatever we want inside of there. So essentially we send an email but instead of having a verification link, it's a reset password link. So it takes the user to the reset password page. There's lots of other options you can pass in for email and password, but in my experience, this has been fantastic. I'm in control of all the users. They all live in my database next to the rest of my data, but we still have all the perks of what some of these hosted email providers give us, like email verification and reset password and all that good stuff. Now, for my app, verification links were perfectly fine, but some people don't like verification links because pretty often you might be like logging in on mobile and then you have to go to your email client to get the link and and there's like a bunch of hopping around.
OTP and Magic Links
One thing you could do instead is use their one-time password plugin. It works almost exactly the same. Essentially, once you've installed the plugin, you now have this SIN verification uh OTP and instead of a token, you now have an OTP. And from there, you can do the same thing. So instead of sending an email that has a link inside of it, you could send an email that has like a six-digit code. And then from there, you could implement a page where if the user enters in that OTP code that talks to the Better Off SDK and then verifies the user within your database. So tons of freedom here in terms of how you verify users. And beyond that, if you don't want to use passwords at all, they also have the magic link plugin. It works exactly the same way. You specify this call back here where you get the user's email, that token, and the URL, and then you do whatever you want. And the code would look almost exactly the same where we send that email, but instead of saying click this to verify, we say click this to login. So, in my experience, integrating email with better off has been super smooth. And this is really one of the reasons why I love it. Now, you might have thought that you needed some hosted or
thirdparty provider to provide this kind of flow for your users. But as long as you have access to some SMTP server, some server that can send emails, Better Off lets you integrate very easily and you stay in control of all the data. So all the users still live in your database. If you're running locally, you don't have to worry about talking to a third party API to get access to those users. So for all of the apps that I've been working on with Betteroth, it's made that super easy. Next thing some of you are going to maybe get a little bit scared at, but you can actually enable bearer token authentication. Now, a lot of other off frameworks in the JavaScript ecosystem essentially force you to use session cookies, but there are scenarios where session cookies will not work, especially if you have like a separate backend hosted on a different domain from your front end. Cross-domain cookies might not work for that scenario. Uh you might also be building out an API where you want any client to be able to talk to it. And for those scenarios, cookies will not work. But if you want to use bearer authentication,
Bearer Token / JWT
better off has a plugin and it just works out of the box. So you add the bearer plugin and then inside of your client side codes, there's an onsuccess callback and you can store that JSON web token in local storage or session storage if you want to be a little bit more secure and later on use that token when needed. So, Better Off allows you to set up some fetch options whenever you're setting up the uh O client. And inside of here, you can see anytime a fetch is happening through their SDK, it's going to automatically grab that token from local storage. So, you may not like this, but for me, this was absolutely awesome to see because like I said, I've used other off frameworks where this was essentially impossible or needed a ton of customization to get working. But if you need this, this plugin will work for you. And they even provide this this disclaimer here cuz by default it is using session cookies. But if you're putting tokens in local storage or session storage, you you better understand the ramifications of that. Uh but I do. And so for for some apps, that is the way that I like to
Hooks / Auth Middleware
implement O. The next thing I like about better off is how customizable it is without needing to read through their source code or uh create any sort of like weird overrides or anything like that. Their entire O system is based on hooks. And there are before hooks and after hooks. And inside of these hooks, you can basically do anything. You can detect if a user is signing up, if a user is logging in, if a user is resetting their password, if the session's getting created. You can do absolutely anything to modify what's happening before or after any one of these O events is happening. Now, in this simple example here, they only allow users to sign up that have an email that ends with a specific domain.
So, this is perfect if you're building internal tools and you essentially want to restrict what users can sign up, but you do want to enable sign up because maybe you you're not automatically creating a bunch of accounts for every user in your organization. This is a simple little hook that detects if the incoming body has an email, that means we're either like signing up or logging in. And if that email doesn't end with a specific domain, we're going to throw an error. So this is a fairly simple hook, but let's take a look at some of the hooks that I've implemented uh to just to see how custom this stuff gets. Now this is an example that I implemented in a Nux application that I was working on.
And essentially this had to do with server side rendering and also hydration errors. So one of the things about NXT is if an API endpoint returns anything that is not undefined, it will attempt to validate the client side hydration. And the issue was when I was working on this app, better off actually returned null whenever a request came into slashget session. And according to nux, null is not undefined. So I was getting hydration errors. And so to fix this, I just wrote a simple little after hook where if a request was retrieving the session, if that session did not exist, instead of the default behavior of returning null, I returned an object where session was null and user was null. And this worked for my needs. Now, the next integration has since fixed that. Instead of returning null, they now return undefined. But I didn't have to wait for better off to release a new version um or for people to decide on what was the best way to handle this. I
could just add this hook and now I was no longer getting hydration errors. And I customized all my code to handle the session being null and user being null instead of the overall object being null. Another example of a hook that I wrote is for this Nex.js app for resetting passwords. So, let's sign out for this test user and let's try resetting their password. So, we can send the reset link. All of that's using the the better off integration. And if we check our console, we should get access to that reset link. Here it is.
But one of the user experiences I really like is when a user resets their password, they should just be automatically logged in. I don't want them to have to go back to the sign-in page and enter in their new password. We already know what their password is because they just reset it. So, I set my app up so that once a user resets their password, it instantly logs them in and takes them to the dashboard. So, no need to uh go back to the sign-in page and manually enter in all that information.
Now, this isn't supported out of the box, but I wrote some hooks to do this. So, essentially, I have a before hook that detects if a request is coming into reset password, and if it is, I then grab the verification out of the database. And for that specific verification token, I attach the email to the body that's returned by Better Oroth because by default that email is not included. And so essentially once a user arrives on that forgot password page, we don't know who they are. We just have the verification token. So this before hook includes the email in the response. So if it was a valid reset password token, this email is going to get included. And of course, if that verification token didn't exist, it would just use the default response, which would likely be like token not found or or whatever else. So the before hook essentially attaches the email associated with that verification token.
And then the after hook says if this was a valid reset, we actually attach the email there as well. And so this is all I needed. And my front-end code now had access to the email. It had access to the user's reset password because they literally just typed it in. And so essentially on the front end after a reset password is successful that makes a request to sign in with their latest password and then the original logic of redirecting to the dashboard after login kicks in. So as you can see very customizable and again this is just something I wanted for my app. You might think of other scenarios where you might want to modify the response or or do something custom. You can do that with before and after hooks. Now another set of hooks you can tap into that let you customize the behavior of better off are database hooks. So hooks at the top level are all about the incoming and outgoing requests for the better off API. But database hooks allow you to tap in to when things are happening with a user, a session or an account. So just like with the other hooks, there's before hooks and after hooks for before
Database Hooks
a user is created or after a user is created. And in the before hooks, you can basically customize the data before it gets inserted into your database. So this is really great if you want to maybe attach some extra properties. Maybe you want to make some API calls and put some stuff associated with that user when the user gets created and put into the database. For my example, I wanted to make it so that the first user that signs up on my website automatically becomes an administrator.
And this is something you typically do for apps you're building that people like to self-host. So when they self-host, the first user that signs in instantly becomes the admin user. And this is a simple little before create hook. And so for this we just reach into the database count how many users there are and if there are no users before that user is getting created we set their role to admin because otherwise their role would have been set to member in a second. I'm going to talk about the admin plugin so you can see where all of this is coming from. But again this is just custom logic. You can customize this to your heart's content.
Essentially change what the data would be that gets inserted into the database before it gets inserted. And again this was so easy to customize. I've used other off frameworks and a lot of times you have to create like a custom database adapter or you have to do a hack where you're like modifying the database adapter functions directly. With these before and after hooks, it's super simple to just customize the behavior of what happens as Better Off is talking to your database. And that leads me to the next thing I like about Better Off is the admin plugin. So, this essentially gives you everything you would need to administer all of the users within your system. It's a built-in plugin. You just pass it to the plugins array on the backend better off instance and then also on the front-end better off client. And with that, you now have all the functions you would need to administer the users within your system. So you've got create user, list user, set roles, set password, ban user, unban user, list sessions, etc. And one of the things I really like, which you often need to do when you're maybe
Admin Plugin
you're working in a support role or you're trying to figure out what's going on with various users in your system, they also have a built-in way of impersonating users. So you can essentially just pass in any user ID and then that admin which is already logged in becomes that user. So they can navigate around as if they were logged in as that user. And this is awesome because the admin doesn't need to know those users passwords or anything like that. So this is really great for site admins to be able to debug and basically pretend to be some other user to see what they're seeing within their dashboard. And on top of that there is access control. So by default every user that gets created has the user role and then any user that has the admin role is going to be able to perform all of these admin functions like listing and deleting and updating users and impersonating users. And along with roles, they also have permissions. And permissions are resourcebased. And out of the box, there is the user resource and then all of the permissions associated with that user resource. So
Permissions / Access Control
an admin has the ability to create users, list users, set roles on users, ban, impersonate, etc. So it can perform all of these actions on the user resource and an admin can also list, revoke or delete sessions within the system. So that's the built-in permission system, but you can create more permissions on top of that. So in the example they have here, they're creating a resource called project. So let's say your app allows users to CRUD projects. You can define your resource name, we'll call it project, and then all of the actions that can be performed on it. And then from there, you create your roles and then assign the various permissions for those given resources.
So in this case, we have a custom user role where users can create projects. But our admin role has the ability to update projects, but users cannot update projects. And then you could come up with a custom role like uh let's say like project manager or something like that. And then they might have all of the permissions to perform on that specific resource. And in their example here, they're also giving that project manager the ability to ban users from the user resource. So it's highly customizable. I haven't played around with the customizations here just yet.
But what's great about this is once you have all of these roles and permissions set up, they have a built-in way of checking does an authenticated user have a specific permission. So this is something you might put in a middleware or you might put this code right before you're modifying or talking to your database and you can check to see does the current user have the permission to create a project or instead of passing in the user's ID, you can pass in the role of that given user. So this makes it really easy to integrate with your existing API routes. You could essentially just add this admin plugin, set up your roles and permissions, and then any API route you have, you would just add this at the top of that API route to check the permissions for the the incoming request for the loggedin user. So the admin plugin is fantastic.
I did a simple little demo here in my next.js app. I've just created a simple admin dashboard. I actually haven't implemented all of these different pages, but for now on the manage users page, you can just remove a user. And in the source codes, I'm literally just using the remove user method that's uh provided by better off. But we could add buttons that integrate with all of the other types of things that we can do with users like ban them or set their roles or whatever else. And all of that can be handled in my custom dashboard here. The next thing I really like about Betteroth is they have a built-in organization plugin. And so this essentially allows you to have organizations that have teams and then teams can have members. And then within an organization, you can have various roles like the admin of an organization, an owner, a member. And all of that is built in out of the box. And so after you've enabled the plug-in, you'll have the ability to create, read, update, delete organizations. So on the client side, you can do client.organization.create.
Organizations Plugin
And on the server side, you'll do o.api.create organization or list organizations. and from there you can start integrating it into your app. And on top of that, they support invitations for being added to an organization. So, I'm going to sound like a broken record, but basically this send invitation email method is exactly the same as when we're doing our one-time passwords or our verification links. Essentially, just send the email somewhere using something uh but you get access to the invite link, which is essentially a unique token for a user to be added to that organization. So in my app here you can see we have the syntax organization and then within the syntax organization right now we only have one member and we do have a list of buzzwords. But if I head into manage members I can now invite a new member to my organization. So I'm going to invite Wes. I'm going to make them just a member. They can't change the organization but they'll be able to see our list of buzzwords. And then I'll send that invite. So now Wes shows up as
a pending member. And now Wes will need to sign up on the site and then accept that invitation. So I'm Wes boss now. I'm gonna go sign up uh with this email and then I'll be able to click to be added to that organization. Okay. So Wes is here. He's logged in, you'll see that he doesn't have any organizations listed. But if he heads back to his email and clicks that uh join organization email, you'll see it has all the details. So we're joining the syntax organization invited by CJ and you're going to become a member. So when we click accept invite, it'll then show a confirmation. We'll say yes, accept that invitation. And now Wes is a member of the syntax organization. And you can see because Wes is not the owner, he doesn't have the ability to to manage the organization settings or anything like that. And he doesn't have the ability to create new lists of buzzwords, but he can view the existing list of buzzwords uh that was created by me within the system. But if we log out
of Wes's account and log in as me, you'll notice that I do have the ability to perform all of those actions and I have the ability to manage users within my organization. So we head in, we'll see we have the syntax or we can see that Wes has accepted that invite and they've joined the organization. But if we head back to our lists of buzzwords, you'll see that because I'm an owner of this organization, I have the ability to manage these lists of buzzwords. So I can add or remove or update lists within here. Whereas Wes is just a member of the organization. So all of that is enabled via the organization's plugin.
And beyond that, you have even finer grain control. So just like with the admin plugin, you have roles and permissions. The roles that get added whenever you enable this plugin are the owner of an organization, admin, and member. And then you have specific resources like the organization itself, the member resource, the invitation resource, and then all of the various permissions for those given resources.
But just like the admin plugin, you can set up custom permissions. So I can have a buzzword list resource and from there I can specify the operations that can be performed on a buzzword list and then I can set up my access control list and say that a member can only read the buzzword list but the admin and the owner can create update or delete the buzzword list. So it's highly customizable. You can create new roles, new permissions and then add whatever specific resources you have within your application that are beyond organizations and users. Essentially anything that you're create, reading, or updating or deleting, you can add as a custom key here inside of these roles that you define. And so this plugin is awesome for doing things like multi-tenant apps or really just setting up your app in a way where things are restricted based on various teams and organizations. And once you have this plug-in enabled and all of the database tables created, every organization has an organization ID. And so essentially from there, it's up to you to customize where you put this. And you'll see that
in my database, a buzzword list has an organization ID. So I have my my own checks internally to also make sure that when this gets created, it belongs to an organization and and from there no other organization is able to view this specific list. The last thing or things I like about Better Off is all the plugins that are available to you. And one of them that a lot of people might want to use, especially if you're building like software as a service, they have a Stripe plugin. So you can essentially add customer management, subscription management, and all of that automatically integrates back to Stripe via web hooks. And so once you've added this plugin, you can start integrating it into your app and then locking some things behind various subscriptions. One example of that is the organization plugin. So, if you've enabled both of these plugins, now you can use this method on organizations called allow user to create organization and then lock it down to only allow them to create it if they have a specific subscription plan. And once you've set up Stripe, you'll see that under subscription management, you can
Stripe / Other Plugins Available
basically set up what plans you have available. And this ties back to your Stripe dashboard. So, like the basic plan or the pro plan. And then in here, you can check it by plan name. So, this allows you to start monetizing your app. So you can maybe limit the number of users that are created for an organization or only allow a user to create one organization before they have to actually pay and and get a subscription. So the Stripe plugin lets you do that. They also have the Polar plugin. I've never used Polar for payments. I've only used Stripe, but it works in a very similar way. And then this whole other list of all the other things you can do. So you can add two-factor authentication. So on the login page, you can make it so that a user has to type in their O code from their O app. There's also an API key plugin. So imagine this. You set up the Stripe integration. So now users are paying. And then let's say you want to give users API access to their list of buzzwords. Now you can use the API key plugin to create API keys for users, but maybe lock down how many API keys they can create or lock down the number of remaining requests that a given API key
can use based on the subscription that they paid for. So all of this remaining refill expiration is handled within the API key plugin. But just like the other plugins, they have hooks, they have custom functions. So within there, you could actually reach in to see, okay, what subscription do they have from Stripe? Okay, based on that subscription, they're going to get this number of uh API requests per month. Or maybe you even lock down rate limiting.
So they have a built-in rate limiter for API keys. They also have a built-in rate limiter for the off system itself, which I didn't show you. But whenever you create an API key, you can pass in custom rate limit options. And again, right before this, you could query your database to see does the user have a subscription? If they do, set their rate limits accordingly. They also allow you to set up your better o server as an ooth provider for MCP clients. So, you can basically make it so that your app allows users to log in and then the MCP client can use that authentication token to call your API endpoints with the user that just logged in via that MCP login and a million other things. They also have captures. Of course, you want to have captions on your site. Uh, another thing is multi session. So many sites don't allow you to do this where basically be logged in with two different accounts at one time. They also give you open API documentation, which I didn't show. So, you add this plugin and then you instantly get uh some open API docs that show you all of the O endpoints. So, they've basically thought of everything. And then, if they
haven't thought of everything, you can customize things with the hooks like I talked about earlier, or just create a plugin. You can see there's some community plugins listed here or just search around on GitHub to see what plugins people have created. But uh in my experience, it's done everything that I've needed it to do. And when I needed to customize it, I was able to do that without any headaches. And it really just gives you a full-fledged O system that you're fully in control of. There's no monthly fees. You don't have to worry about like user limits. It absolutely lives up to the name. It is better off.
the best O system I've used in all my years of working with back-end JavaScript frameworks. All right, thank you so much for watching. That's it for this video. If you have tried Better Off before, let me know about your experience down in the comments. Um, also, if you're going to try Better Off based on this video, let me know as well. If you have any comments, questions, leave it down below. And I will see you in the next one.
Thanks!
Quote it with credit and a link. Quoting Syntax