Skip to content

Instantly share code, notes, and snippets.

@ChangoMan
Created September 2, 2023 13:21
Show Gist options
  • Save ChangoMan/70b4da9f2286d95f6b58bf206f8d446b to your computer and use it in GitHub Desktop.
Save ChangoMan/70b4da9f2286d95f6b58bf206f8d446b to your computer and use it in GitHub Desktop.
Next.js Server Actions that Updates Supabase Database
'use server'
import { createServerActionClient } from '@supabase/auth-helpers-nextjs'
import { revalidatePath } from 'next/cache'
import { cookies } from 'next/headers'
export const updateAccount = async (formData: FormData, userId: string) => {
try {
const full_name = formData.get('full-name')
const username = formData.get('username')
const website = formData.get('website')
const supabaseAction = createServerActionClient({ cookies })
await supabaseAction.from('profiles').upsert({
id: userId as string,
full_name,
username,
website,
updated_at: new Date().toISOString(),
})
revalidatePath('/account')
return {
message: 'Success!',
}
} catch (error) {
return {
message: 'There was an error',
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment