Created
September 2, 2023 13:21
-
-
Save ChangoMan/70b4da9f2286d95f6b58bf206f8d446b to your computer and use it in GitHub Desktop.
Next.js Server Actions that Updates Supabase Database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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