feat: wire team forgot/reset password to API

Add /reset-password page, connect forgot-password to sendResetCode, and treat team reset endpoints as public auth routes.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Yared Yemane 2026-07-10 06:18:18 -07:00
parent f34aa809d9
commit 7563d89bc1
8 changed files with 405 additions and 24 deletions

View File

@ -67,7 +67,9 @@ const isAuthEndpointRequest = (url?: string) => {
url.includes("/team/google-login") ||
url.includes("/team/refresh") ||
url.includes("/team/invitations/verify") ||
url.includes("/team/invitations/accept")
url.includes("/team/invitations/accept") ||
url.includes("/team/sendResetCode") ||
url.includes("/team/resetPassword")
);
};

View File

@ -18,6 +18,9 @@ import type {
UpdateTeamMeRequest,
TeamMember,
TeamMemberDetail,
TeamResetPasswordRequest,
TeamPasswordResetResponse,
TeamSendPasswordResetRequest,
} from "../types/team.types"
function isRecord(value: unknown): value is Record<string, unknown> {
@ -153,6 +156,18 @@ export const updateTeamMember = (id: number, data: UpdateTeamMemberRequest) =>
export const changeTeamMemberPassword = (id: number, data: ChangeTeamMemberPasswordRequest) =>
http.post<ChangeTeamMemberPasswordResponse>(`/team/members/${id}/change-password`, data)
/** POST /team/sendResetCode — public; emails a password-reset link. */
export const sendTeamPasswordReset = (data: TeamSendPasswordResetRequest) =>
http.post<TeamPasswordResetResponse>("/team/sendResetCode", data, {
skipErrorToast: true,
})
/** POST /team/resetPassword — public; completes reset with email + OTP from the link. */
export const resetTeamPassword = (data: TeamResetPasswordRequest) =>
http.post<TeamPasswordResetResponse>("/team/resetPassword", data, {
skipErrorToast: true,
})
/** POST /team/members/invite — send invitation email (permission: team.members.invite). */
export const inviteTeamMember = (data: InviteTeamMemberRequest) =>
http.post<InviteTeamMemberResponse>("/team/members/invite", data, {

View File

@ -66,6 +66,7 @@ import { AddTeamMemberPage } from "../pages/team/AddTeamMemberPage";
import { TeamMemberDetailPage } from "../pages/team/TeamMemberDetailPage";
import { LoginPage } from "../pages/auth/LoginPage";
import { ForgotPasswordPage } from "../pages/auth/ForgotPasswordPage";
import { ResetPasswordPage } from "../pages/auth/ResetPasswordPage";
import { VerificationPage } from "../pages/auth/VerificationPage";
import { AcceptInvitePage } from "../pages/auth/AcceptInvitePage";
import { AboutPage } from "../pages/AboutPage";
@ -82,6 +83,7 @@ export function AppRoutes() {
<Routes>
<Route path="/login" element={<LoginPage />} />
<Route path="/forgot-password" element={<ForgotPasswordPage />} />
<Route path="/reset-password" element={<ResetPasswordPage />} />
<Route path="/verification" element={<VerificationPage />} />
<Route path="/accept-invite" element={<AcceptInvitePage />} />
<Route path="/about" element={<AboutPage />} />

View File

@ -0,0 +1,47 @@
import axios from "axios"
/** Prefer API `error` detail over generic `message` for auth recovery flows. */
export function getTeamAuthErrorMessage(error: unknown, fallback: string): string {
if (axios.isAxiosError(error)) {
const data = error.response?.data
if (data && typeof data === "object") {
const record = data as Record<string, unknown>
if (typeof record.error === "string" && record.error.trim()) {
return record.error.trim()
}
if (typeof record.message === "string" && record.message.trim()) {
return record.message.trim()
}
}
}
return fallback
}
export function mapTeamPasswordResetError(error: unknown): string {
const detail = getTeamAuthErrorMessage(error, "")
const lower = detail.toLowerCase()
if (lower.includes("not active")) {
return "This account is inactive. Contact an administrator."
}
if (lower.includes("accept your invitation")) {
return "Finish accepting your invitation before resetting your password."
}
if (lower.includes("not found")) {
return "No team account found for that email."
}
if (lower.includes("expired")) {
return "This reset link has expired (valid for 5 minutes). Request a new one."
}
if (lower.includes("already been used")) {
return "This reset link was already used. Request a new one."
}
if (lower.includes("invalid or missing reset code") || lower.includes("invalid")) {
return "Invalid reset code. Request a new link from Forgot password."
}
if (lower.includes("team_password_reset_base_url")) {
return "Password reset is not configured. Contact an administrator."
}
if (detail) return detail
return "Something went wrong. Please try again."
}

View File

@ -1,28 +1,48 @@
import { useState } from "react"
import { Link } from "react-router-dom"
import { ArrowLeft } from "lucide-react"
import { toast } from "sonner"
import successSrc from "../../assets/success.svg"
import { sendTeamPasswordReset } 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"
export function ForgotPasswordPage() {
const [email, setEmail] = useState("")
const [submitted, setSubmitted] = useState(false)
const [submitting, setSubmitting] = useState(false)
const [error, setError] = useState<string | null>(null)
const handleSubmit = (e: React.FormEvent) => {
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
// Handle forgot password logic here
console.log("Forgot password:", { email })
setSubmitted(true)
setError(null)
const trimmed = email.trim()
if (!trimmed) {
setError("Email is required.")
return
}
setSubmitting(true)
try {
await sendTeamPasswordReset({ email: trimmed })
setSubmitted(true)
toast.success("Reset link sent", {
description: "Check your email. The link expires in 5 minutes.",
})
} catch (err) {
setError(mapTeamPasswordResetError(err))
} finally {
setSubmitting(false)
}
}
return (
<div className="relative flex h-dvh overflow-y-auto">
{/* Decorative left panel */}
<div className="hidden lg:flex lg:w-1/2 xl:w-[55%] items-center justify-center bg-gradient-to-br from-brand-600 via-brand-500 to-brand-400 relative">
{/* Abstract decorative shapes */}
<div className="relative hidden items-center justify-center bg-gradient-to-br from-brand-600 via-brand-500 to-brand-400 lg:flex lg:w-1/2 xl:w-[55%]">
<div className="absolute inset-0 overflow-hidden">
<div className="absolute -left-20 -top-20 h-96 w-96 rounded-full bg-white/5" />
<div className="absolute -bottom-32 -right-16 h-[500px] w-[500px] rounded-full bg-white/5" />
@ -31,7 +51,6 @@ export function ForgotPasswordPage() {
</div>
<div className="relative z-10 max-w-md px-12 text-center">
{/* Brand logo */}
<BrandLogo variant="light" className="mx-auto mb-8 h-16" />
<p className="text-base leading-relaxed text-white/70">
Manage your academy, track student progress, and streamline
@ -40,15 +59,12 @@ export function ForgotPasswordPage() {
</div>
</div>
{/* Right panel – form */}
<div className="flex w-full flex-col items-center justify-center bg-white px-6 py-12 lg:w-1/2 xl:w-[45%]">
<div className="w-full max-w-[420px]">
{/* Mobile-only logo */}
<div className="mb-10 flex justify-center lg:hidden">
<BrandLogo />
</div>
{/* Back link */}
<Link
to="/login"
className="mb-8 inline-flex items-center gap-1.5 text-sm font-medium text-grayScale-400 transition-colors hover:text-grayScale-600"
@ -58,30 +74,30 @@ export function ForgotPasswordPage() {
</Link>
{submitted ? (
/* Success state */
<div className="text-center">
<img src={successSrc} alt="" className="mx-auto mb-6 h-20 w-20" />
<h1 className="mb-2 text-2xl font-bold tracking-tight text-grayScale-600">
Check your email
</h1>
<p className="mb-8 text-sm leading-relaxed text-grayScale-400">
We've sent a password reset link to{" "}
<span className="font-medium text-grayScale-600">{email}</span>.
Please check your inbox and follow the instructions.
We&apos;ve sent a password reset link to{" "}
<span className="font-medium text-grayScale-600">{email.trim()}</span>.
The link expires in 5 minutes — open it promptly to set a new password.
</p>
<Button
type="button"
variant="outline"
className="h-11 rounded-xl px-6 text-sm font-semibold"
onClick={() => setSubmitted(false)}
onClick={() => {
setSubmitted(false)
setError(null)
}}
>
Try a different email
</Button>
</div>
) : (
/* Form state */
<>
{/* Header */}
<div className="mb-10">
<p className="mb-1.5 text-sm font-medium uppercase tracking-widest text-brand-400">
Account Recovery
@ -90,7 +106,7 @@ export function ForgotPasswordPage() {
Forgot password?
</h1>
<p className="text-sm leading-relaxed text-grayScale-400">
No worries — enter your email and we'll send you a reset link.
Enter your team email and we&apos;ll send you a reset link.
</p>
</div>
@ -107,23 +123,41 @@ export function ForgotPasswordPage() {
type="email"
placeholder="you@example.com"
value={email}
onChange={(e) => setEmail(e.target.value)}
onChange={(e) => {
setEmail(e.target.value)
if (error) setError(null)
}}
required
autoComplete="email"
disabled={submitting}
className="h-11 rounded-xl"
/>
</div>
{error ? (
<p className="rounded-lg border border-red-200 bg-red-50 px-3 py-2 text-sm text-red-700">
{error}
</p>
) : null}
<Button
type="submit"
disabled={submitting}
className="mt-2 h-11 w-full rounded-xl text-sm font-semibold tracking-wide"
>
Send reset link
{submitting ? (
<span className="inline-flex items-center gap-2">
<SpinnerIcon className="h-4 w-4" />
Sending…
</span>
) : (
"Send reset link"
)}
</Button>
</form>
</>
)}
{/* Footer */}
<p className="mt-10 text-center text-xs text-grayScale-400">
© {new Date().getFullYear()} Yimaru Academy · All rights reserved
</p>

View File

@ -78,6 +78,14 @@ export function LoginPage() {
setSearchParams({}, { replace: true });
}, [searchParams, setSearchParams]);
useEffect(() => {
if (searchParams.get("reset") !== "1") return;
toast.success("Password reset", {
description: "Sign in with your new password.",
});
setSearchParams({}, { replace: true });
}, [searchParams, setSearchParams]);
useEffect(() => {
if (searchParams.get("account_inactive") !== "1") return;
toast.error("Account inactive", {

View File

@ -0,0 +1,255 @@
import { useMemo, useState } from "react"
import { Link, useNavigate, useSearchParams } from "react-router-dom"
import { ArrowLeft, Eye, EyeOff } from "lucide-react"
import { toast } from "sonner"
import { resetTeamPassword } 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"
export function ResetPasswordPage() {
const navigate = useNavigate()
const [searchParams] = useSearchParams()
const emailFromQuery = searchParams.get("email")?.trim() ?? ""
const otpFromQuery = searchParams.get("otp")?.trim() ?? ""
const [email, setEmail] = useState(emailFromQuery)
const [otp, setOtp] = useState(otpFromQuery)
const [password, setPassword] = useState("")
const [confirmPassword, setConfirmPassword] = useState("")
const [showPassword, setShowPassword] = useState(false)
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 canSubmit = useMemo(() => {
return email.trim().length > 0 && otp.trim().length > 0 && password.length >= 8
}, [email, otp, password])
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
setError(null)
if (!email.trim() || !otp.trim()) {
setError("Email and reset code are required. Open the link from your email or request a new one.")
return
}
if (password.length < 8) {
setError("Password must be at least 8 characters.")
return
}
if (password !== confirmPassword) {
setError("Passwords do not match.")
return
}
setSubmitting(true)
try {
await resetTeamPassword({
email: email.trim(),
otp: otp.trim(),
password,
})
toast.success("Password reset successfully", {
description: "Sign in with your new password.",
})
navigate("/login?reset=1", { replace: true })
} catch (err) {
setError(mapTeamPasswordResetError(err))
} finally {
setSubmitting(false)
}
}
return (
<div className="relative flex h-dvh overflow-y-auto">
<div className="relative hidden items-center justify-center bg-gradient-to-br from-brand-600 via-brand-500 to-brand-400 lg:flex lg:w-1/2 xl:w-[55%]">
<div className="absolute inset-0 overflow-hidden">
<div className="absolute -left-20 -top-20 h-96 w-96 rounded-full bg-white/5" />
<div className="absolute -bottom-32 -right-16 h-[500px] w-[500px] rounded-full bg-white/5" />
<div className="absolute left-1/3 top-1/4 h-64 w-64 rounded-full bg-white/5" />
</div>
<div className="relative z-10 max-w-md px-12 text-center">
<BrandLogo variant="light" className="mx-auto mb-8 h-16" />
<p className="text-base leading-relaxed text-white/70">
Choose a strong password to secure your admin account.
</p>
</div>
</div>
<div className="flex w-full flex-col items-center justify-center bg-white px-6 py-12 lg:w-1/2 xl:w-[45%]">
<div className="w-full max-w-[420px]">
<div className="mb-10 flex justify-center lg:hidden">
<BrandLogo />
</div>
<Link
to="/login"
className="mb-8 inline-flex items-center gap-1.5 text-sm font-medium text-grayScale-400 transition-colors hover:text-grayScale-600"
>
<ArrowLeft size={16} />
Back to sign in
</Link>
<div className="mb-8">
<p className="mb-1.5 text-sm font-medium uppercase tracking-widest text-brand-400">
Account Recovery
</p>
<h1 className="mb-2 text-3xl font-bold tracking-tight text-grayScale-600">
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."}
</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>
.
</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>
</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>
<p className="mt-10 text-center text-xs text-grayScale-400">
© {new Date().getFullYear()} Yimaru Academy · All rights reserved
</p>
</div>
</div>
</div>
)
}

View File

@ -139,3 +139,21 @@ export interface ChangeTeamMemberPasswordResponse {
status_code?: number
metadata?: unknown
}
/** POST /team/sendResetCode — public forgot-password */
export interface TeamSendPasswordResetRequest {
email: string
}
/** POST /team/resetPassword — public complete reset from email link */
export interface TeamResetPasswordRequest {
email: string
otp: string
password: string
}
export interface TeamPasswordResetResponse {
message?: string
success?: boolean
data?: unknown
}