Skip to content
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

Update Edge Compatibility section for Middleware #10821

Closed
IlyyA opened this issue May 5, 2024 · 2 comments
Closed

Update Edge Compatibility section for Middleware #10821

IlyyA opened this issue May 5, 2024 · 2 comments
Labels
documentation Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.

Comments

@IlyyA
Copy link

IlyyA commented May 5, 2024

What is the improvement or update you wish to see?

The middleware section in the migrating to v5 page does not let the user know that you cannot access the authenticated user through the middleware if using the database. It currently reads as if making the relevant changes will be a successful workaround for all usecases, when instead it merely just allows the user to use the database adapter in other parts of the applicatoin except the middleware completely.

Is there any context that might help us understand?

Inform the user that following this process to remove any Edge Runtime errors in the middleware will result in no database access in the middleware, resulting in no ability to protect resources in the middleware level, only on each individual page.

Does the docs page already exist? Please link to it.

https://authjs.dev/getting-started/migrating-to-v5#edge-compatibility

@IlyyA IlyyA added documentation Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime. labels May 5, 2024
@ndom91
Copy link
Member

ndom91 commented May 15, 2024

Thanks for pointing this out, we've just shipped a more general edge compatibility doc and mentioned the fact that when applying the split config workaround, that the middleware then doesn't ahve any access to the db.

https://authjs.dev/guides/edge-compatibility

@ndom91 ndom91 closed this as completed May 15, 2024
@Chenalejandro
Copy link

Chenalejandro commented May 23, 2024

EDIT: I created an issue with the steps to reproduce: #11001

Please ignore the message below.

@ndom91

The provided documentation doesn't seem to work, I'm getting the error:

[auth][error] JWTSessionError: Read more at https://errors.authjs.dev#jwtsessionerror
[auth][cause]: JWEInvalid: Invalid Compact JWE

The error described in https://errors.authjs.dev#jwtsessionerror shouldn't be possible (It says that it is related to jwt strategy) since I am using the database strategy (since having an adapter in auth.ts automatically uses that stategy).

Another side note: I'm using an edge compatible driver (@vercel/postgres) and deploying to vercel (edge middleware), so in theory I am able to access the database in the middleware.
The problem I faced was too much latency (each page takes more than 1200ms to load). This is because the middleware always runs in the edge, which is a problem when the database is in another region (for example, if the edge middleware is in India and the database is in us-east). It only works when the edge middleware is being run in the same region. As soon as I remove the next-auth middleware, the page only takes 200ms to load.

Vercel does not allows you to change the edge middleware region, it always default to the region nearest to the user.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Relates to documentation triage Unseen or unconfirmed by a maintainer yet. Provide extra information in the meantime.
Projects
None yet
Development

No branches or pull requests

3 participants