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