Is serverless authentication possible? (JWT without JWE) #3365
-
Question 💬I would like to have a simple and fast way to authenticate users. For this reason, I had previously developed my own solution based on JWT. The advantage of JWT for me was that no lookup is necessary to know on the client side who is currently logged in and which role he has. This means that my app can load completely from cache and thus would theoretically work offline. With next-auth, it seems that the session stored in the cookie needs to be resolved or validated on the server side. I always see a request to /api/auth/session before useSession gives me a result. This means that the page loading is unnecessarily delayed (until the request is through) and in the worst case even doesn't work at all (if there was a connection problem, or the user is offline for a short time). Since I'm just getting into next-auth, my question is whether or not "serverless" authentication using JWT is possible? And if not, if we want to talk about what an appropriate implementation could look like? If possible, I'd like to omit the roundtrip to the server completely, or if it makes sense from a technical or security point of view, make this request using the "stale-while-invalidate" principle: when there is a local session, it is returned immediately (maybe with an additional status that indicates it is stale), while the session is validated in the background. Of course this function can be opt-in. Thanks for having this project and for maintaining it. How to reproduce ☕️Contributing 🙌🏽Yes, I am willing to help answer this question in a PR |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
This is literally the purpose of our library. See the landing page: https://next-auth.js.org/
The check on the backend is necessary for security reasons. If you set everything up correctly, you shouldn't be calling the endpoint every time, since there is a cache mechanism built into the client-side code. |
Beta Was this translation helpful? Give feedback.
This is literally the purpose of our library. See the landing page: https://next-auth.js.org/
The check on the backend is necessary for security reasons. If you set everything up correctly, you shouldn't be calling the endpoint every time, since there is a cache mechanism built into the client-side code.
https://next-auth.js.org/getting-started/example