From 07060e1350788ded394c931b9699cd3670b8fe21 Mon Sep 17 00:00:00 2001 From: Jason Creviston Date: Thu, 4 Jul 2024 08:35:09 -0400 Subject: [PATCH] feat: delete users by uuid --- .../(authenticated)/self/+page.server.ts | 22 +++++++++++++++++++ src/routes/(authenticated)/self/+page.svelte | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/src/routes/(authenticated)/self/+page.server.ts b/src/routes/(authenticated)/self/+page.server.ts index 72a298f..b00bdfb 100644 --- a/src/routes/(authenticated)/self/+page.server.ts +++ b/src/routes/(authenticated)/self/+page.server.ts @@ -1,5 +1,8 @@ import type { Provider } from "@supabase/supabase-js" import { fail, redirect } from "@sveltejs/kit" +import { PUBLIC_SUPABASE_URL } from '$env/static/public' +import { SUPABASE_SERVICE_ROLE_KEY } from '$env/static/private' +import { createClient } from '@supabase/supabase-js' export const load = async ({ locals: { getSession } }) => { /** @@ -59,6 +62,25 @@ export const actions = { if (data.url) redirect(303, data.url) }, + delete_user: async({ request}) => { + const formData = await request.formData() + const user = formData.get('user') as string + + if (!user) { + return fail(400, { + error: 'Please enter a user id.' + }) + } + + const supabase = createClient(PUBLIC_SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY) + + const { error } = await supabase.auth.admin.deleteUser(user) + + if (error) + return fail(400, { error: error.message }) + + return { message: 'User deleted.' } + }, password: async({ request, locals: { supabase } }) => { const formData = await request.formData() const password = formData.get('password') as string diff --git a/src/routes/(authenticated)/self/+page.svelte b/src/routes/(authenticated)/self/+page.svelte index 404113b..669c691 100644 --- a/src/routes/(authenticated)/self/+page.svelte +++ b/src/routes/(authenticated)/self/+page.svelte @@ -12,6 +12,11 @@

Welcome to /self!

Your id is {session.user.id}

Your nickname is {session.user.user_metadata.nickname ?? "not set"}

+
+ Delete a user: + + +
Change your nickname: