Next.js Discord

Discord Forum

How to solve auth-middleware and edge runtime problems?

Answered
Peterbald posted this in #help-forum
Open in Discord
PeterbaldOP
Hey guys,

Currently I'm working on a small projects which needs authentication. I implemented auth with authjs and my provider is GitHub. Now I want to secure my pages, which need authentication. I use route grouping to group unauthenticated and authenticated routes and handle their layouts. I had a couple of ideas:

1. Auth-Wrapper
- Component which is used in layout and wraps children
- uses useSession({ required: true }) to handle auth
-> downside of this approach that it had some weird sideeffects on logout callback-urls...

2. Middleware
- Now I wanted to implement a middleware like this: https://authjs.dev/guides/upgrade-to-v5?authentication-method=middleware#authentication-methods
- I would for example like to secure all /dashboard/* pages
- And now I get the error: The edge runtime does not support Node.js 'crypto' module.
- nextjs uses the edge runtime for its middleware functionality
-> what is happening here, as it is the official documentation I don't know how to avoid this issue...

Has somebody have a working example on middleware auth with authjs and nextjs?
Answered by Peterbald
View full answer

1 Reply

PeterbaldOP
Answer