@auth0/nextjs-auth0 - 如何使用 App router 在 nextjs-auth0 handleAuth 函数中获取 Nextjs api 请求查询参数

问题描述 投票:0回答:1

在 Nextjs 应用程序中,我使用 @auth0/nextjs-auth0 中的handleAuth 函数,使用应用程序路由器,并希望将动态电子邮件地址传递给handleLogin 函数。我可以使用 login_hint 参数传递预定义的电子邮件,并使用硬编码的电子邮件常量对此进行了测试。

import { handleAuth, handleLogin, handleLogout } from '@auth0/nextjs-auth0';

const email = '[email protected]'

export const GET = handleAuth({
    logout: handleLogout({ returnTo: '/login' }),
    login: handleLogin({ authorizationParams: { login_hint: email }, returnTo: '/profile' }),
});

我现在需要从 API URL 检索查询参数,以便我可以传入动态电子邮件;但是,我无法弄清楚获取查询参数的语法。

有人知道如何在handleAuth函数中获取请求对象吗?

我尝试在handleAuth函数中获取API请求对象,但它一直出错

next.js query-string auth0 app-router
1个回答
0
投票

当用户注册帐户时,我必须访问 handleAuth 函数中的请求对象才能获取包含电子邮件的查询参数。我是这样做的:

import { handleAuth, handleLogin } from "@auth0/nextjs-auth0";

export const GET = handleAuth({
  login: handleLogin({
    authorizationParams: { prompt: "login" },
    returnTo: "/dashboard",
  }),
  signup: handleLogin((req) => {
    const url = new URL(req.url ?? "");
    const email = url.searchParams.get("email") ?? "";
    return {
      returnTo: "/dashboard",
      authorizationParams: {
        prompt: "login",
        screen_hint: "signup",
        login_hint: email,
      },
    };
  }),
});
© www.soinside.com 2019 - 2024. All rights reserved.