avoid too many duplicate requireUserInServerComponent requests for each page+layout
This commit is contained in:
@@ -10,7 +10,6 @@ import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||
|
||||
import { pathsConfig } from '@kit/shared/config';
|
||||
|
||||
|
||||
import { UpdateAccountSchema } from '../schemas/update-account.schema';
|
||||
|
||||
export const onUpdateAccount = enhanceAction(
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
import { signOutAction } from '@/lib/actions/sign-out';
|
||||
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
||||
|
||||
import { BackButton } from '@kit/shared/components/back-button';
|
||||
import { MedReportLogo } from '@kit/shared/components/med-report-logo';
|
||||
@@ -15,12 +14,8 @@ import { loadCurrentUserAccount } from '@/app/home/(user)/_lib/server/load-user-
|
||||
import { toTitleCase } from '~/lib/utils';
|
||||
|
||||
async function UpdateAccount() {
|
||||
const client = getSupabaseServerClient();
|
||||
const account = await loadCurrentUserAccount();
|
||||
const { account, user } = await loadCurrentUserAccount();
|
||||
|
||||
const {
|
||||
data: { user },
|
||||
} = await client.auth.getUser();
|
||||
const isKeycloakUser = user?.app_metadata?.provider === 'keycloak';
|
||||
|
||||
if (!user) {
|
||||
|
||||
@@ -22,13 +22,14 @@ export default async function AnalysisResultsPage({
|
||||
id: string;
|
||||
}>;
|
||||
}) {
|
||||
const account = await loadCurrentUserAccount();
|
||||
|
||||
const { id: analysisOrderId } = await params;
|
||||
|
||||
const analysisResponse = await loadUserAnalysis(Number(analysisOrderId));
|
||||
const [{ account }, analysisResponse] = await Promise.all([
|
||||
loadCurrentUserAccount(),
|
||||
loadUserAnalysis(Number(analysisOrderId)),
|
||||
]);
|
||||
|
||||
if (!account?.id || !analysisResponse) {
|
||||
if (!account?.id) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -37,6 +38,10 @@ export default async function AnalysisResultsPage({
|
||||
action: PageViewAction.VIEW_ANALYSIS_RESULTS,
|
||||
});
|
||||
|
||||
if (!analysisResponse) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<PageHeader />
|
||||
|
||||
@@ -168,7 +168,7 @@ async function sendAnalysisPackageOrderEmail({
|
||||
}
|
||||
|
||||
export async function processMontonioCallback(orderToken: string) {
|
||||
const account = await loadCurrentUserAccount();
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error("Account not found in context");
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ export const generateMetadata = async () => {
|
||||
};
|
||||
|
||||
async function OrderAnalysisPage() {
|
||||
const account = await loadCurrentUserAccount();
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ export const generateMetadata = async () => {
|
||||
async function UserHomePage() {
|
||||
const client = getSupabaseServerClient();
|
||||
|
||||
const account = await loadCurrentUserAccount();
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
const api = createAccountsApi(client);
|
||||
const bmiThresholds = await api.fetchBmiThresholds();
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { createPageViewLog, PageViewAction } from "~/lib/services/audit/pageView
|
||||
import { loadCurrentUserAccount } from "../../_lib/server/load-user-account";
|
||||
|
||||
export async function logAnalysisResultsNavigateAction(analysisOrderId: string) {
|
||||
const account = await loadCurrentUserAccount();
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
|
||||
@@ -140,7 +140,7 @@ async function analysisPackagesWithVariantLoader({
|
||||
}
|
||||
|
||||
async function analysisPackagesLoader() {
|
||||
const account = await loadCurrentUserAccount();
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
|
||||
@@ -16,9 +16,12 @@ export const loadUserAccount = cache(accountLoader);
|
||||
|
||||
export async function loadCurrentUserAccount() {
|
||||
const user = await requireUserInServerComponent();
|
||||
return user?.id
|
||||
? await loadUserAccount(user.id)
|
||||
: null;
|
||||
const userId = user?.id;
|
||||
if (!userId) {
|
||||
return { account: null, user: null };
|
||||
}
|
||||
const account = await loadUserAccount(userId);
|
||||
return { account, user };
|
||||
}
|
||||
|
||||
async function accountLoader(userId: string) {
|
||||
|
||||
@@ -17,7 +17,7 @@ export const generateMetadata = async () => {
|
||||
};
|
||||
|
||||
async function PersonalAccountSettingsPage() {
|
||||
const account = await loadCurrentUserAccount();
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
return (
|
||||
<PageBody>
|
||||
<div className="mx-auto w-full bg-white p-6">
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
import { CardTitle } from '@kit/ui/card';
|
||||
import { LanguageSelector } from '@kit/ui/language-selector';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import { loadCurrentUserAccount } from '../../_lib/server/load-user-account';
|
||||
import AccountPreferencesForm from '../_components/account-preferences-form';
|
||||
import SettingsSectionHeader from '../_components/settings-section-header';
|
||||
|
||||
export default async function PreferencesPage() {
|
||||
const account = await loadCurrentUserAccount();
|
||||
const { account } = await loadCurrentUserAccount();
|
||||
|
||||
return (
|
||||
<div className="mx-auto w-full bg-white p-6">
|
||||
@@ -16,7 +12,6 @@ export default async function PreferencesPage() {
|
||||
titleKey="account:preferencesTabLabel"
|
||||
descriptionKey="account:preferencesTabDescription"
|
||||
/>
|
||||
|
||||
<AccountPreferencesForm account={account} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { requireUserInServerComponent } from '@/lib/server/require-user-in-server-component';
|
||||
import { createAccountsApi } from '@/packages/features/accounts/src/server/api';
|
||||
import { getSupabaseServerClient } from '@/packages/supabase/src/clients/server-client';
|
||||
|
||||
@@ -12,8 +11,7 @@ export default async function HomeLayout({
|
||||
}) {
|
||||
const client = getSupabaseServerClient();
|
||||
|
||||
const user = await requireUserInServerComponent();
|
||||
const account = await loadCurrentUserAccount();
|
||||
const { account, user } = await loadCurrentUserAccount();
|
||||
const api = createAccountsApi(client);
|
||||
|
||||
const hasAccountTeamMembership = await api.hasAccountTeamMembership(
|
||||
|
||||
@@ -38,8 +38,7 @@ export async function handleAddToCart({
|
||||
countryCode: string;
|
||||
}) {
|
||||
const supabase = getSupabaseServerClient();
|
||||
const user = await requireUserInServerComponent();
|
||||
const account = await loadCurrentUserAccount();
|
||||
const { account, user } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
@@ -70,8 +69,7 @@ export async function handleDeleteCartItem({ lineId }: { lineId: string }) {
|
||||
|
||||
const supabase = getSupabaseServerClient();
|
||||
const cartId = await getCartId();
|
||||
const user = await requireUserInServerComponent();
|
||||
const account = await loadCurrentUserAccount();
|
||||
const { account, user } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
@@ -96,8 +94,7 @@ export async function handleNavigateToPayment({
|
||||
paymentSessionId: string;
|
||||
}) {
|
||||
const supabase = getSupabaseServerClient();
|
||||
const user = await requireUserInServerComponent();
|
||||
const account = await loadCurrentUserAccount();
|
||||
const { account, user } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
@@ -137,8 +134,7 @@ export async function handleLineItemTimeout({
|
||||
lineItem: StoreCartLineItem;
|
||||
}) {
|
||||
const supabase = getSupabaseServerClient();
|
||||
const user = await requireUserInServerComponent();
|
||||
const account = await loadCurrentUserAccount();
|
||||
const { account, user } = await loadCurrentUserAccount();
|
||||
if (!account) {
|
||||
throw new Error('Account not found');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user