mirror of
https://github.com/fosrl/pangolin.git
synced 2026-02-23 05:16:38 +00:00
fix invite flow
This commit is contained in:
@@ -1,11 +1,6 @@
|
||||
import { internal } from "@app/lib/api";
|
||||
import { authCookieHeader } from "@app/lib/api/cookies";
|
||||
import { verifySession } from "@app/lib/auth/verifySession";
|
||||
import { AcceptInviteResponse } from "@server/routers/user";
|
||||
import { AxiosResponse } from "axios";
|
||||
import { redirect } from "next/navigation";
|
||||
import InviteStatusCard from "../../components/InviteStatusCard";
|
||||
import { formatAxiosError } from "@app/lib/api";
|
||||
import { getTranslations } from "next-intl/server";
|
||||
|
||||
export default async function InvitePage(props: {
|
||||
@@ -27,8 +22,8 @@ export default async function InvitePage(props: {
|
||||
if (parts.length !== 2) {
|
||||
return (
|
||||
<>
|
||||
<h1>{t('inviteInvalid')}</h1>
|
||||
<p>{t('inviteInvalidDescription')}</p>
|
||||
<h1>{t("inviteInvalid")}</h1>
|
||||
<p>{t("inviteInvalidDescription")}</p>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -36,58 +31,15 @@ export default async function InvitePage(props: {
|
||||
const inviteId = parts[0];
|
||||
const token = parts[1];
|
||||
|
||||
let error = "";
|
||||
const res = await internal
|
||||
.post<AxiosResponse<AcceptInviteResponse>>(
|
||||
`/invite/accept`,
|
||||
{
|
||||
inviteId,
|
||||
token,
|
||||
},
|
||||
await authCookieHeader()
|
||||
)
|
||||
.catch((e) => {
|
||||
error = formatAxiosError(e);
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
if (res && res.status === 200) {
|
||||
redirect(`/${res.data.data.orgId}`);
|
||||
}
|
||||
|
||||
function cardType() {
|
||||
if (error.includes("Invite is not for this user")) {
|
||||
return "wrong_user";
|
||||
} else if (
|
||||
error.includes("User does not exist. Please create an account first.")
|
||||
) {
|
||||
return "user_does_not_exist";
|
||||
} else if (error.includes("You must be logged in to accept an invite")) {
|
||||
return "not_logged_in";
|
||||
} else {
|
||||
return "rejected";
|
||||
}
|
||||
}
|
||||
|
||||
const type = cardType();
|
||||
|
||||
if (!user && type === "user_does_not_exist") {
|
||||
const redirectUrl = emailParam
|
||||
? `/auth/signup?redirect=/invite?token=${params.token}&email=${encodeURIComponent(emailParam)}`
|
||||
: `/auth/signup?redirect=/invite?token=${params.token}`;
|
||||
redirect(redirectUrl);
|
||||
}
|
||||
|
||||
if (!user && type === "not_logged_in") {
|
||||
const redirectUrl = emailParam
|
||||
? `/auth/login?redirect=/invite?token=${params.token}&email=${encodeURIComponent(emailParam)}`
|
||||
: `/auth/login?redirect=/invite?token=${params.token}`;
|
||||
redirect(redirectUrl);
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<InviteStatusCard type={type} token={tokenParam} email={emailParam} />
|
||||
<InviteStatusCard
|
||||
tokenParam={tokenParam}
|
||||
inviteToken={token}
|
||||
inviteId={inviteId}
|
||||
user={user}
|
||||
email={emailParam}
|
||||
/>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user