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:
parent
7563d89bc1
commit
a880f06b14
|
|
@ -69,6 +69,7 @@ const isAuthEndpointRequest = (url?: string) => {
|
|||
url.includes("/team/invitations/verify") ||
|
||||
url.includes("/team/invitations/accept") ||
|
||||
url.includes("/team/sendResetCode") ||
|
||||
url.includes("/team/verifyResetCode") ||
|
||||
url.includes("/team/resetPassword")
|
||||
);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ import type {
|
|||
TeamResetPasswordRequest,
|
||||
TeamPasswordResetResponse,
|
||||
TeamSendPasswordResetRequest,
|
||||
TeamVerifyPasswordResetData,
|
||||
TeamVerifyPasswordResetResponse,
|
||||
} from "../types/team.types"
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
|
|
@ -162,6 +164,23 @@ export const sendTeamPasswordReset = (data: TeamSendPasswordResetRequest) =>
|
|||
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. */
|
||||
export const resetTeamPassword = (data: TeamResetPasswordRequest) =>
|
||||
http.post<TeamPasswordResetResponse>("/team/resetPassword", data, {
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import axios from "axios"
|
||||
import type { TeamVerifyPasswordResetData } from "../types/team.types"
|
||||
|
||||
/** Prefer API `error` detail over generic `message` for auth recovery flows. */
|
||||
export function getTeamAuthErrorMessage(error: unknown, fallback: string): string {
|
||||
|
|
@ -45,3 +46,33 @@ export function mapTeamPasswordResetError(error: unknown): string {
|
|||
if (detail) return detail
|
||||
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."
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 { ArrowLeft, Eye, EyeOff } from "lucide-react"
|
||||
import { AlertCircle, ArrowLeft, Eye, EyeOff } from "lucide-react"
|
||||
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 { Button } from "../../components/ui/button"
|
||||
import { Input } from "../../components/ui/input"
|
||||
import { SpinnerIcon } from "../../components/ui/spinner-icon"
|
||||
import { mapTeamPasswordResetError } from "../../lib/teamPasswordReset"
|
||||
import {
|
||||
getInvalidResetLinkDescription,
|
||||
getInvalidResetLinkTitle,
|
||||
mapTeamPasswordResetError,
|
||||
} from "../../lib/teamPasswordReset"
|
||||
|
||||
export function ResetPasswordPage() {
|
||||
const navigate = useNavigate()
|
||||
|
|
@ -15,6 +23,13 @@ export function ResetPasswordPage() {
|
|||
|
||||
const emailFromQuery = searchParams.get("email")?.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 [otp, setOtp] = useState(otpFromQuery)
|
||||
|
|
@ -24,10 +39,41 @@ export function ResetPasswordPage() {
|
|||
const [submitting, setSubmitting] = useState(false)
|
||||
const [error, setError] = useState<string | null>(null)
|
||||
|
||||
const hasLinkParams = Boolean(emailFromQuery && otpFromQuery)
|
||||
const emailLocked = Boolean(emailFromQuery)
|
||||
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(() => {
|
||||
return email.trim().length > 0 && otp.trim().length > 0 && password.length >= 8
|
||||
}, [email, otp, password])
|
||||
|
|
@ -102,148 +148,181 @@ export function ResetPasswordPage() {
|
|||
Account Recovery
|
||||
</p>
|
||||
<h1 className="mb-2 text-3xl font-bold tracking-tight text-grayScale-600">
|
||||
Reset password
|
||||
{verifyState === "invalid" ? invalidTitle : "Reset password"}
|
||||
</h1>
|
||||
<p className="text-sm leading-relaxed text-grayScale-400">
|
||||
{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."}
|
||||
{verifyState === "loading"
|
||||
? "Checking your reset link…"
|
||||
: 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>
|
||||
</div>
|
||||
|
||||
{!hasLinkParams ? (
|
||||
<div className="mb-5 rounded-lg border border-amber-200 bg-amber-50 px-3 py-2 text-sm text-amber-800">
|
||||
Missing reset link details. You can enter them manually, or{" "}
|
||||
<Link to="/forgot-password" className="font-semibold underline">
|
||||
request a new link
|
||||
</Link>
|
||||
.
|
||||
{verifyState === "loading" ? (
|
||||
<div className="flex flex-col items-center gap-3 py-16">
|
||||
<SpinnerIcon className="h-8 w-8" />
|
||||
<p className="text-sm text-grayScale-400">Verifying reset link…</p>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<label
|
||||
htmlFor="reset-email"
|
||||
className="mb-1.5 block text-sm font-medium text-grayScale-600"
|
||||
>
|
||||
Email address
|
||||
</label>
|
||||
<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>
|
||||
{verifyState === "invalid" ? (
|
||||
<div className="space-y-4 rounded-xl border border-red-200 bg-red-50 px-4 py-4">
|
||||
<div className="flex gap-3">
|
||||
<AlertCircle className="mt-0.5 h-5 w-5 shrink-0 text-red-600" />
|
||||
<div className="space-y-2 text-sm text-red-800">
|
||||
<p className="font-semibold">{invalidTitle}</p>
|
||||
<p>{invalidDescription}</p>
|
||||
</div>
|
||||
</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"
|
||||
<Link
|
||||
to="/forgot-password"
|
||||
className="inline-block text-sm font-semibold text-red-800 underline"
|
||||
>
|
||||
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"
|
||||
/>
|
||||
Request a new reset link
|
||||
</Link>
|
||||
</div>
|
||||
) : null}
|
||||
|
||||
{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
|
||||
{verifyState === "ready" ? (
|
||||
<>
|
||||
{!hasLinkParams ? (
|
||||
<div className="mb-5 rounded-lg border border-amber-200 bg-amber-50 px-3 py-2 text-sm text-amber-800">
|
||||
Missing reset link details. You can enter them manually, or{" "}
|
||||
<Link to="/forgot-password" className="font-semibold underline">
|
||||
request a new link
|
||||
</Link>
|
||||
)}
|
||||
</div>
|
||||
) : null}
|
||||
.
|
||||
</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>
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
<div>
|
||||
<label
|
||||
htmlFor="reset-email"
|
||||
className="mb-1.5 block text-sm font-medium text-grayScale-600"
|
||||
>
|
||||
Email address
|
||||
</label>
|
||||
<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>
|
||||
<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">
|
||||
© {new Date().getFullYear()} Yimaru Academy · All rights reserved
|
||||
|
|
|
|||
|
|
@ -157,3 +157,21 @@ export interface TeamPasswordResetResponse {
|
|||
success?: boolean
|
||||
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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user