Next.js Discord

Discord Forum

Best practices for service pattern

Unanswered
Sokoke posted this in #help-forum
Open in Discord
SokokeOP
This may be more of a general React question than specific to Nextjs, but I'm starting my first Nextjs project (and first React project in several years), and I'm curious about what the current best practices are for managing services. For example, I'm leveraging the OAuth2 client from google-auth-library package to authorize access to some Google APIs on behalf of the user. I need to access that client from both route handlers (oauth callback) and server components, to actually interact with the Google APIs. Based on what I'm reading, my current plan is to initialize and export the client in a module that lives somewhere outside the app directory, use the Context API to inject it into components and import it directly into route handlers that need it. Is that on the right track, and are there examples of this pattern somewhere? I'm having a hard time finding anything but extremely trivial examples online.

0 Replies