33 lines
1.3 KiB
TypeScript
33 lines
1.3 KiB
TypeScript
"use client";
|
|
|
|
import Link from "next/link";
|
|
import { useState } from "react";
|
|
import { Card, Heading, Notice } from "@crosscode/ui";
|
|
import { PublicShell } from "@/components/PublicShell";
|
|
import { useBranding } from "@/components/BrandingProvider";
|
|
import { call, selfSignIn } from "@/lib/api";
|
|
import { useExternalEffect } from "@/lib/hooks";
|
|
|
|
export function ConfirmEmail({ token }: { token: string }) {
|
|
const branding = useBranding();
|
|
const [state, setState] = useState<{ ok: boolean; text: string } | null>(null);
|
|
useExternalEffect(() => {
|
|
let alive = true;
|
|
call("POST", "/me/email/confirm", { token }).then((r) => {
|
|
if (!alive) return;
|
|
if (r.status === 401) return window.location.assign(selfSignIn(`/account/confirm-email?token=${encodeURIComponent(token)}`));
|
|
setState(r.ok ? { ok: true, text: "Your new email address is confirmed. Sign in with it from now on." } : { ok: false, text: r.error.message });
|
|
});
|
|
return () => { alive = false; };
|
|
}, [token]);
|
|
return (
|
|
<PublicShell>
|
|
<Card>
|
|
<Heading>Your email address</Heading>
|
|
{state ? <Notice tone={state.ok ? "success" : "error"}>{state.text}</Notice> : <p className="text-gray-600">Confirming…</p>}
|
|
<Link href="/account" className="text-brand underline">Back to your account</Link>
|
|
</Card>
|
|
</PublicShell>
|
|
);
|
|
}
|