mirror of
https://github.com/formbricks/formbricks.git
synced 2025-12-30 02:10:12 -06:00
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { getServerSession } from "next-auth";
|
|
import { DEFAULT_SERVER_ERROR_MESSAGE, createSafeActionClient } from "next-safe-action";
|
|
import { authOptions } from "@formbricks/lib/authOptions";
|
|
import { getUser } from "@formbricks/lib/user/service";
|
|
import {
|
|
AuthenticationError,
|
|
AuthorizationError,
|
|
InvalidInputError,
|
|
ResourceNotFoundError,
|
|
UnknownError,
|
|
} from "@formbricks/types/errors";
|
|
|
|
export const actionClient = createSafeActionClient({
|
|
handleServerError(e) {
|
|
if (
|
|
e instanceof ResourceNotFoundError ||
|
|
e instanceof AuthorizationError ||
|
|
e instanceof InvalidInputError ||
|
|
e instanceof UnknownError
|
|
) {
|
|
return e.message;
|
|
}
|
|
|
|
console.error("SERVER ERROR: ", e);
|
|
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 } });
|
|
});
|