next-auth vs @auth0/auth0-react
認証と認可
next-auth@auth0/auth0-react類似パッケージ:
認証と認可

認証と認可は、アプリケーションやシステムにおいてユーザーの身元を確認し、特定のリソースや機能へのアクセスを制御するプロセスです。認証はユーザーが誰であるかを確認するプロセスであり、通常はユーザー名とパスワード、または生体認証などの資格情報を使用します。一方、認可は認証されたユーザーが特定のリソースやアクションにアクセスする権限を持っているかどうかを判断するプロセスです。これにより、機密情報や重要な機能へのアクセスを制限し、セキュリティを確保します。

npmのダウンロードトレンド
3 年
GitHub Starsランキング
統計詳細
パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
next-auth2,405,30827,872824 kB5661ヶ月前ISC
@auth0/auth0-react814,3159721.2 MB205日前MIT
機能比較: next-auth vs @auth0/auth0-react

認証プロバイダーのサポート

  • next-auth:

    next-authは、複数のソーシャル認証プロバイダー(Google、Facebook、Twitterなど)や、メールリンク認証、OAuth、OpenID Connectなど、幅広い認証方法をサポートしています。カスタムプロバイダーの追加も容易です。

  • @auth0/auth0-react:

    @auth0/auth0-reactは、Auth0プラットフォームを介して多様な認証プロバイダー(ソーシャル、企業、カスタム)をサポートします。特に、シングルサインオン(SSO)や多要素認証(MFA)など、高度な機能も簡単に設定できます。

カスタマイズ性

  • next-auth:

    next-authは、認証フローやセッション管理、コールバック関数などをコードレベルで自由にカスタマイズできます。特に、データベース接続やセッションの保存方法を自分で実装することができるため、柔軟性が高いです。

  • @auth0/auth0-react:

    @auth0/auth0-reactは、Auth0のダッシュボードから認証フローやUIをカスタマイズできますが、コードレベルでのカスタマイズは制限されています。特に、ログインフォームやエラーメッセージのスタイルを変更することはできますが、フロー全体を変更することは難しいです。

セッション管理

  • next-auth:

    next-authは、セッションをデータベース、JWT、またはクッキーに保存する方法を選択できます。特に、セッションの有効期限や更新方法を細かく設定できるため、より柔軟な管理が可能です。

  • @auth0/auth0-react:

    @auth0/auth0-reactは、Auth0が提供するセッション管理を使用します。セッションはブラウザのクッキーに保存され、Auth0のサーバーで管理されるため、セキュリティが高く、スケーラブルです。ただし、セッションのカスタマイズや長さの設定は、Auth0のダッシュボードで行う必要があります。

エラーハンドリング

  • next-auth:

    next-authは、認証フロー中のエラーをキャッチし、カスタムエラーハンドリングを実装することができます。特に、コールバック関数を使用してエラーをログに記録したり、ユーザーに通知したりすることが容易です。

  • @auth0/auth0-react:

    @auth0/auth0-reactは、Auth0のエラーハンドリング機能を利用します。認証エラーやAPIエラーは、Auth0のサーバーから返されるため、エラーメッセージやステータスコードを元に適切に処理する必要があります。特に、エラーの種類に応じてカスタムメッセージを表示することが推奨されます。

Ease of Use: Code Examples

  • next-auth:

    next-authを使用した認証の基本的な実装例

    import NextAuth from 'next-auth';
    import Providers from 'next-auth/providers';
    
    export default NextAuth({
      providers: [
        Providers.Google({
          clientId: process.env.GOOGLE_CLIENT_ID,
          clientSecret: process.env.GOOGLE_CLIENT_SECRET,
        }),
      ],
      database: process.env.DATABASE_URL,
    });
    
  • @auth0/auth0-react:

    @auth0/auth0-reactを使用した認証の基本的な実装例

    import React from 'react';
    import { Auth0Provider } from '@auth0/auth0-react';
    
    const App = () => {
      return (
        <Auth0Provider
          domain="YOUR_AUTH0_DOMAIN"
          clientId="YOUR_AUTH0_CLIENT_ID"
          redirectUri={window.location.origin}
        >
          <YourComponent />
        </Auth0Provider>
      );
    };
    
    export default App;
    
選び方: next-auth vs @auth0/auth0-react
  • next-auth:

    next-authを選択するのは、Next.jsアプリケーションにシームレスに統合できるオープンソースの認証ライブラリが必要な場合です。特に、カスタマイズ性が高く、複数の認証プロバイダー(Google、Facebook、GitHubなど)やメールリンク認証を簡単に設定できます。また、データベースやJWTを使用したセッション管理が可能で、より細かい制御ができます。

  • @auth0/auth0-react:

    @auth0/auth0-reactを選択するのは、Auth0プラットフォームを使用して迅速に認証を実装したい場合です。特に、ソーシャルログインや企業ログイン、シングルサインオン(SSO)など、多様な認証方法を簡単に統合できます。Auth0のダッシュボードから設定を管理できるため、柔軟性とスケーラビリティがあります。

