-
Notifications
You must be signed in to change notification settings - Fork 389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"The getAccessToken method can only be used from the server side" when used in middleware.js #912
Comments
Hi @robertwbradford - thanks for raising Everything under We don't have import { NextResponse } from "next/server";
import { getAccessToken } from "@auth0/nextjs-auth0/edge";
export async function middleware(req) {
const res = NextResponse.next();
const { accessToken, accessTokenExpiresAt } = await getSession(req, res);
if (accessTokenExpiresAt * 1000 < Date.now()) {
// redirect somewhere to refresh it
} else {
// do something with the access token
}
} |
Hey @adamjmcgrath, is there a plan to support refreshing tokens on the edge in the short term? |
Hi @focux - we don't currently have plans to support it in the short term. But we've left this feature request open to gauge support for it and hear about the different use cases - so if the above workaround doesn't work for you, we'd be interested to hear more about it. |
do we have a way to get the token client side? |
Definitely would be good to be able to refresh the token in edge environments, especially with the new Edge API Routes. The edge seems particularly well suited for actions such as these, which are simple, need to be fast, and can be executed during the lifetime of a Request. |
@adamjmcgrath hi,
so "refresh it", you mean by just calling I don't fully understand what you mean by "refresh it". The goal here would be to have an updated AT in edge function's |
Yes, this is the current workaround while we don't have the functionality to refresh the AT at the edge |
Just to follow up, I was able to implement this approach successfully. My use case was wanting to have an api proxy that runs on edge. My solution was to have both an edge and node version of this proxy, and the edge one will forward to the node one if the access token is no longer valid. This works pretty well, though you have to be careful around set-cookie handling to have the set-cookie header returned correctly in the final response. However, I would really like to see all functionality supported on edge - both refreshing the token and the core handlers for the full authentication flow. Clerk already supports this, and it is implemented (though not yet released) in NextAuth so it seems like the Edge APIs do have everything needed to support this. |
@dkokotov which proxy library are you using for edge? Glad to hear the successful story. Thanks. 👍 |
@benevbright I just wrote my own, it is not comprehensive, but it covers our use cases (primarily graphql, but some rest api as well). Below is the edge version - it lives under
And then there is another node-based proxy at
|
@dkokotov many many thanks! very cool :) |
I have this use case as well and it would be nice to have that support as well in the edge. Wondering if there are plans to do this soon. |
👋 Full support for the edge runtime, including More info on the Beta can be found here #1235 |
Checklist
Description
Hello, I see there is a
withMiddlewareAuthRequired
helper coming in v2.We have a use case where we'd like to use
getAccessToken
in a Next.js middleware file but not require authentication. That is, auth is optional, but we will do additional things if a user is authenticated.However, in a
middleware.js
file, the call togetAccessToken()
is reporting:Error: The getAccessToken method can only be used from the server side.
Seems that its check for the server environment is not accounting for the Next.js middleware feature.Any ideas on how to accomplish this?
Reproduction
I have the following in a
middleware.js
file:SDK version
^1.9.2
Next.js version
^12.3.0
Node.js version
v16.16.0
The text was updated successfully, but these errors were encountered: