# How to Use handleLogin with @auth0/nextjs-auth0 in Next.js

**URL:** <https://community.auth0.com/t/how-to-use-handlelogin-with-auth0-nextjs-auth0-in-next-js/116900>\
**Category:** Get Help\
**Tags:** clients, management-api\
**Created:** [September 22, 2023, 12:30am UTC](https://community.auth0.com/t/how-to-use-handlelogin-with-auth0-nextjs-auth0-in-next-js/116900 "2023-09-22T00:30:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hiroshi0900](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/hiroshi0900/32/51086_2.png) [@Hiroshi0900](https://community.auth0.com/u/Hiroshi0900)\
**Post date:** [September 22, 2023, 12:30am UTC](https://community.auth0.com/t/how-to-use-handlelogin-with-auth0-nextjs-auth0-in-next-js/116900/1 "2023-09-22T00:30:39Z")

</div>

Hello.  
I am currently developing an application with the following architecture:

- Frontend
  - Next.js: 13.4.8
  - @auth0/nextjs-auth0: ^3.1.0

- Backend
  - Go: 1.20
  - Echo: v4.10.2

I am trying to implement the following authentication flow, but I am encountering issues:

1. Click the login button on the frontend.
2. Redirect to Auth0’s authentication page.
3. After successful authentication in Auth0, redirect back to `/api/auth/callback` on the frontend.
4. Use the query parameter `code` to obtain an ID token using the `/oauth/token` request—this is where I’m failing.

When I try using curl, I get an error:

```plaintext
curl --request POST \
     --url 'https://DOMAIN/oauth/token' \
     --header 'content-type: application/x-www-form-urlencoded' \
     --data 'code=code&grant_type=authorization_code&client_id=id&client_secret=secret&redirect_uri=http://localhost:3500/api/auth/callback'
{"error":"invalid_grant","error_description":"Invalid authorization code"}

```

For troubleshooting, when I manually go to the following URL in a browser and use the obtained code, I can successfully get an ID token:

```auto
https://DOMAIN/authorize?audience=http://localhost:8080&scope=openid profile email&response_type=code&client_id=id&redirect_uri=http://localhost:3500/api/auth/callback

```

I’m using a plugin in Next.js to handle this:

```auto
import { getAuthUserId } from '@/util/auth';
import { handleAuth, handleCallback, handleLogin } from '@auth0/nextjs-auth0';
import { NextRequest } from 'next/server';

// export default handleAuth();

export default handleAuth({
  callback: async (request: any, response: any) => {
    try {
      console.log('Callback :: ', response);
      await handleCallback(request, response, { redirectUri: 'http://localhost:3500/mypage' });
    } catch (error) {
      console.log('Callback error :: ', error);
      response.redirect("/auth/error");
    }
  },
  login: async (request: any, response: any) => {
    console.log('Logging in');
    // Pass custom parameters to login
    try {
      await handleLogin(request, response, {
        authorizationParams: {
          audience: 'http://localhost:8080',
          scope: 'openid profile email',
          response_type: 'code',
          redirect_uri: 'http://localhost:3500/api/auth/callback',
          connection_scope: 'openid profile email',
        },
        returnTo: '/mypage',
      });
      console.log('Successfully logged in');
    } catch (error) {
      console.log('Error logging in:', error);
    }
  },
});

```

What I want to achieve is to generate an authenticable `code` using `handleLogin` and obtain an ID token. The curl testing is for technical verification; in production, I plan to implement this in Go.

Your assistance is greatly appreciated.

---

<div class="post-metadata">

**Author:** ![nik.baleca](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nik.baleca/32/68749_2.png) [@nik.baleca](https://community.auth0.com/u/nik.baleca)\
**Post date:** [February 18, 2025, 6:23pm UTC](https://community.auth0.com/t/how-to-use-handlelogin-with-auth0-nextjs-auth0-in-next-js/116900/3 "2025-02-18T18:23:06Z")

</div>

Hi @Hiroshi0900

Welcome to the Auth0 Community!

I am sorry about the late reply to your inquiry!

I would recommend to review this community post regarding [retrieving the access token](https://community.auth0.com/t/auth0-nextjs-get-accesstoken-without-ssr/147818) and check out our [Github documentation for NextJS](https://auth0.github.io/nextjs-auth0/modules.html) in order to read more about the `handleLogin()` function since you might not be passing the correct scopes/audience to your CURL call.

If you have any other questions, feel free to let me know or post again on the community!

Kind Regards,  
Nik

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 4, 2025, 6:23pm UTC](https://community.auth0.com/t/how-to-use-handlelogin-with-auth0-nextjs-auth0-in-next-js/116900/4 "2025-03-04T18:23:48Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
