Skip to content

Instantly share code, notes, and snippets.

@exonomyapp
Created August 10, 2024 10:21
Show Gist options
  • Save exonomyapp/a16383cd35d38227f052fce79a1223b6 to your computer and use it in GitHub Desktop.
Save exonomyapp/a16383cd35d38227f052fce79a1223b6 to your computer and use it in GitHub Desktop.
Nuxt Auth for Google and GitHub Authentication

In a Nuxt.js application, the @nuxtjs/auth module (commonly referred to as Nuxt Auth) plays a critical role in managing authentication and securing your application. When you want to support authentication for both Google and GitHub users, Nuxt Auth simplifies the integration by providing a unified system for handling authentication, token management, and user sessions.

Role of Nuxt Auth

  1. Abstraction of Authentication Providers:

    • Nuxt Auth abstracts the complexities of dealing with multiple authentication providers, like Google and GitHub. It provides pre-built strategies that you can easily configure to enable OAuth authentication with these providers.
  2. Token Management:

    • Nuxt Auth handles token storage and management, ensuring that your application correctly handles user sessions, token expiration, and refreshing tokens as needed. This is crucial for maintaining a secure and seamless user experience.
  3. User State Management:

    • Nuxt Auth manages the user state, allowing you to easily access authenticated user information (like their profile details) throughout your application. This information can be used to customize the UI or make authorized API calls.
  4. Route Protection:

    • The module provides an easy way to protect certain routes or pages in your application, ensuring that only authenticated users can access specific parts of your app.
  5. Configuration and Customization:

    • Nuxt Auth allows you to configure various authentication strategies, including OAuth2 for Google and GitHub. You can customize the behavior, such as redirect URLs, scopes, and how tokens are handled, according to your application's needs.

How to Use Nuxt Auth with Google and GitHub

Here’s a high-level overview of how you can set up Nuxt Auth for Google and GitHub authentication:

  1. Install Nuxt Auth:

    npm install @nuxtjs/auth-next @nuxtjs/axios
  2. Add Nuxt Auth and Axios Modules: In your nuxt.config.js:

    export default {
      modules: [
        '@nuxtjs/axios',
        '@nuxtjs/auth-next'
      ],
      auth: {
        strategies: {
          google: {
            clientId: 'YOUR_GOOGLE_CLIENT_ID',
            codeChallengeMethod: '',
            responseType: 'token id_token',
            redirectUri: 'YOUR_REDIRECT_URI'
          },
          github: {
            clientId: 'YOUR_GITHUB_CLIENT_ID',
            clientSecret: 'YOUR_GITHUB_CLIENT_SECRET',
            codeChallengeMethod: '',
            redirectUri: 'YOUR_REDIRECT_URI'
          }
        }
      }
    }
  3. Configure Redirect URIs:

    • Make sure that the redirect URIs specified in your Google and GitHub developer consoles match those configured in nuxt.config.js.
  4. Use Auth in Your Application:

    • Use Nuxt Auth’s methods and properties in your components to manage login, logout, and accessing the user state:
    <template>
      <div>
        <button @click="$auth.loginWith('google')">Login with Google</button>
        <button @click="$auth.loginWith('github')">Login with GitHub</button>
      </div>
    </template>
  5. Protect Routes:

    • To protect a route, you can set the auth property in the page component:
    export default {
      auth: true
    }
    • For more advanced use, you can define different levels of access based on the user’s authentication status.

By leveraging Nuxt Auth, you can easily support multiple authentication methods in your application with minimal configuration, while maintaining secure and user-friendly authentication flows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment