nextjs-auth0 icon indicating copy to clipboard operation
nextjs-auth0 copied to clipboard

Cannot get getAccessToken - cookies() expects to have requestAsyncStorage, none available

Open jLynx opened this issue 5 months ago • 7 comments

Checklist

  • [X] The issue can be reproduced in the nextjs-auth0 sample app (or N/A).
  • [X] I have looked into the Readme, Examples, and FAQ and have not found a suitable solution or answer.
  • [X] I have looked into the API documentation and have not found a suitable solution or answer.
  • [X] I have searched the issues and have not found a suitable solution or answer.
  • [X] I have searched the Auth0 Community forums and have not found a suitable solution or answer.
  • [X] I agree to the terms within the Auth0 Code of Conduct.

Description

I am trying to get Auth0 working in our project using the tutorial from the ReadMe. I have it so I can login and log out successfully, but I am now needing to get the AccessToken to pass to an external API. I have tried following the page setup from here https://auth0.github.io/nextjs-auth0/types/session_get_access_token.GetAccessToken.html

But sadly I am getting the error: Uncaught (in promise) Error: Invariant: cookies() expects to have requestAsyncStorage, none available. image

If I follow the example exactly I can get it to output using

// app/my-api/route.js
import { getAccessToken } from "@auth0/nextjs-auth0/edge" // Note the /edge import
import { NextResponse } from "next/server"

export const GET = async () => {
  const { accessToken } = await getAccessToken()
  return NextResponse.json({ accessToken: accessToken })
}

export const runtime = "edge"

But I need to get the accessToken on a page that also uses useState, so when I do const { accessToken } = await getAccessToken() I get that error above. Is this a bug, or what can I do to get access to the logged in users accessToken in the example codesandbox.io I have linked?

Thanks for your time!

Reproduction

I have set up a code example here, just change the .env settings with your own: https://codesandbox.io/p/devbox/auth0-login-token-dwl8rj?file=%2Fapp%2Fpage.tsx%3A18%2C27

Additional context

No response

nextjs-auth0 version

^3.5.0

Next.js version

14.0.4

Node.js version

v20.7.0

jLynx avatar Jan 22 '24 20:01 jLynx