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:
@@ -5,7 +5,7 @@ import { redirect } from 'next/navigation';
|
||||
import { z } from 'zod';
|
||||
|
||||
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 { CreateTeamSchema } from '../../schema/create-team.schema';
|
||||
@@ -25,13 +25,13 @@ export async function createOrganizationAccountAction(
|
||||
|
||||
const client = getSupabaseServerActionClient();
|
||||
const service = new CreateTeamAccountService(client);
|
||||
const session = await requireAuth(client);
|
||||
const auth = await requireUser(client);
|
||||
|
||||
if (session.error) {
|
||||
redirect(session.redirectTo);
|
||||
if (auth.error) {
|
||||
redirect(auth.redirectTo);
|
||||
}
|
||||
|
||||
const userId = session.data.user.id;
|
||||
const userId = auth.data.id;
|
||||
|
||||
const createAccountResponse = await service.createNewOrganizationAccount({
|
||||
name: accountName,
|
||||
|
||||
@@ -5,7 +5,7 @@ import { redirect } from 'next/navigation';
|
||||
import { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
import { requireAuth } from '@kit/supabase/require-auth';
|
||||
import { requireUser } from '@kit/supabase/require-user';
|
||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
||||
|
||||
import { DeleteTeamAccountSchema } from '../../schema/delete-team-account.schema';
|
||||
@@ -17,7 +17,7 @@ export async function deleteTeamAccountAction(formData: FormData) {
|
||||
);
|
||||
|
||||
const client = getSupabaseServerActionClient();
|
||||
const auth = await requireAuth(client);
|
||||
const auth = await requireUser(client);
|
||||
|
||||
if (auth.error) {
|
||||
throw new Error('Authentication required');
|
||||
@@ -36,7 +36,7 @@ export async function deleteTeamAccountAction(formData: FormData) {
|
||||
}),
|
||||
{
|
||||
accountId: params.accountId,
|
||||
userId: auth.data.user.id,
|
||||
userId: auth.data.id,
|
||||
},
|
||||
);
|
||||
|
||||
@@ -47,13 +47,13 @@ async function assertUserPermissionsToDeleteTeamAccount(
|
||||
client: SupabaseClient<Database>,
|
||||
accountId: string,
|
||||
) {
|
||||
const auth = await requireAuth(client);
|
||||
const auth = await requireUser(client);
|
||||
|
||||
if (auth.error ?? !auth.data.user.id) {
|
||||
if (auth.error ?? !auth.data.id) {
|
||||
throw new Error('Authentication required');
|
||||
}
|
||||
|
||||
const userId = auth.data.user.id;
|
||||
const userId = auth.data.id;
|
||||
|
||||
const { data, error } = await client
|
||||
.from('accounts')
|
||||
|
||||
@@ -1,16 +1,35 @@
|
||||
'use server';
|
||||
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import { redirect } from 'next/navigation';
|
||||
|
||||
import { requireUser } from '@kit/supabase/require-user';
|
||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
||||
|
||||
import { LeaveTeamAccountSchema } from '../../schema/leave-team-account.schema';
|
||||
import { LeaveAccountService } from '../services/leave-account.service';
|
||||
import { LeaveTeamAccountService } from '../services/leave-team-account.service';
|
||||
|
||||
export async function leaveTeamAccountAction(formData: FormData) {
|
||||
const body = Object.fromEntries(formData.entries());
|
||||
const params = LeaveTeamAccountSchema.parse(body);
|
||||
const service = new LeaveAccountService(getSupabaseServerActionClient());
|
||||
const client = getSupabaseServerActionClient();
|
||||
|
||||
await service.leaveTeamAccount(params);
|
||||
const auth = await requireUser(client);
|
||||
|
||||
return { success: true };
|
||||
if (auth.error) {
|
||||
throw new Error('Authentication required');
|
||||
}
|
||||
|
||||
const service = new LeaveTeamAccountService(
|
||||
getSupabaseServerActionClient({ admin: true }),
|
||||
);
|
||||
|
||||
await service.leaveTeamAccount({
|
||||
accountId: params.accountId,
|
||||
userId: auth.data.id,
|
||||
});
|
||||
|
||||
revalidatePath('/home/[account]', 'layout');
|
||||
|
||||
return redirect('/home');
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import { SupabaseClient } from '@supabase/supabase-js';
|
||||
import { z } from 'zod';
|
||||
|
||||
import { Database } from '@kit/supabase/database';
|
||||
import { requireAuth } from '@kit/supabase/require-auth';
|
||||
import { requireUser } from '@kit/supabase/require-user';
|
||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
||||
|
||||
import { AcceptInvitationSchema } from '../../schema/accept-invitation.schema';
|
||||
@@ -94,7 +94,7 @@ export async function acceptInvitationAction(data: FormData) {
|
||||
Object.fromEntries(data),
|
||||
);
|
||||
|
||||
const { user } = await assertSession(client);
|
||||
const user = await assertSession(client);
|
||||
|
||||
const service = new AccountInvitationsService(client);
|
||||
|
||||
@@ -123,7 +123,7 @@ export async function renewInvitationAction(params: { invitationId: number }) {
|
||||
}
|
||||
|
||||
async function assertSession(client: SupabaseClient<Database>) {
|
||||
const { error, data } = await requireAuth(client);
|
||||
const { error, data } = await requireUser(client);
|
||||
|
||||
if (error) {
|
||||
throw new Error(`Authentication required`);
|
||||
|
||||
Reference in New Issue
Block a user