# Auth0 token obtained through nextjs-auth0 SDK in Nextjs is valid in local environment, but invalid when deployed on AWS

**URL:** <https://community.auth0.com/t/auth0-token-obtained-through-nextjs-auth0-sdk-in-nextjs-is-valid-in-local-environment-but-invalid-when-deployed-on-aws/67963>\
**Category:** Get Help\
**Tags:** nextjs\
**Created:** [August 15, 2021, 7:20am UTC](https://community.auth0.com/t/auth0-token-obtained-through-nextjs-auth0-sdk-in-nextjs-is-valid-in-local-environment-but-invalid-when-deployed-on-aws/67963 "2021-08-15T07:20:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacoblim](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/jacoblim/32/12805_2.png) [@jacoblim](https://community.auth0.com/u/jacoblim)\
**Post date:** [August 15, 2021, 7:20am UTC](https://community.auth0.com/t/auth0-token-obtained-through-nextjs-auth0-sdk-in-nextjs-is-valid-in-local-environment-but-invalid-when-deployed-on-aws/67963/1 "2021-08-15T07:20:19Z")

</div>

- **Which SDK this is regarding:** nextjs-auth0
- **SDK Version:** v1.5.0
- **Platform Version:** Node v14.6.1
- **Code Snippets/Error Messages/Supporting Details/Screenshots:**  
Hey, my issue is this, I have a next’s project that is linked to an express backend. Using the nextjs-auth0 SDK, a JWT token is obtained using the getAccessToken() function in the package. This is done with the API functionality of Nextjs. The JWT is then sent to the express backend along with any and all HTTP requests which will then be checked and validated on the express backend. This process works flawlessly on local environment, with the appropriate middleware in express rejecting any requests without a token and allowing ones with a token through. HOWEVER, when deployed on AWS, the token obtained by Nextjs is always invalid thus causing valid user logins to always not be able to access data from the backend.

All appropriate environment variables have been declared, and there doesn’t seem to be anything missing, yet this problem still persists.

Nextjs API where token is obtained

```auto
import { NextApiRequest, NextApiResponse } from 'next'
import { getAccessToken, withApiAuthRequired } from '@auth0/nextjs-auth0'

export default withApiAuthRequired(async function User(
  req: NextApiRequest,
  res: NextApiResponse
) {
  const { accessToken } = await getAccessToken(req, res)
  console.log(accessToken)
  if (req.method === 'GET') {
    const response = await fetch(
      `${process.env.NEXT_PUBLIC_API_URL}/api/users/${req.query.userId}`,
      {
        headers: {
          Authorization: `Bearer ${accessToken}`
        }
      }
    )
    const data = await response.json()
    res.status(200).json(data)
  } else {
    const response = await fetch(
      `${process.env.NEXT_PUBLIC_API_URL}/api/users/${req.query.userId}`,
      {
        body: JSON.stringify(req.body),
        headers: {
          'Content-Type': 'application/json',
          Authorization: `Bearer ${accessToken}`
        },
        method: 'PUT'
      }
    )
    const data = await response.json()
    console.log(data)
    res.status(200).json(data)
  }
})

```

Middleware on Express validating JWTs

```auto
const jwt = require("express-jwt");
const jwks = require("jwks-rsa");
const jwtAuthz = require("express-jwt-authz");
const dotenv = require("dotenv-safe");

dotenv.config();
const jwtCheck = jwt({
  secret: jwks.expressJwtSecret({
    cache: true,
    rateLimit: true,
    jwksRequestsPerMinute: 5,
    jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`,
  }),
  audience: process.env.AUTH0_AUDIENCE,
  issuer: `https://${process.env.AUTH0_DOMAIN}/`,
  algorithms: ["RS256"],
}).unless(ROUTES);
// unless is an array of public routes that are whitelisted from JWT middleware checks

module.exports = jwtCheck;

```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [August 16, 2021, 8:12pm UTC](https://community.auth0.com/t/auth0-token-obtained-through-nextjs-auth0-sdk-in-nextjs-is-valid-in-local-environment-but-invalid-when-deployed-on-aws/67963/3 "2021-08-16T20:12:22Z")

</div>

Have you compared the JWTs you get from local and deployed? Are they identical? You can easily decode them with [jwt.io](http://jwt.io).

---

<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:** [October 13, 2021, 6:38pm UTC](https://community.auth0.com/t/auth0-token-obtained-through-nextjs-auth0-sdk-in-nextjs-is-valid-in-local-environment-but-invalid-when-deployed-on-aws/67963/4 "2021-10-13T18:38:27Z")

</div>

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