GuidesAPI Calls
Any file inside the /src/app/api
folder will be automatically exposed as an API endpoint.
Creating protected endpoints with PullTheCode is easy thanks to its wrappers around NextAuth, which exposes a series of functions to check if the user is authenticated or not.
In the following example, you can see how to leverage NextAuth to check if the user is authenticated.
import {getServerSession} from "next-auth/next";import {authOptions} from "@/helpers/auth/auth.helpers";import {generateErrorBadGateway, generateErrorNotAuthorized} from "@/helpers/errors/error-responses.helpers";import {getAllLeadNewsletters} from "@/helpers/models/lead-newsletter.helpers";import {isUserAuthenticated} from "@/shared";export const GET = async () => {try {const session = await getServerSession(authOptions)if (!isUserAuthenticated(session)) return generateErrorNotAuthorized()const leadsNewsletter = await getAllLeadNewsletters()return Response.json({ leadsNewsletter })} catch (error) {return generateErrorBadGateway()}}