Overview:
nuxt-auth is an open-source authentication module designed for Nuxt 3 applications. It supports both server-side rendered (SSR) and static Nuxt applications. The module provides authentication functionality for any universal Nuxt 3 application by supporting two providers: Auth.js / NextAuth.js and local for static pages with an external backend. It offers a range of features including support for various authentication providers, session management, middleware protection, and a REST API.
Features:
- Authentication providers: Supports OAuth providers (e.g., Github, Google, Twitter, Azure), custom OAuth, and credential-based authentication.
- Email Magic URLs: Enables authentication using email magic links.
- Isomorphic / Universal Auth: Provides a composable
useAuth
hook with methods for session retrieval, CSRF token retrieval, provider management, sign-in, and sign-out. - Middleware protection: Allows protection of application-side and server-side middleware and endpoints.
- Session life-cycle management: Offers advanced features for managing the session life-cycle, including periodic session refresh, session refresh on tab-refocus, one-time session fetch on page load, and persistent sessions across requests.
- Session broadcasting between tabs: Supports session broadcasting between different browser tabs.
- REST API: Provides a REST API with endpoints for sign-in, sign-out, session retrieval, CSRF token retrieval, and provider retrieval.
Installation:
To install the nuxt-auth module, follow these steps:
Add the module to your Nuxt 3 project using npm or yarn:
npm install nuxt-auth
or
yarn add nuxt-auth
Configure the module in your Nuxt 3 project by adding it to the
buildModules
array in thenuxt.config.js
file:export default { // ... buildModules: [ 'nuxt-auth', ], }
Configure the authentication providers, session management, and other settings in the
auth
object in thenuxt.config.js
file:export default { // ... auth: { strategies: { // Configure authentication providers here }, session: { // Configure session management here }, }, }
Use the provided
useAuth
hook in your Nuxt 3 components to access the authentication functionality:import { useAuth } from 'nuxt-auth' export default { setup() { const { signIn, signOut, data } = useAuth() // Use the authentication methods and data here }, }
Summary:
nuxt-auth is a powerful authentication module for Nuxt 3 applications. It supports various authentication providers, session management, middleware protection, and a REST API. It is suitable for both SSR and static Nuxt applications. With its comprehensive features and easy-to-use APIs, nuxt-auth simplifies the process of implementing authentication in Nuxt 3 projects.