Lucia is an auth library for server that abstracts away the complexity of handling sessions. It is a simple and easy to use library that provides a strong support of database out of the box.
It has built in adapters for various databases like MongoDB, Postgres, SQLite, and MySQL. Which makes it easy to use with any database and with less configuration and full control over the user data. Unlike other libraries where there are many configurations and setup required and things are very abstracted which makes it difficult to understand and use. Lucia gives start to end control over the user data and session management.
It has strong TypeScript support and i works well with all the major runtime like Node.js, Deno, and Bun.
First, we need to create a new Next.js app. You can do this by running the following command
Next, we need to install Lucia and the MongoDB adapter. You can do this by running the following command.
Lucia use oslo for session management and @lucia-auth/adapter-mongodb for MongoDB adapter. We also need to install mongoose to connect to the MongoDB database.
Next, we need to configure MongoDB. You can do this by creating a new file called mongoose.ts and adding the following code.
This code will connect to the MongoDB database and cache the connection. Since its recommended to cache the connection to the database to avoid creating a new connection for every request.
Next we need to create Model for the User and Session. You can do this by creating a new file called user.ts and adding the following code
We just configured the User and Session model and created an adapter for Lucia. The adapter will be used to store the session data in the MongoDB database.
Since Lucia give us full control over how we wanna store the session data and user data. You can configure it according to your need.
Creating Rest API on BUN with ElysiaJS
Elysia is a simple, type-safe, high-performance framework optimized for Bun and WinterCG compliant, enabling it to run directly in your browser.
Next, we need to create a new file called auth.ts and add the following code
We just created a new instance of Lucia where we are passing our adapter and some configuration. We also created a function called validateRequest which will be used to validate the request and get the user and session data.
And thats pretty much it. We have successfully setup Lucia and now we are ready to use Login, Logout, Signup and Protected routes in our Next.js app.
We can use server action to register the user. You can do this by creating a new page /app/register/page.tsx.
Here as you can we are using server action to register the user. We are hashing the password using Argon2id and then creating a session for the user and setting the session cookie.
You can handle the registration action according to your need and use any library to validate the form data.
We can use server action to login the user. You can do this by creating a new page /app/login/page.tsx.
I have just created a simple login action where we are validating the user and creating a session for the user.
You can handle the login action according to your need and use any library to validate the form data. and also you can handle validation for existing user and password.
Right now we are protecting the routes using lucia validateRequest function. But since Next.Js give us middleware and lucia setting cookie on the client side. We can use cookie to protect the routes in middleware.
It advised by Lucia to configure Next.config.ts to prevent Oslo to getting
bundled. You can do this by adding the following code.
I Found Lucia to be a very simple and easy to use library for authentication. It gives us full control over the user data and session management. There are several provider for OAuth and other social logins. It has strong TypeScript support and works well with all the major runtime like Node.js, Deno, and Bun.
I hope you found this article helpful. If you have any questions or feedback, feel free to comment below. You can found the complete code on my GitHub.
Comments (10)