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:
giancarlo
2024-03-29 15:52:32 +08:00
parent 40c730141a
commit 2b0fbc445b
22 changed files with 121 additions and 105 deletions

View File

@@ -7,7 +7,7 @@ import { z } from 'zod';
import { Mailer } from '@kit/mailers';
import { Logger } from '@kit/shared/logger';
import { Database } from '@kit/supabase/database';
import { requireAuth } from '@kit/supabase/require-auth';
import { requireUser } from '@kit/supabase/require-user';
import { DeleteInvitationSchema } from '../../schema/delete-invitation.schema';
import { InviteMembersSchema } from '../../schema/invite-members.schema';
@@ -102,8 +102,7 @@ export class AccountInvitationsService {
);
const mailer = new Mailer();
const { user } = await this.getUser();
const user = await this.getUser();
const accountResponse = await this.client
.from('accounts')
@@ -258,7 +257,7 @@ export class AccountInvitationsService {
}
private async getUser() {
const { data, error } = await requireAuth(this.client);
const { data, error } = await requireUser(this.client);
if (error ?? !data) {
throw new Error('Authentication required');

View File

@@ -1,20 +0,0 @@
import { SupabaseClient } from '@supabase/supabase-js';
import 'server-only';
import { z } from 'zod';
import { Database } from '@kit/supabase/database';
import { LeaveTeamAccountSchema } from '../../schema/leave-team-account.schema';
export class LeaveAccountService {
constructor(private readonly client: SupabaseClient<Database>) {}
async leaveTeamAccount(params: z.infer<typeof LeaveTeamAccountSchema>) {
await Promise.resolve();
console.log(params);
// TODO
// implement this method
}
}

View File

@@ -0,0 +1,31 @@
import { SupabaseClient } from '@supabase/supabase-js';
import 'server-only';
import { z } from 'zod';
import { Database } from '@kit/supabase/database';
const Schema = z.object({
accountId: z.string(),
userId: z.string(),
});
export class LeaveTeamAccountService {
constructor(private readonly adminClient: SupabaseClient<Database>) {}
async leaveTeamAccount(params: z.infer<typeof Schema>) {
const { accountId, userId } = Schema.parse(params);
const { error } = await this.adminClient
.from('accounts_memberships')
.delete()
.match({
account_id: accountId,
user_id: userId,
});
if (error) {
throw error;
}
}
}