| 35 | ) => Promise<T>; |
| 36 | |
| 37 | export function validatedActionWithUser<S extends z.ZodType<any, any>, T>( |
| 38 | schema: S, |
| 39 | action: ValidatedActionWithUserFunction<S, T> |
| 40 | ) { |
| 41 | return async (prevState: ActionState, formData: FormData) => { |
| 42 | const user = await getUser(); |
| 43 | if (!user) { |
| 44 | throw new Error('User is not authenticated'); |
| 45 | } |
| 46 | |
| 47 | const result = schema.safeParse(Object.fromEntries(formData)); |
| 48 | if (!result.success) { |
| 49 | return { error: result.error.errors[0].message }; |
| 50 | } |
| 51 | |
| 52 | return action(result.data, formData, user); |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | type ActionWithTeamFunction<T> = ( |
| 57 | formData: FormData, |