Add more efficient authentication check function to server components.
Added request logging to Next.js config. This commit introduces a new function 'requireUserInServerComponent' which checks for user authentication and is used in multiple server components. The aim is to enhance efficiency by caching the function so that data is only fetched once per request, preventing unnecessary database hits. Existing components were modified accordingly to incorporate this new method.
This commit is contained in:
@@ -9,6 +9,7 @@ import { getSupabaseServerComponentClient } from '@kit/supabase/server-component
|
||||
|
||||
import billingConfig from '~/config/billing.config';
|
||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||
import { requireUserInServerComponent } from '~/lib/server/require-user-in-server-component';
|
||||
|
||||
interface SessionPageProps {
|
||||
searchParams: {
|
||||
@@ -73,13 +74,9 @@ function BlurryBackdrop() {
|
||||
}
|
||||
|
||||
async function loadCheckoutSession(sessionId: string) {
|
||||
await requireUserInServerComponent();
|
||||
|
||||
const client = getSupabaseServerComponentClient();
|
||||
const { error } = await requireUser(client);
|
||||
|
||||
if (error) {
|
||||
throw new Error('Authentication required');
|
||||
}
|
||||
|
||||
const gateway = await getBillingGatewayProvider(client);
|
||||
|
||||
const session = await gateway.retrieveCheckoutSession({
|
||||
|
||||
Reference in New Issue
Block a user