Verify team reset links before showing the password form.

Call GET /team/verifyResetCode on load and surface used, expired, or invalid link states with a path to request a new link.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-07-10 06:38:50 -07:00
parent 7563d89bc1
commit a880f06b14
5 changed files with 281 additions and 133 deletions

View File

@ -69,6 +69,7 @@ const isAuthEndpointRequest = (url?: string) => {
url.includes("/team/invitations/verify") || url.includes("/team/invitations/verify") ||
url.includes("/team/invitations/accept") || url.includes("/team/invitations/accept") ||
url.includes("/team/sendResetCode") || url.includes("/team/sendResetCode") ||
url.includes("/team/verifyResetCode") ||
url.includes("/team/resetPassword") url.includes("/team/resetPassword")
); );
}; };

View File

@ -21,6 +21,8 @@ import type {
TeamResetPasswordRequest, TeamResetPasswordRequest,
TeamPasswordResetResponse, TeamPasswordResetResponse,
TeamSendPasswordResetRequest, TeamSendPasswordResetRequest,
TeamVerifyPasswordResetData,
TeamVerifyPasswordResetResponse,
} from "../types/team.types" } from "../types/team.types"
function isRecord(value: unknown): value is Record<string, unknown> { function isRecord(value: unknown): value is Record<string, unknown> {
@ -162,6 +164,23 @@ export const sendTeamPasswordReset = (data: TeamSendPasswordResetRequest) =>
skipErrorToast: true, skipErrorToast: true,
}) })
/** GET /team/verifyResetCode — public; validates email+otp without consuming. */
export const verifyTeamPasswordReset = (email: string, otp: string) =>
http.get<TeamVerifyPasswordResetResponse>("/team/verifyResetCode", {
params: { email, otp },
skipErrorToast: true,
})
export function parseVerifyPasswordReset(
response: Awaited<ReturnType<typeof verifyTeamPasswordReset>>,
): TeamVerifyPasswordResetData | null {
const body = response.data
if (body?.data && typeof body.data === "object" && "valid" in body.data) {
return body.data
}
return null
}
/** POST /team/resetPassword — public; completes reset with email + OTP from the link. */ /** POST /team/resetPassword — public; completes reset with email + OTP from the link. */
export const resetTeamPassword = (data: TeamResetPasswordRequest) => export const resetTeamPassword = (data: TeamResetPasswordRequest) =>
http.post<TeamPasswordResetResponse>("/team/resetPassword", data, { http.post<TeamPasswordResetResponse>("/team/resetPassword", data, {

View File

@ -1,4 +1,5 @@
import axios from "axios" import axios from "axios"
import type { TeamVerifyPasswordResetData } from "../types/team.types"
/** Prefer API `error` detail over generic `message` for auth recovery flows. */ /** Prefer API `error` detail over generic `message` for auth recovery flows. */
export function getTeamAuthErrorMessage(error: unknown, fallback: string): string { export function getTeamAuthErrorMessage(error: unknown, fallback: string): string {
@ -45,3 +46,33 @@ export function mapTeamPasswordResetError(error: unknown): string {
if (detail) return detail if (detail) return detail
return "Something went wrong. Please try again." return "Something went wrong. Please try again."
} }
export function getInvalidResetLinkTitle(
data: TeamVerifyPasswordResetData | null,
): string {
switch (data?.reason) {
case "used":
return "This reset link was already used"
case "expired":
return "This reset link has expired"
case "missing":
return "Reset link is incomplete"
default:
return "This reset link is invalid"
}
}
export function getInvalidResetLinkDescription(
data: TeamVerifyPasswordResetData | null,
): string {
switch (data?.reason) {
case "used":
return "Each reset link can only be used once. Request a new link to set a new password."
case "expired":
return "Reset links are valid for 5 minutes. Request a new one to continue."
case "missing":
return "Open the full link from your email, or request a new reset link."
default:
return "The link may be expired, invalid, or already used. Request a new one from Forgot password."
}
}

View File

@ -1,13 +1,21 @@
import { useMemo, useState } from "react" import { useCallback, useEffect, useMemo, useState } from "react"
import { Link, useNavigate, useSearchParams } from "react-router-dom" import { Link, useNavigate, useSearchParams } from "react-router-dom"
import { ArrowLeft, Eye, EyeOff } from "lucide-react" import { AlertCircle, ArrowLeft, Eye, EyeOff } from "lucide-react"
import { toast } from "sonner" import { toast } from "sonner"
import { resetTeamPassword } from "../../api/team.api" import {
parseVerifyPasswordReset,
resetTeamPassword,
verifyTeamPasswordReset,
} from "../../api/team.api"
import { BrandLogo } from "../../components/brand/BrandLogo" import { BrandLogo } from "../../components/brand/BrandLogo"
import { Button } from "../../components/ui/button" import { Button } from "../../components/ui/button"
import { Input } from "../../components/ui/input" import { Input } from "../../components/ui/input"
import { SpinnerIcon } from "../../components/ui/spinner-icon" import { SpinnerIcon } from "../../components/ui/spinner-icon"
import { mapTeamPasswordResetError } from "../../lib/teamPasswordReset" import {
getInvalidResetLinkDescription,
getInvalidResetLinkTitle,
mapTeamPasswordResetError,
} from "../../lib/teamPasswordReset"
export function ResetPasswordPage() { export function ResetPasswordPage() {
const navigate = useNavigate() const navigate = useNavigate()
@ -15,6 +23,13 @@ export function ResetPasswordPage() {
const emailFromQuery = searchParams.get("email")?.trim() ?? "" const emailFromQuery = searchParams.get("email")?.trim() ?? ""
const otpFromQuery = searchParams.get("otp")?.trim() ?? "" const otpFromQuery = searchParams.get("otp")?.trim() ?? ""
const hasLinkParams = Boolean(emailFromQuery && otpFromQuery)
const [verifyState, setVerifyState] = useState<"loading" | "invalid" | "ready">(
hasLinkParams ? "loading" : "ready",
)
const [invalidTitle, setInvalidTitle] = useState("")
const [invalidDescription, setInvalidDescription] = useState("")
const [email, setEmail] = useState(emailFromQuery) const [email, setEmail] = useState(emailFromQuery)
const [otp, setOtp] = useState(otpFromQuery) const [otp, setOtp] = useState(otpFromQuery)
@ -24,10 +39,41 @@ export function ResetPasswordPage() {
const [submitting, setSubmitting] = useState(false) const [submitting, setSubmitting] = useState(false)
const [error, setError] = useState<string | null>(null) const [error, setError] = useState<string | null>(null)
const hasLinkParams = Boolean(emailFromQuery && otpFromQuery)
const emailLocked = Boolean(emailFromQuery) const emailLocked = Boolean(emailFromQuery)
const otpLocked = Boolean(otpFromQuery) const otpLocked = Boolean(otpFromQuery)
const loadVerification = useCallback(async () => {
if (!hasLinkParams) {
setVerifyState("ready")
return
}
setVerifyState("loading")
try {
const res = await verifyTeamPasswordReset(emailFromQuery, otpFromQuery)
const data = parseVerifyPasswordReset(res)
if (!data || data.valid !== true) {
setInvalidTitle(getInvalidResetLinkTitle(data))
setInvalidDescription(getInvalidResetLinkDescription(data))
setVerifyState("invalid")
return
}
setVerifyState("ready")
} catch {
setInvalidTitle("This reset link is invalid")
setInvalidDescription(
"The link may be expired, invalid, or already used. Request a new one from Forgot password.",
)
setVerifyState("invalid")
}
}, [emailFromQuery, hasLinkParams, otpFromQuery])
useEffect(() => {
void loadVerification()
}, [loadVerification])
const canSubmit = useMemo(() => { const canSubmit = useMemo(() => {
return email.trim().length > 0 && otp.trim().length > 0 && password.length >= 8 return email.trim().length > 0 && otp.trim().length > 0 && password.length >= 8
}, [email, otp, password]) }, [email, otp, password])
@ -102,148 +148,181 @@ export function ResetPasswordPage() {
Account Recovery Account Recovery
</p> </p>
<h1 className="mb-2 text-3xl font-bold tracking-tight text-grayScale-600"> <h1 className="mb-2 text-3xl font-bold tracking-tight text-grayScale-600">
Reset password {verifyState === "invalid" ? invalidTitle : "Reset password"}
</h1> </h1>
<p className="text-sm leading-relaxed text-grayScale-400"> <p className="text-sm leading-relaxed text-grayScale-400">
{hasLinkParams {verifyState === "loading"
? "Set a new password for your team account. This link expires in 5 minutes." ? "Checking your reset link…"
: "Enter the email and code from your reset email, then choose a new password."} : verifyState === "invalid"
? invalidDescription
: hasLinkParams
? "Set a new password for your team account. This link expires in 5 minutes."
: "Enter the email and code from your reset email, then choose a new password."}
</p> </p>
</div> </div>
{!hasLinkParams ? ( {verifyState === "loading" ? (
<div className="mb-5 rounded-lg border border-amber-200 bg-amber-50 px-3 py-2 text-sm text-amber-800"> <div className="flex flex-col items-center gap-3 py-16">
Missing reset link details. You can enter them manually, or{" "} <SpinnerIcon className="h-8 w-8" />
<Link to="/forgot-password" className="font-semibold underline"> <p className="text-sm text-grayScale-400">Verifying reset link…</p>
request a new link
</Link>
.
</div> </div>
) : null} ) : null}
<form onSubmit={handleSubmit} className="space-y-4"> {verifyState === "invalid" ? (
<div> <div className="space-y-4 rounded-xl border border-red-200 bg-red-50 px-4 py-4">
<label <div className="flex gap-3">
htmlFor="reset-email" <AlertCircle className="mt-0.5 h-5 w-5 shrink-0 text-red-600" />
className="mb-1.5 block text-sm font-medium text-grayScale-600" <div className="space-y-2 text-sm text-red-800">
> <p className="font-semibold">{invalidTitle}</p>
Email address <p>{invalidDescription}</p>
</label> </div>
<Input
id="reset-email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
readOnly={emailLocked}
required
autoComplete="email"
disabled={submitting}
className="h-11 rounded-xl"
/>
</div>
<div>
<label
htmlFor="reset-otp"
className="mb-1.5 block text-sm font-medium text-grayScale-600"
>
Reset code
</label>
<Input
id="reset-otp"
type={otpLocked ? "password" : "text"}
value={otp}
onChange={(e) => setOtp(e.target.value)}
readOnly={otpLocked}
required
autoComplete="one-time-code"
disabled={submitting}
className="h-11 rounded-xl"
/>
</div>
<div>
<label
htmlFor="reset-password"
className="mb-1.5 block text-sm font-medium text-grayScale-600"
>
New password
</label>
<div className="relative">
<Input
id="reset-password"
type={showPassword ? "text" : "password"}
value={password}
onChange={(e) => setPassword(e.target.value)}
required
minLength={8}
autoComplete="new-password"
disabled={submitting}
className="h-11 rounded-xl pr-11"
/>
<button
type="button"
className="absolute right-3 top-1/2 -translate-y-1/2 text-grayScale-400 hover:text-grayScale-600"
onClick={() => setShowPassword((v) => !v)}
aria-label={showPassword ? "Hide password" : "Show password"}
>
{showPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
</button>
</div> </div>
<p className="mt-1 text-xs text-grayScale-400">At least 8 characters</p> <Link
</div> to="/forgot-password"
className="inline-block text-sm font-semibold text-red-800 underline"
<div>
<label
htmlFor="reset-confirm"
className="mb-1.5 block text-sm font-medium text-grayScale-600"
> >
Confirm password Request a new reset link
</label> </Link>
<Input
id="reset-confirm"
type={showPassword ? "text" : "password"}
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
required
minLength={8}
autoComplete="new-password"
disabled={submitting}
className="h-11 rounded-xl"
/>
</div> </div>
) : null}
{error ? ( {verifyState === "ready" ? (
<div className="space-y-2 rounded-lg border border-red-200 bg-red-50 px-3 py-2 text-sm text-red-700"> <>
<p>{error}</p> {!hasLinkParams ? (
{(error.toLowerCase().includes("expired") || <div className="mb-5 rounded-lg border border-amber-200 bg-amber-50 px-3 py-2 text-sm text-amber-800">
error.toLowerCase().includes("already used") || Missing reset link details. You can enter them manually, or{" "}
error.toLowerCase().includes("invalid reset")) && ( <Link to="/forgot-password" className="font-semibold underline">
<Link request a new link
to="/forgot-password"
className="inline-block font-semibold text-red-800 underline"
>
Request a new reset link
</Link> </Link>
)} .
</div> </div>
) : null} ) : null}
<Button <form onSubmit={handleSubmit} className="space-y-4">
type="submit" <div>
disabled={submitting || !canSubmit} <label
className="mt-2 h-11 w-full rounded-xl text-sm font-semibold tracking-wide" htmlFor="reset-email"
> className="mb-1.5 block text-sm font-medium text-grayScale-600"
{submitting ? ( >
<span className="inline-flex items-center gap-2"> Email address
<SpinnerIcon className="h-4 w-4" /> </label>
Saving… <Input
</span> id="reset-email"
) : ( type="email"
"Reset password" value={email}
)} onChange={(e) => setEmail(e.target.value)}
</Button> readOnly={emailLocked}
</form> required
autoComplete="email"
disabled={submitting}
className="h-11 rounded-xl"
/>
</div>
<div>
<label
htmlFor="reset-otp"
className="mb-1.5 block text-sm font-medium text-grayScale-600"
>
Reset code
</label>
<Input
id="reset-otp"
type={otpLocked ? "password" : "text"}
value={otp}
onChange={(e) => setOtp(e.target.value)}
readOnly={otpLocked}
required
autoComplete="one-time-code"
disabled={submitting}
className="h-11 rounded-xl"
/>
</div>
<div>
<label
htmlFor="reset-password"
className="mb-1.5 block text-sm font-medium text-grayScale-600"
>
New password
</label>
<div className="relative">
<Input
id="reset-password"
type={showPassword ? "text" : "password"}
value={password}
onChange={(e) => setPassword(e.target.value)}
required
minLength={8}
autoComplete="new-password"
disabled={submitting}
className="h-11 rounded-xl pr-11"
/>
<button
type="button"
className="absolute right-3 top-1/2 -translate-y-1/2 text-grayScale-400 hover:text-grayScale-600"
onClick={() => setShowPassword((v) => !v)}
aria-label={showPassword ? "Hide password" : "Show password"}
>
{showPassword ? <EyeOff className="h-4 w-4" /> : <Eye className="h-4 w-4" />}
</button>
</div>
<p className="mt-1 text-xs text-grayScale-400">At least 8 characters</p>
</div>
<div>
<label
htmlFor="reset-confirm"
className="mb-1.5 block text-sm font-medium text-grayScale-600"
>
Confirm password
</label>
<Input
id="reset-confirm"
type={showPassword ? "text" : "password"}
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
required
minLength={8}
autoComplete="new-password"
disabled={submitting}
className="h-11 rounded-xl"
/>
</div>
{error ? (
<div className="space-y-2 rounded-lg border border-red-200 bg-red-50 px-3 py-2 text-sm text-red-700">
<p>{error}</p>
{(error.toLowerCase().includes("expired") ||
error.toLowerCase().includes("already used") ||
error.toLowerCase().includes("invalid reset")) && (
<Link
to="/forgot-password"
className="inline-block font-semibold text-red-800 underline"
>
Request a new reset link
</Link>
)}
</div>
) : null}
<Button
type="submit"
disabled={submitting || !canSubmit}
className="mt-2 h-11 w-full rounded-xl text-sm font-semibold tracking-wide"
>
{submitting ? (
<span className="inline-flex items-center gap-2">
<SpinnerIcon className="h-4 w-4" />
Saving…
</span>
) : (
"Reset password"
)}
</Button>
</form>
</>
) : null}
<p className="mt-10 text-center text-xs text-grayScale-400"> <p className="mt-10 text-center text-xs text-grayScale-400">
© {new Date().getFullYear()} Yimaru Academy · All rights reserved © {new Date().getFullYear()} Yimaru Academy · All rights reserved

View File

@ -157,3 +157,21 @@ export interface TeamPasswordResetResponse {
success?: boolean success?: boolean
data?: unknown data?: unknown
} }
/** GET /team/verifyResetCode — public; checks link without consuming OTP */
export type TeamVerifyPasswordResetReason =
| "missing"
| "invalid"
| "used"
| "expired"
export interface TeamVerifyPasswordResetData {
valid: boolean
reason?: TeamVerifyPasswordResetReason | string
}
export interface TeamVerifyPasswordResetResponse {
message?: string
success?: boolean
data?: TeamVerifyPasswordResetData
}