What is the idiomatic Nuxt v3.16 way of Authentication/Authorization ?
I am trying Better-Auth, but its been a real challenge. I am still new to Nuxt and I am still learning. What are the security frameworks/utilities that work great with Nuxt? I don't want to use a third party service. I plan on keeping all of my users, accounts, roles, permissions in my database.
Sugggestions?
2 Replies
Hi @Howler. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
For Nuxt 3.16, the recommended approach for authentication and authorization is using the
nuxt-auth-utils
module, which provides utilities for managing both client-side and server-side session data without requiring a third-party service.
Using nuxt-auth-utils
This official module allows you to:
- Store user session data in secured & sealed cookies
- Protect routes on both client and server sides
- Manage authentication state easily
Installation
This will install the module and add it to your nuxt.config.ts
file Docs > Guide > Recipes > Sessions and Authentication.
Basic Setup
1. Set up cookie encryption:
Add a secure password to your .env
file:
2. Create login API endpoint:
3. Protect API routes:
4. Protect client-side routes with middleware:
5. Apply middleware to your pages:
Working with Your Database
Since you want to keep users, accounts, roles, and permissions in your database:
1. You can use Nitro SQL Database or NuxtHub SQL Database for database integration
2. Implement password hashing using the utilities provided by nuxt-auth-utils
3. Create custom middleware to check for specific roles/permissions
Example Implementation
For a complete implementation example, you can check out the open-source atidone repository which demonstrates a Nuxt app with authentication, database integration, and CRUD operations Docs > Guide > Recipes > Sessions and Authentication > Conclusion.
If you're experiencing challenges with Better-Auth, switching to nuxt-auth-utils
might provide a more straightforward experience as it's specifically designed for Nuxt 3 and maintained by the Nuxt team.
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@391433788590391297> Kapa.ai is still learning and improving, please let me know how I did by reacting below.