NextJS middleware
Hello,
In the middleware file, I want to check if the user is authenticated and allowed to visit specific pages.
Currently how I do it is as following:
This works but what I don't like about it is that I now have double code for the same thing.
When I import the auth functionality from all the pages, so like
it works in dev mode but when building for production, it says
Dynamic Code Evaluation (e. g. 'eval', 'new Function', 'WebAssembly.compile') not allowed in Edge Runtime
because I import those auth from the pages.
Does someone know a better and cleaner way of doing this? I have quite some pages and I don't want to forgot also adding the auth in the routes array in the middleware.2 Replies
Oddly i just shared this in another post earlier... this is how I'm currently handling this in my applications:
You can just as easily use
getSession
to get the current session if you're including your user roles on the session object.Unfortunately, I don't use NextAuth since I have an external backend for some reasons