75 lines
2.1 KiB
TypeScript
75 lines
2.1 KiB
TypeScript
"use client"
|
|
|
|
import { useEffect, useRef, useState } from "react"
|
|
import { useSearchParams } from "next/navigation"
|
|
import { Button } from "@modules/common/components/ui"
|
|
import { confirmEmailVerification } from "@lib/data/customer"
|
|
import LocalizedClientLink from "@modules/common/components/localized-client-link"
|
|
|
|
type VerificationState = "verifying" | "success" | "error"
|
|
|
|
const VerifyAccount = () => {
|
|
const searchParams = useSearchParams()
|
|
const token = searchParams.get("token")
|
|
const [state, setState] = useState<VerificationState>("verifying")
|
|
// Guard against the effect running twice in React Strict Mode, which would
|
|
// consume the single-use token before the customer sees the result.
|
|
const confirmed = useRef(false)
|
|
|
|
useEffect(() => {
|
|
if (confirmed.current) {
|
|
return
|
|
}
|
|
confirmed.current = true
|
|
|
|
if (!token) {
|
|
setState("error")
|
|
return
|
|
}
|
|
|
|
confirmEmailVerification(token).then(({ success }) =>
|
|
setState(success ? "success" : "error")
|
|
)
|
|
}, [token])
|
|
|
|
return (
|
|
<div
|
|
className="max-w-sm w-full flex flex-col items-center text-center gap-y-4"
|
|
data-testid="verify-account-page"
|
|
>
|
|
<h1 className="text-lg font-medium uppercase">Email verification</h1>
|
|
|
|
{state === "verifying" && (
|
|
<p className="text-base text-foreground">
|
|
Potvrđujemo vašu email adresu…
|
|
</p>
|
|
)}
|
|
|
|
{state === "success" && (
|
|
<>
|
|
<p className="text-base text-foreground">
|
|
Email adresa je potvrđena. Sada se možete prijaviti na svoj račun.
|
|
</p>
|
|
<LocalizedClientLink href="/account">
|
|
<Button variant="primary">Na prijavu</Button>
|
|
</LocalizedClientLink>
|
|
</>
|
|
)}
|
|
|
|
{state === "error" && (
|
|
<>
|
|
<p className="text-base text-foreground">
|
|
Link za potvrdu nije ispravan ili je istekao. Prijavite se da dobijete
|
|
novi email za potvrdu.
|
|
</p>
|
|
<LocalizedClientLink href="/account">
|
|
<Button variant="secondary">Na prijavu</Button>
|
|
</LocalizedClientLink>
|
|
</>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default VerifyAccount
|