feat: persist portal invitation locales
This commit is contained in:
@@ -1,10 +1,20 @@
|
||||
"use server";
|
||||
|
||||
import { cookies } from "next/headers";
|
||||
import { redirect } from "next/navigation";
|
||||
import {
|
||||
acceptPortalInvitation,
|
||||
getPortalInvitationPreview,
|
||||
PortalInvitationError,
|
||||
} from "@/server/auth/invitations";
|
||||
import { buildLocaleCookie } from "@/server/i18n/locale";
|
||||
|
||||
function inviteErrorCode(error: unknown): string {
|
||||
if (!(error instanceof PortalInvitationError)) return "auth.messages.portalInviteFailed";
|
||||
if (error.code === "INVITATION_EXPIRED") return "auth.invite.expired";
|
||||
if (error.code === "INVITATION_NOT_PENDING") return "auth.invite.accepted";
|
||||
return "auth.messages.portalInviteFailed";
|
||||
}
|
||||
|
||||
export async function acceptInvitation(formData: FormData) {
|
||||
const token = String(formData.get("token") ?? "");
|
||||
@@ -14,14 +24,10 @@ export async function acceptInvitation(formData: FormData) {
|
||||
try {
|
||||
await acceptPortalInvitation({ token, displayName, password });
|
||||
} catch (error) {
|
||||
const message =
|
||||
error instanceof PortalInvitationError
|
||||
? error.message
|
||||
: "Portal hesabı oluşturulamadı.";
|
||||
redirect(`/invite/${encodeURIComponent(token)}?error=true&message=${encodeURIComponent(message)}`);
|
||||
redirect(`/invite/${encodeURIComponent(token)}?error=true&code=${inviteErrorCode(error)}`);
|
||||
}
|
||||
|
||||
redirect(
|
||||
`/login?message=${encodeURIComponent("Portal hesabın oluşturuldu. Şimdi giriş yapabilirsin.")}`,
|
||||
);
|
||||
const locale = getPortalInvitationPreview(token)?.locale ?? "tr";
|
||||
(await cookies()).set(buildLocaleCookie(locale));
|
||||
redirect("/login?code=auth.invite.success");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user