Files
portabase/src/safe-actions.ts
T

37 lines
931 B
TypeScript
Raw Normal View History

2024-11-03 21:12:40 +01:00
import {createSafeActionClient} from "next-safe-action";
import {currentUser} from "@/auth/current-user";
2024-11-29 19:25:44 +01:00
import {baseAuth} from "@/auth/auth";
import {Organization} from "@prisma/client";
import {currentOrganization} from "@/auth/current-organization";
2024-11-03 21:12:40 +01:00
export class ActionError extends Error {
constructor(message: string) {
super(message);
this.name = "ActionError";
}
}
const handleReturnedServerError = (error: Error) => {
if (error instanceof ActionError) {
return error.message
} else {
return "An unexpected error occurred."
}
}
2024-11-29 19:25:44 +01:00
2024-11-03 21:12:40 +01:00
export const action = createSafeActionClient(
{
handleReturnedServerError: handleReturnedServerError
});
2024-11-29 19:25:44 +01:00
export const userAction = action.use(async ({next, ctx}) => {
2024-11-03 21:12:40 +01:00
const user = await currentUser();
2024-11-29 19:25:44 +01:00
2024-11-03 21:12:40 +01:00
if (!user) {
throw new ActionError("You must be logged in");
}
2024-11-29 19:25:44 +01:00
return next({ctx: {user}});
2024-11-03 21:12:40 +01:00
});