mirror of
https://github.com/formbricks/formbricks.git
synced 2026-01-06 09:00:18 -06:00
Co-authored-by: Dhruwang Jariwala <67850763+Dhruwang@users.noreply.github.com> Co-authored-by: Dhruwang <dhruwangjariwala18@gmail.com> Co-authored-by: Piyush Gupta <piyushguptaa2z123@gmail.com>
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 } });
|
|
});
|