Enterprise agent platform for building, deploying, and operating AI teammates.
"use client"
import React, { useEffect, useRef, useState } from "react"
import Link from "next/link"
import { ArrowRight, Loader2, Eye, EyeOff, Lock, Workflow, Database, UserCheck } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { getApiUrl } from "@/lib/utils"
import { getBrandingFromEnv } from "@/lib/branding"
import { useI18n } from "@/contexts/i18n-context"
import { apiRequest } from "@/lib/api-wrapper"
import { AuthPageShell } from "@/components/auth/auth-page-shell"
import { AuthFormCard } from "@/components/auth/auth-form-card"
import { useSetupStatus } from "@/hooks/use-setup-status"
export function ResetPasswordPage() {
const branding = getBrandingFromEnv()
const { t } = useI18n()
const [token, setToken] = useState("")
const tokenRead = useRef(false)
const [password, setPassword] = useState("")
const [confirmPassword, setConfirmPassword] = useState("")
const [showPassword, setShowPassword] = useState(false)
const [showConfirmPassword, setShowConfirmPassword] = useState(false)
const [isLoading, setIsLoading] = useState(false)
const [error, setError] = useState("")
const [success, setSuccess] = useState("")
const { passwordMinLength } = useSetupStatus()
useEffect(() => {
if (tokenRead.current) return
tokenRead.current = true
const fragment = window.location.hash.replace(/^#/, "")
const fragmentParams = new URLSearchParams(fragment)
const queryParams = new URLSearchParams(window.location.search)
// Accept outstanding pre-upgrade emails for their remaining lifetime.
// New emails use fragments; strip either representation immediately.
const resetToken = fragmentParams.get("token") || queryParams.get("token") || ""
setToken(resetToken)
if (!resetToken) setError(t("resetPassword.alerts.invalid_token"))
const hadQueryToken = queryParams.has("token")
queryParams.delete("token")
fragmentParams.delete("token")
// Remove the one-time token from the address bar and browser history after
// reading it. The token remains only in component state for the POST.
if (window.location.hash || hadQueryToken) {
const query = queryParams.toString()
const hash = fragmentParams.toString()
window.history.replaceState(
null,
"",
`${window.location.pathname}${query ? `?${query}` : ""}${hash ? `#${hash}` : ""}`
)
}
}, [t])
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
setError("")
setSuccess("")
if (!token) {
setError(t("resetPassword.alerts.invalid_token"))
return
}
if (password.length < passwordMinLength) {
setError(
t("resetPassword.alerts.password_too_short", {
min: passwordMinLength,
})
)
return
}
if (password !== confirmPassword) {
setError(t("resetPassword.alerts.password_mismatch"))
return
}
setIsLoading(true)
try {
const response = await apiRequest(`${getApiUrl()}/api/auth/reset-password`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
token,
new_password: password,
}),
})
const data = await response.json()
if (response.ok && data.success) {
setSuccess(data.message || t("resetPassword.alerts.success"))
setTimeout(() => {
window.location.href = "/login"
}, 2000)
} else {
setError(data.message || t("resetPassword.alerts.failed"))
}
} catch (submitError) {
console.error("Reset password failed:", submitError)
setError(t("resetPassword.alerts.failed_retry"))
} finally {
setIsLoading(false)
}
}
const features = [
{
icon: Workflow,
title: t("login.features.version_control.title"),
description: t("login.features.version_control.description"),
},
{
icon: Database,
title: t("login.features.team.title"),
description: t("login.features.team.description"),
},
{
icon: UserCheck,
title: t("login.features.automation.title"),
description: t("login.features.automation.description"),
}
]
return (
<AuthPageShell
appName={branding.appName}
logoPath={branding.logoPath}
logoAlt={branding.logoAlt}
heroTitle={process.env.NEXT_PUBLIC_APP_TAGLINE ? branding.tagline.replace(". ", ".\n") : t("branding.tagline")}
leftDescription={t("branding.hero_description")}
mobileSubtitle={t("resetPassword.mobile_title")}
features={features}
>
<AuthFormCard
appName={branding.appName}
logoPath={branding.logoPath}
logoAlt={branding.logoAlt}
modeLabel={t("resetPassword.mode_label")}
showSocialLogin={false}
title={t("resetPassword.title", { appName: branding.appName })}
description={t("resetPassword.description")}
footer={
<>
{t("resetPassword.back_to_login")}{" "}
<Link href="/login" className="font-semibold text-[#3155F6] hover:text-[#2447D8]">
{t("nav.login")}
</Link>
</>
}
>
<form onSubmit={handleSubmit} className="space-y-5">
{error ? (
<div className="rounded-[16px] border border-[#FFD5D9] bg-[#FFF5F6] px-4 py-3">
<p className="text-sm text-[#C53030]">{error}</p>
</div>
) : null}
{success ? (
<div className="rounded-[16px] border border-[#C8F1DA] bg-[#F1FFF7] px-4 py-3">
<p className="text-sm text-[#137A47]">{success}</p>
</div>
) : null}
<div className="space-y-2">
<label className="block text-sm font-semibold text-[#4A5365]">
{t("resetPassword.form.password")}
</label>
<div className="relative">
<Lock className="pointer-events-none absolute left-4 top-1/2 h-4 w-4 -translate-y-1/2 text-[#A0A9B8]" />
<Input
type={showPassword ? "text" : "password"}
minLength={passwordMinLength}
value={password}
onChange={(event) => {
setPassword(event.target.value)
if (error) setError("")
if (success) setSuccess("")
}}
placeholder={t("resetPassword.form.password_placeholder")}
className="h-12 rounded-[14px] border-[#E2E8F3] bg-white pl-11 pr-11 text-[#171A2F] placeholder:text-[#A0A9B8] shadow-[0_1px_2px_rgba(16,24,40,0.04)] focus-visible:border-[#5B7CFF] focus-visible:ring-[#5B7CFF]/20"
required
/>
<button
type="button"
onClick={() => setShowPassword(!showPassword)}
className="absolute right-4 top-1/2 -translate-y-1/2 text-[#A0A9B8] transition-colors hover:text-[#4A5365]"
aria-label={showPassword ? "Hide password" : "Show password"}
>
{showPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
</button>
</div>
</div>
<div className="space-y-2">
<label className="block text-sm font-semibold text-[#4A5365]">
{t("resetPassword.form.confirm_password")}
</label>
<div className="relative">
<Lock className="pointer-events-none absolute left-4 top-1/2 h-4 w-4 -translate-y-1/2 text-[#A0A9B8]" />
<Input
type={showConfirmPassword ? "text" : "password"}
value={confirmPassword}
onChange={(event) => {
setConfirmPassword(event.target.value)
if (error) setError("")
if (success) setSuccess("")
}}
placeholder={t("resetPassword.form.confirm_password_placeholder")}
className="h-12 rounded-[14px] border-[#E2E8F3] bg-white pl-11 pr-11 text-[#171A2F] placeholder:text-[#A0A9B8] shadow-[0_1px_2px_rgba(16,24,40,0.04)] focus-visible:border-[#5B7CFF] focus-visible:ring-[#5B7CFF]/20"
required
/>
<button
type="button"
onClick={() => setShowConfirmPassword(!showConfirmPassword)}
className="absolute right-4 top-1/2 -translate-y-1/2 text-[#A0A9B8] transition-colors hover:text-[#4A5365]"
aria-label={showConfirmPassword ? "Hide password" : "Show password"}
>
{showConfirmPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
</button>
</div>
</div>
<Button
type="submit"
disabled={!token || !password || !confirmPassword || isLoading}
className="h-12 w-full rounded-[14px] bg-[linear-gradient(180deg,#4B6BFF_0%,#2F54EB_100%)] text-base font-semibold text-white shadow-[0_14px_30px_rgba(47,84,235,0.32)] transition-all hover:translate-y-[-1px] hover:opacity-95 disabled:translate-y-0 disabled:opacity-60"
>
{isLoading ? (
<span className="flex items-center gap-2">
<Loader2 className="h-4 w-4 animate-spin" />
{t("resetPassword.form.submitting")}
</span>
) : (
<span className="flex items-center gap-2">
{t("resetPassword.form.submit")}
<ArrowRight className="h-4 w-4" />
</span>
)}
</Button>
</form>
</AuthFormCard>
</AuthPageShell>
)
}