-
Notifications
You must be signed in to change notification settings - Fork 126
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
fix: Fix next-auth middleware not working on Lambda #38
Conversation
…iddleware to work
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@@ -306,6 +310,7 @@ function esbuildSync(options: BuildOptions) { | |||
format: "esm", | |||
platform: "node", | |||
bundle: true, | |||
minify: true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This saves up to 60% of size
src/adapters/middleware-adapter.ts
Outdated
@@ -57,8 +57,14 @@ export async function handler(event: CloudFrontRequestEvent): Promise<CloudFront | |||
return request; | |||
} | |||
|
|||
// In the case where `middleware.ts` response w/ a body, send that back to the client |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This may be useful to have if your middleware.ts returns data immediately to the client.
Just my opinion but having to request quota increases on every AWS account you want to deploy a small-medium sized nextjs project is not a great experience |
Yes I agree, but we have to eat the 💩 sandwich due to the default limitation. 25 should be enough for a small project though. If you're using a lot more behaviors then expecting the user to request for quota increase seems reasonable. |
It's great to see the progress on this PR. Next-auth support is a blocker to using SST for my team. 🚀🚀🚀🚀 Let's Ship it!! |
src/adapters/middleware-adapter.ts
Outdated
@@ -57,8 +57,14 @@ export async function handler(event: CloudFrontRequestEvent): Promise<CloudFront | |||
return request; | |||
} | |||
|
|||
// In the case where `middleware.ts` response w/ a body, send that back to the client | |||
let responseBody | |||
const state = Object.getOwnPropertySymbols(response).find(s => s.toString() === 'Symbol(state)') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this symbol not exported?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason, the symbol wasn't create via Symbol.for('state')
so I couldn't access it by key.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
turns out it was much simpler to use response.text()
to get the body.
return { | ||
status: response.status, | ||
body: responseBody, | ||
body: response.body ? await response.text() : undefined, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for cleaning up! Can you also remove all the console.log
please? CW $ adds up pretty quick from the extra bytes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah.. I will make a separate PR for that today. I want to add a DEBUG flag, and when u do DEBUG=true open-next build
, it will console.log
.
This PR allows
next-auth/middleware
to properly run in Lambda environment.When using this w/ NextjsSitev2 you will probably get infinite redirects due to the way the CloudFront Distribution is set up. To get around this, you can make changes to NextjsSite, eg:
NOTE: there's a limit of 25 cache behaviors, so if you need more than that just request a quota upgrade and you should easily get several hundred.
Or you can not use the
default
middleware and read the token manually:NOTE: the current NextjsSite distribution will route all requests to middleware... this should be fixed in the future per logic above.
This will guard all your
/api
endpoints: