Refactor authentication method to requireUser
Replaced the requireAuth method with requireUser to improve clarity and modified all instances where it was used. Renamed the import throughout multiple files and services and made changes accordingly, thus making it more specific and understandable that a logged-in user is needed. The return type of the method was also updated from Session to User to more accurately reflect the information it provides.
This commit is contained in:
@@ -30,4 +30,4 @@ NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY=
|
||||
|
||||
### Supabase
|
||||
|
||||
Please follow the instructions in the [Supabase README](../supabase/README.md) to setup your Supabase project.
|
||||
Please follow the instructions in the [Supabase README](../supabase/README.md) to setup your Supabase project.
|
||||
|
||||
@@ -7,7 +7,7 @@ import { z } from 'zod';
|
||||
import { getLineItemsFromPlanId } from '@kit/billing';
|
||||
import { getBillingGatewayProvider } from '@kit/billing-gateway';
|
||||
import { Logger } from '@kit/shared/logger';
|
||||
import { requireAuth } from '@kit/supabase/require-auth';
|
||||
import { requireUser } from '@kit/supabase/require-user';
|
||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
||||
|
||||
import appConfig from '~/config/app.config';
|
||||
@@ -25,9 +25,9 @@ export async function createPersonalAccountCheckoutSession(params: {
|
||||
productId: string;
|
||||
}) {
|
||||
const client = getSupabaseServerActionClient();
|
||||
const { data, error } = await requireAuth(client);
|
||||
const { data: user, error } = await requireUser(client);
|
||||
|
||||
if (error ?? !data.user) {
|
||||
if (error ?? !user) {
|
||||
throw new Error('Authentication required');
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ export async function createPersonalAccountCheckoutSession(params: {
|
||||
|
||||
// in the case of personal accounts
|
||||
// the account ID is the same as the user ID
|
||||
const accountId = data.user.id;
|
||||
const accountId = user.id;
|
||||
|
||||
// the return URL for the checkout session
|
||||
const returnUrl = getCheckoutSessionReturnUrl();
|
||||
@@ -74,13 +74,13 @@ export async function createPersonalAccountCheckoutSession(params: {
|
||||
accountId,
|
||||
trialDays,
|
||||
paymentType: product.paymentType,
|
||||
customerEmail: data.user.email,
|
||||
customerEmail: user.email,
|
||||
customerId,
|
||||
});
|
||||
|
||||
Logger.info(
|
||||
{
|
||||
userId: data.user.id,
|
||||
userId: user.id,
|
||||
},
|
||||
`Checkout session created. Returning checkout token to client...`,
|
||||
);
|
||||
|
||||
@@ -43,6 +43,8 @@ export const loadTeamWorkspace = cache(async (accountSlug: string) => {
|
||||
|
||||
const accountData = accountResult.data[0];
|
||||
|
||||
// we cannot find any record for the selected organization
|
||||
// so we redirect the user to the home page
|
||||
if (!accountData) {
|
||||
return redirect(pathsConfig.app.home);
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import { notFound } from 'next/navigation';
|
||||
|
||||
import { getBillingGatewayProvider } from '@kit/billing-gateway';
|
||||
import { BillingSessionStatus } from '@kit/billing-gateway/components';
|
||||
import { requireAuth } from '@kit/supabase/require-auth';
|
||||
import { requireUser } from '@kit/supabase/require-user';
|
||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
||||
|
||||
import billingConfig from '~/config/billing.config';
|
||||
@@ -66,8 +66,11 @@ export default withI18n(ReturnStripeSessionPage);
|
||||
|
||||
export async function loadCheckoutSession(sessionId: string) {
|
||||
const client = getSupabaseServerComponentClient();
|
||||
const { error } = await requireUser(client);
|
||||
|
||||
await requireAuth(client);
|
||||
if (error) {
|
||||
throw new Error('Authentication required');
|
||||
}
|
||||
|
||||
const gateway = await getBillingGatewayProvider(client);
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import { z } from 'zod';
|
||||
|
||||
import { getLineItemsFromPlanId } from '@kit/billing';
|
||||
import { getBillingGatewayProvider } from '@kit/billing-gateway';
|
||||
import { requireAuth } from '@kit/supabase/require-auth';
|
||||
import { requireUser } from '@kit/supabase/require-user';
|
||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
||||
|
||||
import appConfig from '~/config/app.config';
|
||||
@@ -33,13 +33,13 @@ export async function createTeamAccountCheckoutSession(params: {
|
||||
const productId = z.string().min(1).parse(params.productId);
|
||||
|
||||
// we require the user to be authenticated
|
||||
const { data: session } = await requireAuth(client);
|
||||
const { data: user } = await requireUser(client);
|
||||
|
||||
if (!session) {
|
||||
if (!user) {
|
||||
throw new Error('Authentication required');
|
||||
}
|
||||
|
||||
const userId = session.user.id;
|
||||
const userId = user.id;
|
||||
const accountId = params.accountId;
|
||||
|
||||
const hasPermission = await getPermissionsForAccountId(userId, accountId);
|
||||
@@ -67,7 +67,7 @@ export async function createTeamAccountCheckoutSession(params: {
|
||||
// find the customer ID for the account if it exists
|
||||
// (eg. if the account has been billed before)
|
||||
const customerId = await getCustomerIdFromAccountId(client, accountId);
|
||||
const customerEmail = session.user.email;
|
||||
const customerEmail = user.email;
|
||||
|
||||
// the return URL for the checkout session
|
||||
const returnUrl = getCheckoutSessionReturnUrl(params.slug);
|
||||
@@ -100,13 +100,13 @@ export async function createBillingPortalSession(formData: FormData) {
|
||||
})
|
||||
.parse(Object.fromEntries(formData));
|
||||
|
||||
const { data: session, error } = await requireAuth(client);
|
||||
const { data: user, error } = await requireUser(client);
|
||||
|
||||
if (error ?? !session) {
|
||||
if (error ?? !user) {
|
||||
throw new Error('Authentication required');
|
||||
}
|
||||
|
||||
const userId = session.user.id;
|
||||
const userId = user.id;
|
||||
|
||||
// we require the user to have permissions to manage billing for the account
|
||||
const hasPermission = await getPermissionsForAccountId(userId, accountId);
|
||||
|
||||
@@ -4,7 +4,7 @@ import { notFound, redirect } from 'next/navigation';
|
||||
import { ArrowLeft } from 'lucide-react';
|
||||
|
||||
import { Logger } from '@kit/shared/logger';
|
||||
import { requireAuth } from '@kit/supabase/require-auth';
|
||||
import { requireUser } from '@kit/supabase/require-user';
|
||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
||||
import { AcceptInvitationContainer } from '@kit/team-accounts/components';
|
||||
import { Button } from '@kit/ui/button';
|
||||
@@ -12,6 +12,7 @@ import { Heading } from '@kit/ui/heading';
|
||||
import { Trans } from '@kit/ui/trans';
|
||||
|
||||
import pathsConfig from '~/config/paths.config';
|
||||
import { createI18nServerInstance } from '~/lib/i18n/i18n.server';
|
||||
import { withI18n } from '~/lib/i18n/with-i18n';
|
||||
|
||||
interface Context {
|
||||
@@ -20,9 +21,11 @@ interface Context {
|
||||
};
|
||||
}
|
||||
|
||||
export const generateMetadata = () => {
|
||||
export const generateMetadata = async () => {
|
||||
const i18n = await createI18nServerInstance();
|
||||
|
||||
return {
|
||||
title: 'Join Team Account',
|
||||
title: i18n.t('teams:joinTeamAccount'),
|
||||
};
|
||||
};
|
||||
|
||||
@@ -35,12 +38,12 @@ async function JoinTeamAccountPage({ searchParams }: Context) {
|
||||
}
|
||||
|
||||
const client = getSupabaseServerComponentClient();
|
||||
const session = await requireAuth(client);
|
||||
const auth = await requireUser(client);
|
||||
|
||||
// if the user is not logged in or there is an error
|
||||
// redirect to the sign up page with the invite token
|
||||
// so that they will get back to this page after signing up
|
||||
if (session.error ?? !session.data) {
|
||||
if (auth.error ?? !auth.data) {
|
||||
redirect(pathsConfig.auth.signUp + '?invite_token=' + token);
|
||||
}
|
||||
|
||||
@@ -61,7 +64,7 @@ async function JoinTeamAccountPage({ searchParams }: Context) {
|
||||
{
|
||||
name: 'join-team-account',
|
||||
accountId: invitation.account.id,
|
||||
userId: session.data.user.id,
|
||||
userId: auth.data.id,
|
||||
},
|
||||
'User is already in the account. Redirecting to account page.',
|
||||
);
|
||||
@@ -137,8 +140,6 @@ async function getInviteDataFromInviteToken(token: string) {
|
||||
.gte('expires_at', new Date().toISOString())
|
||||
.single();
|
||||
|
||||
console.log(invitation, error);
|
||||
|
||||
if (!invitation ?? error) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { redirect } from 'next/navigation';
|
||||
|
||||
import { PasswordResetForm } from '@kit/auth/password-reset';
|
||||
import { AuthLayoutShell } from '@kit/auth/shared';
|
||||
import { requireAuth } from '@kit/supabase/require-auth';
|
||||
import { requireUser } from '@kit/supabase/require-user';
|
||||
import { getSupabaseServerComponentClient } from '@kit/supabase/server-component-client';
|
||||
|
||||
import { AppLogo } from '~/components/app-logo';
|
||||
@@ -11,7 +11,7 @@ import { withI18n } from '~/lib/i18n/with-i18n';
|
||||
|
||||
async function PasswordResetPage() {
|
||||
const client = getSupabaseServerComponentClient();
|
||||
const auth = await requireAuth(client);
|
||||
const auth = await requireUser(client);
|
||||
|
||||
// we require the user to be logged in to access this page
|
||||
if (auth.error) {
|
||||
|
||||
@@ -152,5 +152,6 @@
|
||||
"acceptInvitationHeading": "Accept Invitation to join {{accountName}}",
|
||||
"acceptInvitationDescription": "You have been invited to join the team {{accountName}}. If you wish to accept the invitation, please click the button below.",
|
||||
"joinTeam": "Join {{accountName}}",
|
||||
"joinTeamAccount": "Join Team",
|
||||
"joiningTeam": "Joining team..."
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user