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:
@@ -4,11 +4,11 @@ import { cache } from 'react';
|
||||
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
import { requireUser } from '@kit/supabase/require-user';
|
||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
||||
import { createTeamAccountsApi } from '@kit/team-accounts/api';
|
||||
|
||||
import pathsConfig from '~/config/paths.config';
|
||||
import { requireUserInServerComponent } from '~/lib/server/require-user-in-server-component';
|
||||
|
||||
export type TeamAccountWorkspace = Awaited<
|
||||
ReturnType<typeof loadTeamWorkspace>
|
||||
@@ -29,9 +29,9 @@ async function workspaceLoader(accountSlug: string) {
|
||||
const client = getSupabaseServerComponentClient();
|
||||
const api = createTeamAccountsApi(client);
|
||||
|
||||
const [workspace, auth] = await Promise.all([
|
||||
const [workspace, user] = await Promise.all([
|
||||
api.getAccountWorkspace(accountSlug),
|
||||
requireUser(client),
|
||||
requireUserInServerComponent(),
|
||||
]);
|
||||
|
||||
// we cannot find any record for the selected account
|
||||
@@ -40,12 +40,6 @@ async function workspaceLoader(accountSlug: string) {
|
||||
return redirect(pathsConfig.app.home);
|
||||
}
|
||||
|
||||
if (!auth.data) {
|
||||
return redirect(auth.redirectTo);
|
||||
}
|
||||
|
||||
const user = auth.data;
|
||||
|
||||
return {
|
||||
...workspace.data,
|
||||
user,
|
||||
|
||||
Reference in New Issue
Block a user