Gary Voss
Gary Voss

Reputation: 11

@auth0/nextjs-auth0 - How to get Nextjs api request query parameters within nextjs-auth0 handleAuth function, using App router

Within a Nextjs app, I am using the handleAuth function in @auth0/nextjs-auth0, using the App router, and want to pass a dynamic email address to the handleLogin function. I can pass a predefined email using the login_hint param and have tested this with a hard-coded email const.

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' }),
});

I now need to retrieve the query parameter from the API URL so that I can pass in dynamic emails; however, I can’t figure out the syntax to get the query parameters.

Does anyone know how to get the request object within the handleAuth function?

I tried to get the API request object within the handleAuth function but it keeps erroring

Upvotes: 1

Views: 257

Answers (1)

Kevin Barreiro
Kevin Barreiro

Reputation: 39

I had to access the request object within the handleAuth function to get a query parameter containing an email when a user is signing up for an account. Here's how I did it:

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,
      },
    };
  }),
});

Upvotes: 0

Related Questions