Updated account deletion process and refactor packages
The primary update was on the process of account deletion where email notifications are now sent to users. The @kit/emails was also renamed to @kit/email-templates and adjustments were accordingly made on the relevant code and configuration files. In addition, package interaction was refactored to enhance readability and ease of maintenance. Some minor alterations were made on the User Interface, and code comments were updated.
This commit is contained in:
@@ -2,12 +2,16 @@
|
||||
|
||||
import { RedirectType, redirect } from 'next/navigation';
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
import { Logger } from '@kit/shared/logger';
|
||||
import { requireAuth } from '@kit/supabase/require-auth';
|
||||
import { getSupabaseServerActionClient } from '@kit/supabase/server-actions-client';
|
||||
|
||||
import { PersonalAccountsService } from './services/personal-accounts.service';
|
||||
|
||||
const emailSettings = getEmailSettingsFromEnvironment();
|
||||
|
||||
export async function refreshAuthSession() {
|
||||
const client = getSupabaseServerActionClient();
|
||||
|
||||
@@ -23,40 +27,45 @@ export async function deletePersonalAccountAction(formData: FormData) {
|
||||
throw new Error('Confirmation required to delete account');
|
||||
}
|
||||
|
||||
const session = await requireAuth(getSupabaseServerActionClient());
|
||||
const client = getSupabaseServerActionClient();
|
||||
const session = await requireAuth(client);
|
||||
|
||||
if (session.error) {
|
||||
Logger.error(`User is not authenticated. Redirecting to login page`);
|
||||
|
||||
redirect(session.redirectTo);
|
||||
}
|
||||
|
||||
const client = getSupabaseServerActionClient();
|
||||
const service = new PersonalAccountsService(client);
|
||||
// retrieve user ID and email
|
||||
const userId = session.data.user.id;
|
||||
const userEmail = session.data.user.email ?? null;
|
||||
|
||||
Logger.info(
|
||||
{
|
||||
userId,
|
||||
name: 'accounts',
|
||||
},
|
||||
`Deleting personal account...`,
|
||||
);
|
||||
// create a new instance of the personal accounts service
|
||||
const service = new PersonalAccountsService(client);
|
||||
|
||||
await service.deletePersonalAccount(
|
||||
getSupabaseServerActionClient({ admin: true }),
|
||||
{
|
||||
userId,
|
||||
},
|
||||
);
|
||||
|
||||
Logger.info(
|
||||
{
|
||||
userId,
|
||||
name: 'accounts',
|
||||
},
|
||||
`Personal account deleted successfully.`,
|
||||
);
|
||||
// delete the user's account and cancel all subscriptions
|
||||
await service.deletePersonalAccount({
|
||||
adminClient: getSupabaseServerActionClient({ admin: true }),
|
||||
userId,
|
||||
userEmail,
|
||||
emailSettings,
|
||||
});
|
||||
|
||||
// sign out the user after deleting their account
|
||||
await client.auth.signOut();
|
||||
|
||||
// redirect to the home page
|
||||
redirect('/', RedirectType.replace);
|
||||
}
|
||||
|
||||
function getEmailSettingsFromEnvironment() {
|
||||
return z
|
||||
.object({
|
||||
fromEmail: z.string().email(),
|
||||
productName: z.string().min(1),
|
||||
})
|
||||
.parse({
|
||||
fromEmail: process.env.EMAIL_SENDER,
|
||||
productName: process.env.NEXT_PUBLIC_PRODUCT_NAME,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user