MCPcopy
hub / github.com/nextjs/saas-starter / validatedActionWithUser

Function validatedActionWithUser

lib/auth/middleware.ts:37–54  ·  view source on GitHub ↗
(
  schema: S,
  action: ValidatedActionWithUserFunction<S, T>
)

Source from the content-addressed store, hash-verified

35) => Promise<T>;
36
37export 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
56type ActionWithTeamFunction<T> = (
57 formData: FormData,

Callers 1

actions.tsFile · 0.90

Calls 1

getUserFunction · 0.90

Tested by

no test coverage detected