Refactored Supabase Clients using the new recommended approach by Supabase by centralizing all clients around one single implementation. (#51)
The previous clients have been marked as deprecated and will be removed at some point.
This commit is contained in:
committed by
GitHub
parent
2f0c4b4ae3
commit
ba6e649461
@@ -1,11 +1,11 @@
|
|||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import { SiteFooter } from '~/(marketing)/_components/site-footer';
|
import { SiteFooter } from '~/(marketing)/_components/site-footer';
|
||||||
import { SiteHeader } from '~/(marketing)/_components/site-header';
|
import { SiteHeader } from '~/(marketing)/_components/site-header';
|
||||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||||
|
|
||||||
async function SiteLayout(props: React.PropsWithChildren) {
|
async function SiteLayout(props: React.PropsWithChildren) {
|
||||||
const client = getSupabaseServerComponentClient();
|
const client = getSupabaseServerClient();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: { user },
|
data: { user },
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { cache } from 'react';
|
|||||||
|
|
||||||
import { AdminAccountPage } from '@kit/admin/components/admin-account-page';
|
import { AdminAccountPage } from '@kit/admin/components/admin-account-page';
|
||||||
import { AdminGuard } from '@kit/admin/components/admin-guard';
|
import { AdminGuard } from '@kit/admin/components/admin-guard';
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||||
import { PageBody } from '@kit/ui/page';
|
import { PageBody } from '@kit/ui/page';
|
||||||
|
|
||||||
interface Params {
|
interface Params {
|
||||||
@@ -34,9 +34,7 @@ export default AdminGuard(AccountPage);
|
|||||||
const loadAccount = cache(accountLoader);
|
const loadAccount = cache(accountLoader);
|
||||||
|
|
||||||
async function accountLoader(id: string) {
|
async function accountLoader(id: string) {
|
||||||
const client = getSupabaseServerComponentClient({
|
const client = getSupabaseServerAdminClient();
|
||||||
admin: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
const { data, error } = await client
|
const { data, error } = await client
|
||||||
.from('accounts')
|
.from('accounts')
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { ServerDataLoader } from '@makerkit/data-loader-supabase-nextjs';
|
|||||||
|
|
||||||
import { AdminAccountsTable } from '@kit/admin/components/admin-accounts-table';
|
import { AdminAccountsTable } from '@kit/admin/components/admin-accounts-table';
|
||||||
import { AdminGuard } from '@kit/admin/components/admin-guard';
|
import { AdminGuard } from '@kit/admin/components/admin-guard';
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||||
import { PageBody, PageHeader } from '@kit/ui/page';
|
import { PageBody, PageHeader } from '@kit/ui/page';
|
||||||
|
|
||||||
interface SearchParams {
|
interface SearchParams {
|
||||||
@@ -16,9 +16,7 @@ export const metadata = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function AccountsPage({ searchParams }: { searchParams: SearchParams }) {
|
function AccountsPage({ searchParams }: { searchParams: SearchParams }) {
|
||||||
const client = getSupabaseServerComponentClient({
|
const client = getSupabaseServerAdminClient();
|
||||||
admin: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
const page = searchParams.page ? parseInt(searchParams.page) : 1;
|
const page = searchParams.page ? parseInt(searchParams.page) : 1;
|
||||||
const filters = getFilters(searchParams);
|
const filters = getFilters(searchParams);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { getBillingEventHandlerService } from '@kit/billing-gateway';
|
import { getBillingEventHandlerService } from '@kit/billing-gateway';
|
||||||
import { enhanceRouteHandler } from '@kit/next/routes';
|
import { enhanceRouteHandler } from '@kit/next/routes';
|
||||||
import { getLogger } from '@kit/shared/logger';
|
import { getLogger } from '@kit/shared/logger';
|
||||||
import { getSupabaseRouteHandlerClient } from '@kit/supabase/route-handler-client';
|
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||||
|
|
||||||
import billingConfig from '~/config/billing.config';
|
import billingConfig from '~/config/billing.config';
|
||||||
|
|
||||||
@@ -20,8 +20,7 @@ export const POST = enhanceRouteHandler(
|
|||||||
|
|
||||||
logger.info(ctx, `Received billing webhook. Processing...`);
|
logger.info(ctx, `Received billing webhook. Processing...`);
|
||||||
|
|
||||||
const supabaseClientProvider = () =>
|
const supabaseClientProvider = () => getSupabaseServerAdminClient();
|
||||||
getSupabaseRouteHandlerClient({ admin: true });
|
|
||||||
|
|
||||||
const service = await getBillingEventHandlerService(
|
const service = await getBillingEventHandlerService(
|
||||||
supabaseClientProvider,
|
supabaseClientProvider,
|
||||||
|
|||||||
@@ -2,12 +2,12 @@ import { redirect } from 'next/navigation';
|
|||||||
import type { NextRequest } from 'next/server';
|
import type { NextRequest } from 'next/server';
|
||||||
|
|
||||||
import { createAuthCallbackService } from '@kit/supabase/auth';
|
import { createAuthCallbackService } from '@kit/supabase/auth';
|
||||||
import { getSupabaseRouteHandlerClient } from '@kit/supabase/route-handler-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import pathsConfig from '~/config/paths.config';
|
import pathsConfig from '~/config/paths.config';
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
const service = createAuthCallbackService(getSupabaseRouteHandlerClient());
|
const service = createAuthCallbackService(getSupabaseServerClient());
|
||||||
|
|
||||||
const { nextPath } = await service.exchangeCodeForSession(request, {
|
const { nextPath } = await service.exchangeCodeForSession(request, {
|
||||||
joinTeamPath: pathsConfig.app.joinTeam,
|
joinTeamPath: pathsConfig.app.joinTeam,
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import { NextRequest, NextResponse } from 'next/server';
|
import { NextRequest, NextResponse } from 'next/server';
|
||||||
|
|
||||||
import { createAuthCallbackService } from '@kit/supabase/auth';
|
import { createAuthCallbackService } from '@kit/supabase/auth';
|
||||||
import { getSupabaseRouteHandlerClient } from '@kit/supabase/route-handler-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import pathsConfig from '~/config/paths.config';
|
import pathsConfig from '~/config/paths.config';
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
const service = createAuthCallbackService(getSupabaseRouteHandlerClient());
|
const service = createAuthCallbackService(getSupabaseServerClient());
|
||||||
|
|
||||||
const url = await service.verifyTokenHash(request, {
|
const url = await service.verifyTokenHash(request, {
|
||||||
joinTeamPath: pathsConfig.app.joinTeam,
|
joinTeamPath: pathsConfig.app.joinTeam,
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { redirect } from 'next/navigation';
|
|||||||
|
|
||||||
import { MultiFactorChallengeContainer } from '@kit/auth/mfa';
|
import { MultiFactorChallengeContainer } from '@kit/auth/mfa';
|
||||||
import { checkRequiresMultiFactorAuthentication } from '@kit/supabase/check-requires-mfa';
|
import { checkRequiresMultiFactorAuthentication } from '@kit/supabase/check-requires-mfa';
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import pathsConfig from '~/config/paths.config';
|
import pathsConfig from '~/config/paths.config';
|
||||||
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||||
@@ -23,7 +23,7 @@ export const generateMetadata = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async function VerifyPage(props: Props) {
|
async function VerifyPage(props: Props) {
|
||||||
const client = getSupabaseServerComponentClient();
|
const client = getSupabaseServerClient();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: { user },
|
data: { user },
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { cache } from 'react';
|
import { cache } from 'react';
|
||||||
|
|
||||||
import { createAccountsApi } from '@kit/accounts/api';
|
import { createAccountsApi } from '@kit/accounts/api';
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import featureFlagsConfig from '~/config/feature-flags.config';
|
import featureFlagsConfig from '~/config/feature-flags.config';
|
||||||
import { requireUserInServerComponent } from '~/lib/server/require-user-in-server-component';
|
import { requireUserInServerComponent } from '~/lib/server/require-user-in-server-component';
|
||||||
@@ -19,7 +19,7 @@ export type UserWorkspace = Awaited<ReturnType<typeof loadUserWorkspace>>;
|
|||||||
export const loadUserWorkspace = cache(workspaceLoader);
|
export const loadUserWorkspace = cache(workspaceLoader);
|
||||||
|
|
||||||
async function workspaceLoader() {
|
async function workspaceLoader() {
|
||||||
const client = getSupabaseServerComponentClient();
|
const client = getSupabaseServerClient();
|
||||||
const api = createAccountsApi(client);
|
const api = createAccountsApi(client);
|
||||||
|
|
||||||
const accountsPromise = shouldLoadAccounts
|
const accountsPromise = shouldLoadAccounts
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { cache } from 'react';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { createAccountsApi } from '@kit/accounts/api';
|
import { createAccountsApi } from '@kit/accounts/api';
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The variable BILLING_MODE represents the billing mode for a service. It can
|
* The variable BILLING_MODE represents the billing mode for a service. It can
|
||||||
@@ -33,7 +33,7 @@ export const loadPersonalAccountBillingPageData = cache(
|
|||||||
);
|
);
|
||||||
|
|
||||||
function personalAccountBillingPageDataLoader(userId: string) {
|
function personalAccountBillingPageDataLoader(userId: string) {
|
||||||
const client = getSupabaseServerComponentClient();
|
const client = getSupabaseServerClient();
|
||||||
const api = createAccountsApi(client);
|
const api = createAccountsApi(client);
|
||||||
|
|
||||||
const data =
|
const data =
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
import { enhanceAction } from '@kit/next/actions';
|
import { enhanceAction } from '@kit/next/actions';
|
||||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import featureFlagsConfig from '~/config/feature-flags.config';
|
import featureFlagsConfig from '~/config/feature-flags.config';
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ export const createPersonalAccountCheckoutSession = enhanceAction(
|
|||||||
throw new Error('Personal account billing is not enabled');
|
throw new Error('Personal account billing is not enabled');
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
const service = createUserBillingService(client);
|
const service = createUserBillingService(client);
|
||||||
|
|
||||||
return await service.createCheckoutSession(data);
|
return await service.createCheckoutSession(data);
|
||||||
@@ -46,7 +46,7 @@ export const createPersonalAccountBillingPortalSession = enhanceAction(
|
|||||||
throw new Error('Personal account billing is not enabled');
|
throw new Error('Personal account billing is not enabled');
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
const service = createUserBillingService(client);
|
const service = createUserBillingService(client);
|
||||||
|
|
||||||
// get url to billing portal
|
// get url to billing portal
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { cache } from 'react';
|
|||||||
|
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
import { createTeamAccountsApi } from '@kit/team-accounts/api';
|
import { createTeamAccountsApi } from '@kit/team-accounts/api';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -29,7 +29,7 @@ const BILLING_MODE = z
|
|||||||
export const loadTeamAccountBillingPage = cache(teamAccountBillingPageLoader);
|
export const loadTeamAccountBillingPage = cache(teamAccountBillingPageLoader);
|
||||||
|
|
||||||
function teamAccountBillingPageLoader(accountId: string) {
|
function teamAccountBillingPageLoader(accountId: string) {
|
||||||
const client = getSupabaseServerComponentClient();
|
const client = getSupabaseServerClient();
|
||||||
const api = createTeamAccountsApi(client);
|
const api = createTeamAccountsApi(client);
|
||||||
|
|
||||||
const data =
|
const data =
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { cache } from 'react';
|
|||||||
|
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
import { createTeamAccountsApi } from '@kit/team-accounts/api';
|
import { createTeamAccountsApi } from '@kit/team-accounts/api';
|
||||||
|
|
||||||
import pathsConfig from '~/config/paths.config';
|
import pathsConfig from '~/config/paths.config';
|
||||||
@@ -26,7 +26,7 @@ export type TeamAccountWorkspace = Awaited<
|
|||||||
export const loadTeamWorkspace = cache(workspaceLoader);
|
export const loadTeamWorkspace = cache(workspaceLoader);
|
||||||
|
|
||||||
async function workspaceLoader(accountSlug: string) {
|
async function workspaceLoader(accountSlug: string) {
|
||||||
const client = getSupabaseServerComponentClient();
|
const client = getSupabaseServerClient();
|
||||||
const api = createTeamAccountsApi(client);
|
const api = createTeamAccountsApi(client);
|
||||||
|
|
||||||
const [workspace, user] = await Promise.all([
|
const [workspace, user] = await Promise.all([
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
import { enhanceAction } from '@kit/next/actions';
|
import { enhanceAction } from '@kit/next/actions';
|
||||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import featureFlagsConfig from '~/config/feature-flags.config';
|
import featureFlagsConfig from '~/config/feature-flags.config';
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ export const createTeamAccountCheckoutSession = enhanceAction(
|
|||||||
throw new Error('Team account billing is not enabled');
|
throw new Error('Team account billing is not enabled');
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
const service = createTeamBillingService(client);
|
const service = createTeamBillingService(client);
|
||||||
|
|
||||||
return service.createCheckout(data);
|
return service.createCheckout(data);
|
||||||
@@ -53,7 +53,7 @@ export const createBillingPortalSession = enhanceAction(
|
|||||||
|
|
||||||
const params = TeamBillingPortalSchema.parse(Object.fromEntries(formData));
|
const params = TeamBillingPortalSchema.parse(Object.fromEntries(formData));
|
||||||
|
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
const service = createTeamBillingService(client);
|
const service = createTeamBillingService(client);
|
||||||
|
|
||||||
// get url to billing portal
|
// get url to billing portal
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import { LineItemSchema } from '@kit/billing';
|
|||||||
import { getBillingGatewayProvider } from '@kit/billing-gateway';
|
import { getBillingGatewayProvider } from '@kit/billing-gateway';
|
||||||
import { getLogger } from '@kit/shared/logger';
|
import { getLogger } from '@kit/shared/logger';
|
||||||
import { requireUser } from '@kit/supabase/require-user';
|
import { requireUser } from '@kit/supabase/require-user';
|
||||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
import { createTeamAccountsApi } from '@kit/team-accounts/api';
|
import { createTeamAccountsApi } from '@kit/team-accounts/api';
|
||||||
|
|
||||||
import appConfig from '~/config/app.config';
|
import appConfig from '~/config/app.config';
|
||||||
@@ -150,7 +150,7 @@ class TeamBillingService {
|
|||||||
accountId: string;
|
accountId: string;
|
||||||
slug: string;
|
slug: string;
|
||||||
}) {
|
}) {
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
const logger = await getLogger();
|
const logger = await getLogger();
|
||||||
|
|
||||||
logger.info(
|
logger.info(
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { notFound, redirect } from 'next/navigation';
|
|||||||
|
|
||||||
import { getBillingGatewayProvider } from '@kit/billing-gateway';
|
import { getBillingGatewayProvider } from '@kit/billing-gateway';
|
||||||
import { BillingSessionStatus } from '@kit/billing-gateway/components';
|
import { BillingSessionStatus } from '@kit/billing-gateway/components';
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import billingConfig from '~/config/billing.config';
|
import billingConfig from '~/config/billing.config';
|
||||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||||
@@ -75,7 +75,7 @@ function BlurryBackdrop() {
|
|||||||
async function loadCheckoutSession(sessionId: string) {
|
async function loadCheckoutSession(sessionId: string) {
|
||||||
await requireUserInServerComponent();
|
await requireUserInServerComponent();
|
||||||
|
|
||||||
const client = getSupabaseServerComponentClient();
|
const client = getSupabaseServerClient();
|
||||||
const gateway = await getBillingGatewayProvider(client);
|
const gateway = await getBillingGatewayProvider(client);
|
||||||
|
|
||||||
const session = await gateway.retrieveCheckoutSession({
|
const session = await gateway.retrieveCheckoutSession({
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { PlusCircle } from 'lucide-react';
|
import { PlusCircle } from 'lucide-react';
|
||||||
|
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
import {
|
import {
|
||||||
AccountInvitationsTable,
|
AccountInvitationsTable,
|
||||||
AccountMembersTable,
|
AccountMembersTable,
|
||||||
@@ -42,7 +42,7 @@ export const generateMetadata = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async function TeamAccountMembersPage({ params }: Params) {
|
async function TeamAccountMembersPage({ params }: Params) {
|
||||||
const client = getSupabaseServerComponentClient();
|
const client = getSupabaseServerClient();
|
||||||
|
|
||||||
const [members, invitations, canAddMember, { user, account }] =
|
const [members, invitations, canAddMember, { user, account }] =
|
||||||
await loadMembersPageData(client, params.account);
|
await loadMembersPageData(client, params.account);
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
import { createTeamAccountsApi } from '@kit/team-accounts/api';
|
import { createTeamAccountsApi } from '@kit/team-accounts/api';
|
||||||
import { TeamAccountSettingsContainer } from '@kit/team-accounts/components';
|
import { TeamAccountSettingsContainer } from '@kit/team-accounts/components';
|
||||||
import { AppBreadcrumbs } from '@kit/ui/app-breadcrumbs';
|
import { AppBreadcrumbs } from '@kit/ui/app-breadcrumbs';
|
||||||
@@ -32,7 +32,7 @@ const paths = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
async function TeamAccountSettingsPage(props: Props) {
|
async function TeamAccountSettingsPage(props: Props) {
|
||||||
const api = createTeamAccountsApi(getSupabaseServerComponentClient());
|
const api = createTeamAccountsApi(getSupabaseServerClient());
|
||||||
const data = await api.getTeamAccount(props.params.account);
|
const data = await api.getTeamAccount(props.params.account);
|
||||||
|
|
||||||
const account = {
|
const account = {
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ import { ArrowLeft } from 'lucide-react';
|
|||||||
|
|
||||||
import { AuthLayoutShell } from '@kit/auth/shared';
|
import { AuthLayoutShell } from '@kit/auth/shared';
|
||||||
import { requireUser } from '@kit/supabase/require-user';
|
import { requireUser } from '@kit/supabase/require-user';
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||||
import { createTeamAccountsApi } from '@kit/team-accounts/api';
|
import { createTeamAccountsApi } from '@kit/team-accounts/api';
|
||||||
import { AcceptInvitationContainer } from '@kit/team-accounts/components';
|
import { AcceptInvitationContainer } from '@kit/team-accounts/components';
|
||||||
import { Button } from '@kit/ui/button';
|
import { Button } from '@kit/ui/button';
|
||||||
@@ -40,7 +41,7 @@ async function JoinTeamAccountPage({ searchParams }: Context) {
|
|||||||
notFound();
|
notFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = getSupabaseServerComponentClient();
|
const client = getSupabaseServerClient();
|
||||||
const auth = await requireUser(client);
|
const auth = await requireUser(client);
|
||||||
|
|
||||||
// if the user is not logged in or there is an error
|
// if the user is not logged in or there is an error
|
||||||
@@ -59,7 +60,7 @@ async function JoinTeamAccountPage({ searchParams }: Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get api to interact with team accounts
|
// get api to interact with team accounts
|
||||||
const adminClient = getSupabaseServerComponentClient({ admin: true });
|
const adminClient = getSupabaseServerAdminClient();
|
||||||
const api = createTeamAccountsApi(client);
|
const api = createTeamAccountsApi(client);
|
||||||
|
|
||||||
// the user is logged in, we can now check if the token is valid
|
// the user is logged in, we can now check if the token is valid
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import Link from 'next/link';
|
|||||||
|
|
||||||
import { ArrowLeft } from 'lucide-react';
|
import { ArrowLeft } from 'lucide-react';
|
||||||
|
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
import { Button } from '@kit/ui/button';
|
import { Button } from '@kit/ui/button';
|
||||||
import { Heading } from '@kit/ui/heading';
|
import { Heading } from '@kit/ui/heading';
|
||||||
import { Trans } from '@kit/ui/trans';
|
import { Trans } from '@kit/ui/trans';
|
||||||
@@ -21,7 +21,7 @@ export const generateMetadata = async () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const NotFoundPage = async () => {
|
const NotFoundPage = async () => {
|
||||||
const client = getSupabaseServerComponentClient();
|
const client = getSupabaseServerClient();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: { user },
|
data: { user },
|
||||||
|
|||||||
@@ -6,15 +6,15 @@ import { redirect } from 'next/navigation';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { enhanceAction } from '@kit/next/actions';
|
import { enhanceAction } from '@kit/next/actions';
|
||||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||||
import { DeletePersonalAccountSchema } from '../schema/delete-personal-account.schema';
|
import { DeletePersonalAccountSchema } from '../schema/delete-personal-account.schema';
|
||||||
import { createDeletePersonalAccountService } from './services/delete-personal-account.service';
|
import { createDeletePersonalAccountService } from './services/delete-personal-account.service';
|
||||||
|
|
||||||
const emailSettings = getEmailSettingsFromEnvironment();
|
const emailSettings = getEmailSettingsFromEnvironment();
|
||||||
|
|
||||||
export async function refreshAuthSession() {
|
export async function refreshAuthSession() {
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
|
|
||||||
await client.auth.refreshSession();
|
await client.auth.refreshSession();
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ export const deletePersonalAccountAction = enhanceAction(
|
|||||||
throw new Error('Invalid form data');
|
throw new Error('Invalid form data');
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
|
|
||||||
// create a new instance of the personal accounts service
|
// create a new instance of the personal accounts service
|
||||||
const service = createDeletePersonalAccountService();
|
const service = createDeletePersonalAccountService();
|
||||||
@@ -42,7 +42,7 @@ export const deletePersonalAccountAction = enhanceAction(
|
|||||||
|
|
||||||
// delete the user's account and cancel all subscriptions
|
// delete the user's account and cancel all subscriptions
|
||||||
await service.deletePersonalAccount({
|
await service.deletePersonalAccount({
|
||||||
adminClient: getSupabaseServerActionClient({ admin: true }),
|
adminClient: getSupabaseServerAdminClient(),
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
userEmail: user.email ?? null,
|
userEmail: user.email ?? null,
|
||||||
emailSettings,
|
emailSettings,
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
} from 'lucide-react';
|
} from 'lucide-react';
|
||||||
|
|
||||||
import { Database } from '@kit/supabase/database';
|
import { Database } from '@kit/supabase/database';
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||||
import { Alert, AlertDescription, AlertTitle } from '@kit/ui/alert';
|
import { Alert, AlertDescription, AlertTitle } from '@kit/ui/alert';
|
||||||
import { AppBreadcrumbs } from '@kit/ui/app-breadcrumbs';
|
import { AppBreadcrumbs } from '@kit/ui/app-breadcrumbs';
|
||||||
import { Badge } from '@kit/ui/badge';
|
import { Badge } from '@kit/ui/badge';
|
||||||
@@ -49,9 +49,7 @@ export function AdminAccountPage(props: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function PersonalAccountPage(props: { account: Account }) {
|
async function PersonalAccountPage(props: { account: Account }) {
|
||||||
const client = getSupabaseServerComponentClient({
|
const client = getSupabaseServerAdminClient();
|
||||||
admin: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
const memberships = await getMemberships(props.account.id);
|
const memberships = await getMemberships(props.account.id);
|
||||||
const { data, error } = await client.auth.admin.getUserById(props.account.id);
|
const { data, error } = await client.auth.admin.getUserById(props.account.id);
|
||||||
@@ -196,9 +194,7 @@ async function TeamAccountPage(props: {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function SubscriptionsTable(props: { accountId: string }) {
|
async function SubscriptionsTable(props: { accountId: string }) {
|
||||||
const client = getSupabaseServerComponentClient({
|
const client = getSupabaseServerAdminClient();
|
||||||
admin: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
const { data: subscription, error } = await client
|
const { data: subscription, error } = await client
|
||||||
.from('subscriptions')
|
.from('subscriptions')
|
||||||
@@ -345,9 +341,7 @@ async function SubscriptionsTable(props: { accountId: string }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function getMemberships(userId: string) {
|
async function getMemberships(userId: string) {
|
||||||
const client = getSupabaseServerComponentClient({
|
const client = getSupabaseServerAdminClient();
|
||||||
admin: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
const memberships = await client
|
const memberships = await client
|
||||||
.from('accounts_memberships')
|
.from('accounts_memberships')
|
||||||
@@ -370,9 +364,7 @@ async function getMemberships(userId: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function getMembers(accountSlug: string) {
|
async function getMembers(accountSlug: string) {
|
||||||
const client = getSupabaseServerComponentClient({
|
const client = getSupabaseServerAdminClient();
|
||||||
admin: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
const members = await client.rpc('get_account_members', {
|
const members = await client.rpc('get_account_members', {
|
||||||
account_slug: accountSlug,
|
account_slug: accountSlug,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { notFound } from 'next/navigation';
|
import { notFound } from 'next/navigation';
|
||||||
|
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import { isSuperAdmin } from '../lib/server/utils/is-super-admin';
|
import { isSuperAdmin } from '../lib/server/utils/is-super-admin';
|
||||||
|
|
||||||
@@ -15,7 +15,7 @@ export function AdminGuard<Params extends object>(
|
|||||||
Component: LayoutOrPageComponent<Params>,
|
Component: LayoutOrPageComponent<Params>,
|
||||||
) {
|
) {
|
||||||
return async function AdminGuardServerComponentWrapper(params: Params) {
|
return async function AdminGuardServerComponentWrapper(params: Params) {
|
||||||
const client = getSupabaseServerComponentClient();
|
const client = getSupabaseServerClient();
|
||||||
const isUserSuperAdmin = await isSuperAdmin(client);
|
const isUserSuperAdmin = await isSuperAdmin(client);
|
||||||
|
|
||||||
// if the user is not a super-admin, we redirect to a 404
|
// if the user is not a super-admin, we redirect to a 404
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ import { redirect } from 'next/navigation';
|
|||||||
|
|
||||||
import { enhanceAction } from '@kit/next/actions';
|
import { enhanceAction } from '@kit/next/actions';
|
||||||
import { getLogger } from '@kit/shared/logger';
|
import { getLogger } from '@kit/shared/logger';
|
||||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
BanUserSchema,
|
BanUserSchema,
|
||||||
@@ -150,14 +151,14 @@ export const deleteAccountAction = adminAction(
|
|||||||
);
|
);
|
||||||
|
|
||||||
function getAdminAuthService() {
|
function getAdminAuthService() {
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
const adminClient = getSupabaseServerActionClient({ admin: true });
|
const adminClient = getSupabaseServerAdminClient();
|
||||||
|
|
||||||
return createAdminAuthUserService(client, adminClient);
|
return createAdminAuthUserService(client, adminClient);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAdminAccountsService() {
|
function getAdminAccountsService() {
|
||||||
const adminClient = getSupabaseServerActionClient({ admin: true });
|
const adminClient = getSupabaseServerAdminClient();
|
||||||
|
|
||||||
return createAdminAccountsService(adminClient);
|
return createAdminAccountsService(adminClient);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import 'server-only';
|
|||||||
|
|
||||||
import { cache } from 'react';
|
import { cache } from 'react';
|
||||||
|
|
||||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import { createAdminDashboardService } from '../services/admin-dashboard.service';
|
import { createAdminDashboardService } from '../services/admin-dashboard.service';
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ import { createAdminDashboardService } from '../services/admin-dashboard.service
|
|||||||
export const loadAdminDashboard = cache(adminDashboardLoader);
|
export const loadAdminDashboard = cache(adminDashboardLoader);
|
||||||
|
|
||||||
function adminDashboardLoader() {
|
function adminDashboardLoader() {
|
||||||
const client = getSupabaseServerComponentClient({ admin: true });
|
const client = getSupabaseServerClient();
|
||||||
const service = createAdminDashboardService(client);
|
const service = createAdminDashboardService(client);
|
||||||
|
|
||||||
return service.getDashboardData();
|
return service.getDashboardData();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { notFound } from 'next/navigation';
|
import { notFound } from 'next/navigation';
|
||||||
|
|
||||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import { isSuperAdmin } from './is-super-admin';
|
import { isSuperAdmin } from './is-super-admin';
|
||||||
|
|
||||||
@@ -11,7 +11,7 @@ import { isSuperAdmin } from './is-super-admin';
|
|||||||
*/
|
*/
|
||||||
export function adminAction<Args, Response>(fn: (params: Args) => Response) {
|
export function adminAction<Args, Response>(fn: (params: Args) => Response) {
|
||||||
return async (params: Args) => {
|
return async (params: Args) => {
|
||||||
const isAdmin = await isSuperAdmin(getSupabaseServerActionClient());
|
const isAdmin = await isSuperAdmin(getSupabaseServerClient());
|
||||||
|
|
||||||
if (!isAdmin) {
|
if (!isAdmin) {
|
||||||
notFound();
|
notFound();
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { redirect } from 'next/navigation';
|
|||||||
|
|
||||||
import { enhanceAction } from '@kit/next/actions';
|
import { enhanceAction } from '@kit/next/actions';
|
||||||
import { getLogger } from '@kit/shared/logger';
|
import { getLogger } from '@kit/shared/logger';
|
||||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import { CreateTeamSchema } from '../../schema/create-team.schema';
|
import { CreateTeamSchema } from '../../schema/create-team.schema';
|
||||||
import { createCreateTeamAccountService } from '../services/create-team-account.service';
|
import { createCreateTeamAccountService } from '../services/create-team-account.service';
|
||||||
@@ -12,7 +12,7 @@ import { createCreateTeamAccountService } from '../services/create-team-account.
|
|||||||
export const createTeamAccountAction = enhanceAction(
|
export const createTeamAccountAction = enhanceAction(
|
||||||
async ({ name }, user) => {
|
async ({ name }, user) => {
|
||||||
const logger = await getLogger();
|
const logger = await getLogger();
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
const service = createCreateTeamAccountService(client);
|
const service = createCreateTeamAccountService(client);
|
||||||
|
|
||||||
const ctx = {
|
const ctx = {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { revalidatePath } from 'next/cache';
|
|||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
|
|
||||||
import { enhanceAction } from '@kit/next/actions';
|
import { enhanceAction } from '@kit/next/actions';
|
||||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||||
|
|
||||||
import { LeaveTeamAccountSchema } from '../../schema/leave-team-account.schema';
|
import { LeaveTeamAccountSchema } from '../../schema/leave-team-account.schema';
|
||||||
import { createLeaveTeamAccountService } from '../services/leave-team-account.service';
|
import { createLeaveTeamAccountService } from '../services/leave-team-account.service';
|
||||||
@@ -15,7 +15,7 @@ export const leaveTeamAccountAction = enhanceAction(
|
|||||||
const params = LeaveTeamAccountSchema.parse(body);
|
const params = LeaveTeamAccountSchema.parse(body);
|
||||||
|
|
||||||
const service = createLeaveTeamAccountService(
|
const service = createLeaveTeamAccountService(
|
||||||
getSupabaseServerActionClient({ admin: true }),
|
getSupabaseServerAdminClient(),
|
||||||
);
|
);
|
||||||
|
|
||||||
await service.leaveTeamAccount({
|
await service.leaveTeamAccount({
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ import { redirect } from 'next/navigation';
|
|||||||
|
|
||||||
import { enhanceAction } from '@kit/next/actions';
|
import { enhanceAction } from '@kit/next/actions';
|
||||||
import { getLogger } from '@kit/shared/logger';
|
import { getLogger } from '@kit/shared/logger';
|
||||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import { UpdateTeamNameSchema } from '../../schema/update-team-name.schema';
|
import { UpdateTeamNameSchema } from '../../schema/update-team-name.schema';
|
||||||
|
|
||||||
export const updateTeamAccountName = enhanceAction(
|
export const updateTeamAccountName = enhanceAction(
|
||||||
async (params) => {
|
async (params) => {
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
const logger = await getLogger();
|
const logger = await getLogger();
|
||||||
const { name, path, slug } = params;
|
const { name, path, slug } = params;
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ import { redirect } from 'next/navigation';
|
|||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
import { enhanceAction } from '@kit/next/actions';
|
import { enhanceAction } from '@kit/next/actions';
|
||||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
import { getSupabaseServerAdminClient } from '@kit/supabase/server-admin-client';
|
||||||
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import { AcceptInvitationSchema } from '../../schema/accept-invitation.schema';
|
import { AcceptInvitationSchema } from '../../schema/accept-invitation.schema';
|
||||||
import { DeleteInvitationSchema } from '../../schema/delete-invitation.schema';
|
import { DeleteInvitationSchema } from '../../schema/delete-invitation.schema';
|
||||||
@@ -22,7 +23,7 @@ import { createAccountPerSeatBillingService } from '../services/account-per-seat
|
|||||||
*/
|
*/
|
||||||
export const createInvitationsAction = enhanceAction(
|
export const createInvitationsAction = enhanceAction(
|
||||||
async (params) => {
|
async (params) => {
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
|
|
||||||
// Create the service
|
// Create the service
|
||||||
const service = createAccountInvitationsService(client);
|
const service = createAccountInvitationsService(client);
|
||||||
@@ -51,7 +52,7 @@ export const createInvitationsAction = enhanceAction(
|
|||||||
*/
|
*/
|
||||||
export const deleteInvitationAction = enhanceAction(
|
export const deleteInvitationAction = enhanceAction(
|
||||||
async (data) => {
|
async (data) => {
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
const service = createAccountInvitationsService(client);
|
const service = createAccountInvitationsService(client);
|
||||||
|
|
||||||
// Delete the invitation
|
// Delete the invitation
|
||||||
@@ -74,7 +75,7 @@ export const deleteInvitationAction = enhanceAction(
|
|||||||
*/
|
*/
|
||||||
export const updateInvitationAction = enhanceAction(
|
export const updateInvitationAction = enhanceAction(
|
||||||
async (invitation) => {
|
async (invitation) => {
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
const service = createAccountInvitationsService(client);
|
const service = createAccountInvitationsService(client);
|
||||||
|
|
||||||
await service.updateInvitation(invitation);
|
await service.updateInvitation(invitation);
|
||||||
@@ -96,7 +97,7 @@ export const updateInvitationAction = enhanceAction(
|
|||||||
*/
|
*/
|
||||||
export const acceptInvitationAction = enhanceAction(
|
export const acceptInvitationAction = enhanceAction(
|
||||||
async (data: FormData, user) => {
|
async (data: FormData, user) => {
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
|
|
||||||
const { inviteToken, nextPath } = AcceptInvitationSchema.parse(
|
const { inviteToken, nextPath } = AcceptInvitationSchema.parse(
|
||||||
Object.fromEntries(data),
|
Object.fromEntries(data),
|
||||||
@@ -106,14 +107,14 @@ export const acceptInvitationAction = enhanceAction(
|
|||||||
const perSeatBillingService = createAccountPerSeatBillingService(client);
|
const perSeatBillingService = createAccountPerSeatBillingService(client);
|
||||||
const service = createAccountInvitationsService(client);
|
const service = createAccountInvitationsService(client);
|
||||||
|
|
||||||
|
// use admin client to accept invitation
|
||||||
|
const adminClient = getSupabaseServerAdminClient();
|
||||||
|
|
||||||
// Accept the invitation
|
// Accept the invitation
|
||||||
const accountId = await service.acceptInvitationToTeam(
|
const accountId = await service.acceptInvitationToTeam(adminClient, {
|
||||||
getSupabaseServerActionClient({ admin: true }),
|
inviteToken,
|
||||||
{
|
userId: user.id,
|
||||||
inviteToken,
|
});
|
||||||
userId: user.id,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// If the account ID is not present, throw an error
|
// If the account ID is not present, throw an error
|
||||||
if (!accountId) {
|
if (!accountId) {
|
||||||
@@ -134,7 +135,7 @@ export const acceptInvitationAction = enhanceAction(
|
|||||||
*/
|
*/
|
||||||
export const renewInvitationAction = enhanceAction(
|
export const renewInvitationAction = enhanceAction(
|
||||||
async (params) => {
|
async (params) => {
|
||||||
const client = getSupabaseServerActionClient();
|
const client = getSupabaseServerClient();
|
||||||
const { invitationId } = RenewInvitationSchema.parse(params);
|
const { invitationId } = RenewInvitationSchema.parse(params);
|
||||||
|
|
||||||
const service = createAccountInvitationsService(client);
|
const service = createAccountInvitationsService(client);
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import { ZodType, z } from 'zod';
|
|||||||
|
|
||||||
import { verifyCaptchaToken } from '@kit/auth/captcha/server';
|
import { verifyCaptchaToken } from '@kit/auth/captcha/server';
|
||||||
import { requireUser } from '@kit/supabase/require-user';
|
import { requireUser } from '@kit/supabase/require-user';
|
||||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import { captureException, zodParseFactory } from '../utils';
|
import { captureException, zodParseFactory } from '../utils';
|
||||||
|
|
||||||
@@ -63,7 +63,7 @@ export function enhanceAction<
|
|||||||
// verify the user is authenticated if required
|
// verify the user is authenticated if required
|
||||||
if (requireAuth) {
|
if (requireAuth) {
|
||||||
// verify the user is authenticated if required
|
// verify the user is authenticated if required
|
||||||
const auth = await requireUser(getSupabaseServerActionClient());
|
const auth = await requireUser(getSupabaseServerClient());
|
||||||
|
|
||||||
// If the user is not authenticated, redirect to the specified URL.
|
// If the user is not authenticated, redirect to the specified URL.
|
||||||
if (!auth.data) {
|
if (!auth.data) {
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { z } from 'zod';
|
|||||||
|
|
||||||
import { verifyCaptchaToken } from '@kit/auth/captcha/server';
|
import { verifyCaptchaToken } from '@kit/auth/captcha/server';
|
||||||
import { requireUser } from '@kit/supabase/require-user';
|
import { requireUser } from '@kit/supabase/require-user';
|
||||||
import { getSupabaseRouteHandlerClient } from '@kit/supabase/route-handler-client';
|
import { getSupabaseServerClient } from '@kit/supabase/server-client';
|
||||||
|
|
||||||
import { captureException, zodParseFactory } from '../utils';
|
import { captureException, zodParseFactory } from '../utils';
|
||||||
|
|
||||||
@@ -96,7 +96,7 @@ export const enhanceRouteHandler = <
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = getSupabaseRouteHandlerClient();
|
const client = getSupabaseServerClient();
|
||||||
|
|
||||||
const shouldVerifyAuth = params?.auth ?? true;
|
const shouldVerifyAuth = params?.auth ?? true;
|
||||||
|
|
||||||
|
|||||||
@@ -10,11 +10,13 @@
|
|||||||
},
|
},
|
||||||
"prettier": "@kit/prettier-config",
|
"prettier": "@kit/prettier-config",
|
||||||
"exports": {
|
"exports": {
|
||||||
"./middleware-client": "./src/clients/middleware.client.ts",
|
"./server-client": "./src/clients/server-client.ts",
|
||||||
"./server-actions-client": "./src/clients/server-actions.client.ts",
|
"./server-admin-client": "./src/clients/server-admin-client.ts",
|
||||||
"./route-handler-client": "./src/clients/route-handler.client.ts",
|
"./middleware-client": "./src/clients/middleware-client.ts",
|
||||||
"./server-component-client": "./src/clients/server-component.client.ts",
|
"./server-actions-client": "./src/clients/server-actions-client.ts",
|
||||||
"./browser-client": "./src/clients/browser.client.ts",
|
"./route-handler-client": "./src/clients/route-handler-client.ts",
|
||||||
|
"./server-component-client": "./src/clients/server-component-client.ts",
|
||||||
|
"./browser-client": "./src/clients/browser-client.ts",
|
||||||
"./check-requires-mfa": "./src/check-requires-mfa.ts",
|
"./check-requires-mfa": "./src/check-requires-mfa.ts",
|
||||||
"./require-user": "./src/require-user.ts",
|
"./require-user": "./src/require-user.ts",
|
||||||
"./hooks/*": "./src/hooks/*.ts",
|
"./hooks/*": "./src/hooks/*.ts",
|
||||||
|
|||||||
38
packages/supabase/src/clients/middleware-client.ts
Normal file
38
packages/supabase/src/clients/middleware-client.ts
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
import 'server-only';
|
||||||
|
|
||||||
|
import { type NextRequest, NextResponse } from 'next/server';
|
||||||
|
|
||||||
|
import { createServerClient } from '@supabase/ssr';
|
||||||
|
|
||||||
|
import { Database } from '../database.types';
|
||||||
|
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a middleware client for Supabase.
|
||||||
|
*
|
||||||
|
* @param {NextRequest} request - The Next.js request object.
|
||||||
|
* @param {NextResponse} response - The Next.js response object.
|
||||||
|
*/
|
||||||
|
export function createMiddlewareClient<GenericSchema = Database>(
|
||||||
|
request: NextRequest,
|
||||||
|
response: NextResponse,
|
||||||
|
) {
|
||||||
|
const keys = getSupabaseClientKeys();
|
||||||
|
|
||||||
|
return createServerClient<GenericSchema>(keys.url, keys.anonKey, {
|
||||||
|
cookies: {
|
||||||
|
getAll() {
|
||||||
|
return request.cookies.getAll();
|
||||||
|
},
|
||||||
|
setAll(cookiesToSet) {
|
||||||
|
cookiesToSet.forEach(({ name, value }) =>
|
||||||
|
request.cookies.set(name, value),
|
||||||
|
);
|
||||||
|
|
||||||
|
cookiesToSet.forEach(({ name, value, options }) =>
|
||||||
|
response.cookies.set(name, value, options),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
import { type NextRequest, NextResponse } from 'next/server';
|
|
||||||
|
|
||||||
import { type CookieOptions, createServerClient } from '@supabase/ssr';
|
|
||||||
|
|
||||||
import { Database } from '../database.types';
|
|
||||||
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a middleware client for Supabase.
|
|
||||||
*
|
|
||||||
* @param {NextRequest} request - The Next.js request object.
|
|
||||||
* @param {NextResponse} response - The Next.js response object.
|
|
||||||
*/
|
|
||||||
export function createMiddlewareClient<GenericSchema = Database>(
|
|
||||||
request: NextRequest,
|
|
||||||
response: NextResponse,
|
|
||||||
) {
|
|
||||||
const keys = getSupabaseClientKeys();
|
|
||||||
|
|
||||||
return createServerClient<GenericSchema>(keys.url, keys.anonKey, {
|
|
||||||
cookies: getCookieStrategy(request, response),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCookieStrategy(request: NextRequest, response: NextResponse) {
|
|
||||||
return {
|
|
||||||
set: (name: string, value: string, options: CookieOptions) => {
|
|
||||||
request.cookies.set({ name, value, ...options });
|
|
||||||
|
|
||||||
response = NextResponse.next({
|
|
||||||
request: {
|
|
||||||
headers: request.headers,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
response.cookies.set({
|
|
||||||
name,
|
|
||||||
value,
|
|
||||||
...options,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
get: (name: string) => {
|
|
||||||
return request.cookies.get(name)?.value;
|
|
||||||
},
|
|
||||||
remove: (name: string, options: CookieOptions) => {
|
|
||||||
request.cookies.set({
|
|
||||||
name,
|
|
||||||
value: '',
|
|
||||||
...options,
|
|
||||||
});
|
|
||||||
|
|
||||||
response = NextResponse.next({
|
|
||||||
request: {
|
|
||||||
headers: request.headers,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
response.cookies.set({
|
|
||||||
name,
|
|
||||||
value: '',
|
|
||||||
...options,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -20,6 +20,7 @@ const keys = getSupabaseClientKeys();
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @name getSupabaseRouteHandlerClient
|
* @name getSupabaseRouteHandlerClient
|
||||||
|
* @deprecated Use `getSupabaseServerClient` instead.
|
||||||
* @description Get a Supabase client for use in the Route Handler Routes
|
* @description Get a Supabase client for use in the Route Handler Routes
|
||||||
*/
|
*/
|
||||||
export function getSupabaseRouteHandlerClient<GenericSchema = Database>(
|
export function getSupabaseRouteHandlerClient<GenericSchema = Database>(
|
||||||
@@ -25,6 +25,11 @@ function createServerSupabaseClient<
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name getSupabaseServerComponentClient
|
||||||
|
* @deprecated Use `getSupabaseServerClient` instead.
|
||||||
|
* @param params
|
||||||
|
*/
|
||||||
export function getSupabaseServerActionClient<
|
export function getSupabaseServerActionClient<
|
||||||
GenericSchema extends Database = Database,
|
GenericSchema extends Database = Database,
|
||||||
>(params?: { admin: boolean }) {
|
>(params?: { admin: boolean }) {
|
||||||
31
packages/supabase/src/clients/server-admin-client.ts
Normal file
31
packages/supabase/src/clients/server-admin-client.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import 'server-only';
|
||||||
|
|
||||||
|
import { unstable_noStore as noStore } from 'next/cache';
|
||||||
|
|
||||||
|
import { createClient } from '@supabase/supabase-js';
|
||||||
|
|
||||||
|
import { Database } from '../database.types';
|
||||||
|
import {
|
||||||
|
getServiceRoleKey,
|
||||||
|
warnServiceRoleKeyUsage,
|
||||||
|
} from '../get-service-role-key';
|
||||||
|
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name getSupabaseServerAdminClient
|
||||||
|
* @description Get a Supabase client for use in the Server with admin access to the database.
|
||||||
|
*/
|
||||||
|
export function getSupabaseServerAdminClient<GenericSchema = Database>() {
|
||||||
|
noStore();
|
||||||
|
warnServiceRoleKeyUsage();
|
||||||
|
|
||||||
|
const url = getSupabaseClientKeys().url;
|
||||||
|
|
||||||
|
return createClient<GenericSchema>(url, getServiceRoleKey(), {
|
||||||
|
auth: {
|
||||||
|
persistSession: false,
|
||||||
|
detectSessionInUrl: false,
|
||||||
|
autoRefreshToken: false,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
39
packages/supabase/src/clients/server-client.ts
Normal file
39
packages/supabase/src/clients/server-client.ts
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
import 'server-only';
|
||||||
|
|
||||||
|
import { unstable_noStore as noStore } from 'next/cache';
|
||||||
|
import { cookies } from 'next/headers';
|
||||||
|
|
||||||
|
import { createServerClient } from '@supabase/ssr';
|
||||||
|
|
||||||
|
import { Database } from '../database.types';
|
||||||
|
import { getSupabaseClientKeys } from '../get-supabase-client-keys';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @name getSupabaseServerClient
|
||||||
|
* @description Creates a Supabase client for use in the Server.
|
||||||
|
*/
|
||||||
|
export function getSupabaseServerClient<GenericSchema = Database>() {
|
||||||
|
noStore();
|
||||||
|
|
||||||
|
const cookieStore = cookies();
|
||||||
|
const keys = getSupabaseClientKeys();
|
||||||
|
|
||||||
|
return createServerClient<GenericSchema>(keys.url, keys.anonKey, {
|
||||||
|
cookies: {
|
||||||
|
getAll() {
|
||||||
|
return cookieStore.getAll();
|
||||||
|
},
|
||||||
|
setAll(cookiesToSet) {
|
||||||
|
try {
|
||||||
|
cookiesToSet.forEach(({ name, value, options }) =>
|
||||||
|
cookieStore.set(name, value, options),
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
// The `setAll` method was called from a Server Component.
|
||||||
|
// This can be ignored if you have middleware refreshing
|
||||||
|
// user sessions.
|
||||||
|
}
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { useMemo } from 'react';
|
import { useMemo } from 'react';
|
||||||
|
|
||||||
import { getSupabaseBrowserClient } from '../clients/browser.client';
|
import { getSupabaseBrowserClient } from '../clients/browser-client';
|
||||||
|
|
||||||
export function useSupabase() {
|
export function useSupabase() {
|
||||||
return useMemo(() => getSupabaseBrowserClient(), []);
|
return useMemo(() => getSupabaseBrowserClient(), []);
|
||||||
|
|||||||
Reference in New Issue
Block a user