Next.js Discord

Discord Forum

Fetching Data with Server Action - CurrentUser

Unanswered
Perro de Presa Canario posted this in #help-forum
Open in Discord
Perro de Presa CanarioOP
Hello, everyone! I'm working on a project using Next.js, where I'm integrating Prisma for ORM and NextAuth for user authentication. In my application, each company can have multiple users associated with it (one-to-many relationship), and I'm trying to fetch the company details for the currently authenticated user.

Current Setup:

Authentication: Utilizing NextAuth for handling authentication flows. I've also set up a custom hook, useCurrentUser, to extract the current user's details from the NextAuth session.
Database Schema: My Prisma schema defines users and companies, where multiple users can be associated with a single company via a companyId field in the User model.
Objective: I aim to efficiently fetch and include the authenticated user's associated company details either within the NextAuth session or make it easily accessible on the client side after authentication.

Challenges:

I'm uncertain about the best approach to append the company data for the current user within the NextAuth session callback, considering the one-to-many relationship model.
I'm seeking advice on the most efficient Prisma query to retrieve the company data linked to the authenticated user, ensuring the database call is optimized and follows best practices.

Question:

How can I modify the NextAuth session callback to include the related company data for the current user, given the one-to-many relationship between Company and User?
What is the recommended Prisma query pattern for fetching related data (company for the current user) efficiently and making it available on the client side?
Or is there a other approach then the session callback?

0 Replies