File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ // src/actions/update-profile.ts
2+ "use server" ;
3+
4+ import { headers } from "next/headers" ;
5+ import { revalidatePath } from "next/cache" ;
6+
7+ import { auth } from "@/lib/auth" ;
8+ import { db } from "@/db" ;
9+ import { users } from "@/db/schema" ;
10+ import { eq } from "drizzle-orm" ;
11+
12+ export async function updateProfile ( formData : FormData ) {
13+ const session = await auth . api . getSession ( {
14+ headers : await headers ( ) ,
15+ } ) ;
16+
17+ if ( ! session ?. user ) {
18+ throw new Error ( "Not authenticated" ) ;
19+ }
20+
21+ const userId = session . user . id as any ;
22+
23+ const name = formData . get ( "name" ) ;
24+ const image = formData . get ( "image" ) ;
25+
26+ if ( typeof name !== "string" || name . trim ( ) . length < 2 ) {
27+ throw new Error ( "Name must be at least 2 characters" ) ;
28+ }
29+
30+ await db
31+ . update ( users )
32+ . set ( {
33+ name : name . trim ( ) ,
34+ image :
35+ typeof image === "string" && image . trim ( ) . length > 0
36+ ? image . trim ( )
37+ : null ,
38+ } )
39+ . where ( eq ( users . id , userId ) ) ;
40+
41+ revalidatePath ( "/profile" ) ;
42+ }
You can’t perform that action at this time.
0 commit comments