How can I integrate next-auth with Nestjs #1775
-
Next is a framework that serves the React front end to the user. While, on the other hand, auth functionality is something that is implemented on the backend and it uses a db to store the Sessions data. How next-auth (kind of backend functionality) works in conjunction with the Next (frontend framework)? My Nest JS based application exposes the GraphQL API to the client apps. How can I integrate these three things i.e. next-auth, Next, and Nest while making sure that the calls to the Prisma orm / db should only be made from the NestJS app. Kindly guide. Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 7 replies
-
NextAuth is making use of Next API routes which is Next solution for providing a minimal backend and each 'api route' can be deployed for example as a serverless function. The database with NextAuth is optional if you don't need to persist user data or support email sign in. If you don't specify a database then JWTs will be enabled for session storage and used to store session data. It will be a problem for you if both Nest and Next will interact with your database? |
Beta Was this translation helpful? Give feedback.
-
I had a similar issue as this. I wanted to use a separate Koa backend but there are no examples anywhere lol. All I know is you need to use the authorise/jwt callback to fetch a token from the server and store it in your Next session. Then when you make calls to your Nest server, you need to check this token (that you pass in from Next) to determine whether the user is authorised. In Apollo server I think you need to update the context to do this, something like:
Unfortunately, as I said, there are no end-to-end examples anywhere. I know people post articles and posts with brevity in mind, but it's incredibly frustrating because connecting the dots isn't always straightforward. Some times you just need a codebase to look at to learn. I just went for API routes in the end with a GraphQL endpoint at |
Beta Was this translation helpful? Give feedback.
-
Hi @StefanSelfTaught @JavaJamie The following repository is using both nestjs and next-auth Do you think that is this the right way to use it? Thanks |
Beta Was this translation helpful? Give feedback.
-
There is a discussion in #2294 to expand to other frameworks than Next.js. In the short(er) term, we could aim for anything with a backend and frontend using React. in long(er) terms, we could expand to Svelte, Vue, etc. |
Beta Was this translation helpful? Give feedback.
-
I asked AI and it already help half of the work. |
Beta Was this translation helpful? Give feedback.
There is a discussion in #2294 to expand to other frameworks than Next.js. In the short(er) term, we could aim for anything with a backend and frontend using React. in long(er) terms, we could expand to Svelte, Vue, etc.