next-auth のREADME


NextAuth.js

Authentication for Next.js

Open Source. Full Stack. Own Your Data.

Release Bundle Size Downloads Github Stars Github Stable Release

Overview

NextAuth.js is a complete open source authentication solution for Next.js applications.

It is designed from the ground up to support Next.js and Serverless.

This is a monorepo containing the following packages / projects:

  1. The primary next-auth package
  2. A development test application
  3. All @next-auth/*-adapter packages
  4. The documentation site

Getting Started

npm install next-auth

The easiest way to continue getting started, is to follow the getting started section in our docs.

We also have a section of tutorials for those looking for more specific examples.

See next-auth.js.org for more information and documentation.

Features

Flexible and easy to use

  • Designed to work with any OAuth service, it supports OAuth 1.0, 1.0A and 2.0
  • Built-in support for many popular sign-in services
  • Supports email / passwordless authentication
  • Supports stateless authentication with any backend (Active Directory, LDAP, etc)
  • Supports both JSON Web Tokens and database sessions
  • Designed for Serverless but runs anywhere (AWS Lambda, Docker, Heroku, etc…)

Own your own data

NextAuth.js can be used with or without a database.

  • An open source solution that allows you to keep control of your data
  • Supports Bring Your Own Database (BYOD) and can be used with any database
  • Built-in support for MySQL, MariaDB, Postgres, Microsoft SQL Server, MongoDB and SQLite
  • Works great with databases from popular hosting providers
  • Can also be used without a database (e.g. OAuth + JWT)

Secure by default

  • Promotes the use of passwordless sign-in mechanisms
  • Designed to be secure by default and encourage best practices for safeguarding user data
  • Uses Cross-Site Request Forgery (CSRF) Tokens on POST routes (sign in, sign out)
  • Default cookie policy aims for the most restrictive policy appropriate for each cookie
  • When JSON Web Tokens are enabled, they are encrypted by default (JWE) with A256GCM
  • Auto-generates symmetric signing and encryption keys for developer convenience
  • Features tab/window syncing and session polling to support short lived sessions
  • Attempts to implement the latest guidance published by Open Web Application Security Project

Advanced options allow you to define your own routines to handle controlling what accounts are allowed to sign in, for encoding and decoding JSON Web Tokens and to set custom cookie security policies and session properties, so you can control who is able to sign in and how often sessions have to be re-validated.

TypeScript

NextAuth.js comes with built-in types. For more information and usage, check out the TypeScript section in the documentation.

Example

Add API Route

// pages/api/auth/[...nextauth].js
import NextAuth from "next-auth"
import AppleProvider from "next-auth/providers/apple"
import GoogleProvider from "next-auth/providers/google"
import EmailProvider from "next-auth/providers/email"

export default NextAuth({
  secret: process.env.SECRET,
  providers: [
    // OAuth authentication providers
    AppleProvider({
      clientId: process.env.APPLE_ID,
      clientSecret: process.env.APPLE_SECRET,
    }),
    GoogleProvider({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_SECRET,
    }),
    // Sign in with passwordless email link
    EmailProvider({
      server: process.env.MAIL_SERVER,
      from: "<no-reply@example.com>",
    }),
  ],
})

Add React Hook

The useSession() React Hook in the NextAuth.js client is the easiest way to check if someone is signed in.

import { useSession, signIn, signOut } from "next-auth/react"

export default function Component() {
  const { data: session } = useSession()
  if (session) {
    return (
      <>
        Signed in as {session.user.email} <br />
        <button onClick={() => signOut()}>Sign out</button>
      </>
    )
  }
  return (
    <>
      Not signed in <br />
      <button onClick={() => signIn()}>Sign in</button>
    </>
  )
}

Share/configure session state

Use the <SessionProvider> to allow instances of useSession() to share the session object across components. It also takes care of keeping the session updated and synced between tabs/windows.

import { SessionProvider } from "next-auth/react"

export default function App({
  Component,
  pageProps: { session, ...pageProps },
}) {
  return (
    <SessionProvider session={session}>
      <Component {...pageProps} />
    </SessionProvider>
  )
}

Security

If you think you have found a vulnerability (or not sure) in NextAuth.js or any of the related packages (i.e. Adapters), we ask you to have a read of our Security Policy to reach out responsibly. Please do not open Pull Requests/Issues/Discussions before consulting with us.

Acknowledgments

NextAuth.js is made possible thanks to all of its contributors.

Contributing

We're open to all community contributions! If you'd like to contribute in any way, please first read our Contributing Guide.

License

ISC