Skip to content

Commit

Permalink
feat: delete users by uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
j4w8n-malynium committed Jul 4, 2024
1 parent a83275d commit 07060e1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/routes/(authenticated)/self/+page.server.ts
Original file line number Diff line number Diff line change
@@ -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 } }) => {
/**
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/routes/(authenticated)/self/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
<h2>Welcome to /self!</h2>
<h4>Your id is {session.user.id}</h4>
<h4>Your nickname is {session.user.user_metadata.nickname ?? "not set"}</h4>
<form method="POST" action="?/delete_user">
Delete a user:
<input name="user" type="text">
<button style="margin-top: 12px;">Delete</button>
</form>
<form method="POST" action="?/update">
Change your nickname:
<input name="nickname" type="text" value={ form?.data?.nickname ?? ""}>
Expand Down

0 comments on commit 07060e1

Please sign in to comment.