Files
formbricks-formbricks/packages/lib/actionClient/index.ts
Anshuman Pandey c1492e3429 fix: session token size (#2941)
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2024-08-01 12:27:54 +00:00

34 lines
1023 B
TypeScript

import { getServerSession } from "next-auth";
import { DEFAULT_SERVER_ERROR_MESSAGE, createSafeActionClient } from "next-safe-action";
import { AuthenticationError, AuthorizationError, ResourceNotFoundError } from "@formbricks/types/errors";
import { authOptions } from "../authOptions";
import { getUser } from "../user/service";
export const actionClient = createSafeActionClient({
handleReturnedServerError(e) {
if (e instanceof ResourceNotFoundError) {
return e.message;
} else if (e instanceof AuthorizationError) {
return e.message;
}
return DEFAULT_SERVER_ERROR_MESSAGE;
},
});
export const authenticatedActionClient = actionClient.use(async ({ next }) => {
const session = await getServerSession(authOptions);
if (!session?.user) {
throw new AuthenticationError("Not authenticated");
}
const userId = session.user.id;
const user = await getUser(userId);
if (!user) {
throw new AuthorizationError("User not found");
}
return next({ ctx: { user } });
});