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/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")
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -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, {
|
||||||
|
|
|
||||||
|
|
@ -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."
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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,15 +148,46 @@ 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"
|
||||||
|
? "Checking your reset link…"
|
||||||
|
: verifyState === "invalid"
|
||||||
|
? invalidDescription
|
||||||
|
: hasLinkParams
|
||||||
? "Set a new password for your team account. This link expires in 5 minutes."
|
? "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."}
|
: "Enter the email and code from your reset email, then choose a new password."}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{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}
|
||||||
|
|
||||||
|
{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>
|
||||||
|
<Link
|
||||||
|
to="/forgot-password"
|
||||||
|
className="inline-block text-sm font-semibold text-red-800 underline"
|
||||||
|
>
|
||||||
|
Request a new reset link
|
||||||
|
</Link>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
{verifyState === "ready" ? (
|
||||||
|
<>
|
||||||
{!hasLinkParams ? (
|
{!hasLinkParams ? (
|
||||||
<div className="mb-5 rounded-lg border border-amber-200 bg-amber-50 px-3 py-2 text-sm text-amber-800">
|
<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{" "}
|
Missing reset link details. You can enter them manually, or{" "}
|
||||||
|
|
@ -244,6 +321,8 @@ export function ResetPasswordPage() {
|
||||||
)}
|
)}
|
||||||
</Button>
|
</Button>
|
||||||
</form>
|
</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
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user