# Auth0 with next.js is not working with nest.js

**URL:** <https://community.auth0.com/t/auth0-with-next-js-is-not-working-with-nest-js/185754>\
**Category:** Get Help\
**Tags:** new-universal-login-experience, login-experience\
**Created:** [April 21, 2025, 3:33pm UTC](https://community.auth0.com/t/auth0-with-next-js-is-not-working-with-nest-js/185754 "2025-04-21T15:33:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicolatesla0987](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nicolatesla0987/32/71200_2.png) [@nicolatesla0987](https://community.auth0.com/u/nicolatesla0987)\
**Post date:** [April 21, 2025, 3:33pm UTC](https://community.auth0.com/t/auth0-with-next-js-is-not-working-with-nest-js/185754/1 "2025-04-21T15:33:13Z")

</div>

Hi i am trying to integrate auth0 with my next.js frontend and nest.js backend the frontend is working fine i am logging in and getting the token how secure it is is questionable because all of my keys are in env witch could be accessed from brouser dev tool

the issue is that now i am trying to integrate it with my nest.js backend but it’s not working it’s returning 401

i have spended houres on this don’t know that i am going wrong here

```auto
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
  constructor() {
    // Log env variables for debugging
    console.log('AUTH0_AUDIENCE:', process.env.AUTH0_AUDIENCE);
    console.log('AUTH0_ISSUER_BASE_URL:', process.env.AUTH0_ISSUER_BASE_URL);

    super({
      secretOrKeyProvider: jwksRsa.passportJwtSecret({
        cache: true,
        rateLimit: true,
        jwksRequests Per Minute: 5,
        jwksUri: `${process.env.AUTH0_ISSUER_BASE_URL}/.well-known/jwks.json`,
      }),
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      audience: process.env.AUTH0_AUDIENCE, // must match one value in the token's "aud" array
      issuer: process.env.AUTH0_ISSUER_BASE_URL, // must match token's "iss"
      algorithms: ['RS256'],
    });
  }

  async validate(payload: any) {
    // Log payload for debugging
    console.log('JWT payload:', payload);
    return payload;
  }
}

```

this is how i am getting my token:

```auto
import { getAccessToken } from '@auth0/nextjs-auth0';

export async function GET(request: Request) {
  try {
    const { accessToken } = await getAccessToken();
    if (!accessToken) {
      return new Response(JSON.stringify({ error: 'No access token' }), {
        status: 401,
      });
    }
    return new Response(JSON.stringify({ accessToken }), { status: 200 });
  } catch (e: any) {
    return new Response(JSON.stringify({ error: e.message }), { status: 500 });
  }
}

```

this is how i am handeling the login

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

if (
  !process.env.AUTH0_BASE_URL ||
  !process.env.AUTH0_SECRET ||
  !process.env.AUTH0_ISSUER_BASE_URL ||
  !process.env.AUTH0_CLIENT_ID ||
  !process.env.AUTH0_CLIENT_SECRET
) {
  throw new Error('Missing Auth0 environment variables');
}

// Note: We do not use `params` in this route handler. The warning can be ignored.

export const GET = handleAuth({
  login: handleLogin({
    authorizationParams: {
      scope: 'openid profile email', // Add any other scopes you need
    },
  }),
});
export const POST = handleAuth();
export const PUT = handleAuth();
export const DELETE = handleAuth();

```

let me know if i am using auth0 correctly and if i should even use auth0 in the first place

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [April 24, 2025, 12:08am UTC](https://community.auth0.com/t/auth0-with-next-js-is-not-working-with-nest-js/185754/3 "2025-04-24T00:08:00Z")

</div>

Hi @nicolatesla0987,

Welcome to the Auth0 Community!

The `401` error you encountered typically means that the access token is missing, invalid, or malformed. In this case, it looks like the front end may not be getting a JWT access token because no `audience` is specified.

To ensure it’s a JWT access token, add one of your API identifiers as the `audience` for the authorization parameters.

(Reference: [https://auth0.com/docs/secure/tokens/access-tokens/get-access-tokens#control-access-token-audience](https://auth0.com/docs/secure/tokens/access-tokens/get-access-tokens#control-access-token-audience))

After doing so, the access token will be a JWT, and your backend will be able to validate it.

Let me know how this goes for you.

Thanks,  
Rueben

---

<div class="post-metadata">

**Author:** ![nicolatesla0987](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/nicolatesla0987/32/71200_2.png) [@nicolatesla0987](https://community.auth0.com/u/nicolatesla0987)\
**Post date:** [April 24, 2025, 2:29pm UTC](https://community.auth0.com/t/auth0-with-next-js-is-not-working-with-nest-js/185754/4 "2025-04-24T14:29:14Z")

</div>

> [@rueben.tiow](#):
>
> , or

it’s fixed it was a typo issue

thanks for helping me

currently i am having another issue even though i have added email scope i am not getting the user email in the JWT payload

```auto
JWT Payload: {
  "iss": "https://dev-ba1x5rok53uuqkl0.us.auth0.com/",
  "sub": "auth0|6808ca7135629091a4429fa8",
  "aud": [
    "http://localhost:8080",
    "https://dev-ba1x5rok53uuqkl0.us.auth0.com/userinfo"
  ],
  "iat": 1745503663,
  "exp": 1745590063,
  "scope": "openid profile email",
  "azp": "M2uAyZhg5X9ecdWiKiYp0PP47NVXNOnD"
}

```

can you help me with this

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [April 24, 2025, 7:09pm UTC](https://community.auth0.com/t/auth0-with-next-js-is-not-working-with-nest-js/185754/5 "2025-04-24T19:09:15Z")

</div>

Hi @nicolatesla0987,

Thanks for the update, and I’m glad it’s working now.

If you have specified the `email` scope, the `email_address` and `email_verified` claims can be found in the ID token.

This is mentioned in our [Sample Use Cases: Scopes and Claims](https://auth0.com/docs/get-started/apis/scopes/sample-use-cases-scopes-and-claims#:~:text=Extract%20the%20ID,the%20following%20claims%3A) documentation.

If you haven’t, you might have to include the `openid` scope as well.

Best,  
Rueben

---

<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:** [May 8, 2025, 7:10pm UTC](https://community.auth0.com/t/auth0-with-next-js-is-not-working-with-nest-js/185754/6 "2025-05-08T19:10:04Z")

</div>

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