0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Next.js15×Auth0】[Error: Route "/api/auth/[auth0]" used params.auth0. params should be awaited before using its properties.

Posted at

2024/12/15現在、Auth0のメジャーバージョンではNext.js15対応ができていないため上記エラーが発生する。

対処方法

コード

ベータ版(@auth0/nextjs-auth0@4.0.0-beta.0)を使用する。

npm i @auth0/nextjs-auth0@4.0.0-beta.0

以下のファイルをプロジェクトルートに追加する

middleware.ts
import type { NextRequest } from "next/server"

import { auth0 } from "./lib/auth0"

export async function middleware(request: NextRequest) {
  return await auth0.middleware(request)
}

export const config = {
  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - _next/static (static files)
     * - _next/image (image optimization files)
     * - favicon.ico, sitemap.xml, robots.txt (metadata files)
     */
    "/((?!_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)",
  ],
}

プロジェクトルートにlibディレクトリを作成し、以下のファイルを追加する。

lib/auth0.ts
import { Auth0Client } from "@auth0/nextjs-auth0/server"

export const auth0 = new Auth0Client()

ログインボタンのリンク先を下記に変更。

<a href="/auth/login">xxx</a>

ログアウトボタンのリンク先を下記に変更。

<a href="/auth/logout">xxx</a>

Auth0側の設定

アプリケーションはRegular Web Applicationを使用し、設定を下記に変更する。

ログイン後のリダイレクト設定

先ほど作成したAuth0のクライアントをカスタマイズする。
以下はログイン後のリダイレクト先を/homeに設定している。

lib/auth0.ts
import { Auth0Client } from "@auth0/nextjs-auth0/server"

export const auth0 = new Auth0Client({
    signInReturnToPath: "/home",
})

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?