From 7563d89bc1035985568508057f52184bf006c2cb Mon Sep 17 00:00:00 2001 From: Yared Yemane Date: Fri, 10 Jul 2026 06:18:18 -0700 Subject: [PATCH] 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 --- src/api/http.ts | 4 +- src/api/team.api.ts | 15 ++ src/app/AppRoutes.tsx | 2 + src/lib/teamPasswordReset.ts | 47 +++++ src/pages/auth/ForgotPasswordPage.tsx | 80 +++++--- src/pages/auth/LoginPage.tsx | 8 + src/pages/auth/ResetPasswordPage.tsx | 255 ++++++++++++++++++++++++++ src/types/team.types.ts | 18 ++ 8 files changed, 405 insertions(+), 24 deletions(-) create mode 100644 src/lib/teamPasswordReset.ts create mode 100644 src/pages/auth/ResetPasswordPage.tsx diff --git a/src/api/http.ts b/src/api/http.ts index f690b0a..b9f7ee6 100644 --- a/src/api/http.ts +++ b/src/api/http.ts @@ -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") ); }; diff --git a/src/api/team.api.ts b/src/api/team.api.ts index bda1ebe..9afe06a 100644 --- a/src/api/team.api.ts +++ b/src/api/team.api.ts @@ -18,6 +18,9 @@ import type { UpdateTeamMeRequest, TeamMember, TeamMemberDetail, + TeamResetPasswordRequest, + TeamPasswordResetResponse, + TeamSendPasswordResetRequest, } from "../types/team.types" function isRecord(value: unknown): value is Record { @@ -153,6 +156,18 @@ export const updateTeamMember = (id: number, data: UpdateTeamMemberRequest) => export const changeTeamMemberPassword = (id: number, data: ChangeTeamMemberPasswordRequest) => http.post(`/team/members/${id}/change-password`, data) +/** POST /team/sendResetCode — public; emails a password-reset link. */ +export const sendTeamPasswordReset = (data: TeamSendPasswordResetRequest) => + http.post("/team/sendResetCode", data, { + skipErrorToast: true, + }) + +/** POST /team/resetPassword — public; completes reset with email + OTP from the link. */ +export const resetTeamPassword = (data: TeamResetPasswordRequest) => + http.post("/team/resetPassword", data, { + skipErrorToast: true, + }) + /** POST /team/members/invite — send invitation email (permission: team.members.invite). */ export const inviteTeamMember = (data: InviteTeamMemberRequest) => http.post("/team/members/invite", data, { diff --git a/src/app/AppRoutes.tsx b/src/app/AppRoutes.tsx index 6b258c5..c8b5d17 100644 --- a/src/app/AppRoutes.tsx +++ b/src/app/AppRoutes.tsx @@ -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() { } /> } /> + } /> } /> } /> } /> diff --git a/src/lib/teamPasswordReset.ts b/src/lib/teamPasswordReset.ts new file mode 100644 index 0000000..742ae36 --- /dev/null +++ b/src/lib/teamPasswordReset.ts @@ -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 + 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." +} diff --git a/src/pages/auth/ForgotPasswordPage.tsx b/src/pages/auth/ForgotPasswordPage.tsx index 80c3b72..dc33319 100644 --- a/src/pages/auth/ForgotPasswordPage.tsx +++ b/src/pages/auth/ForgotPasswordPage.tsx @@ -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(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 (
- {/* Decorative left panel */} -
- {/* Abstract decorative shapes */} +
@@ -31,7 +51,6 @@ export function ForgotPasswordPage() {
- {/* Brand logo */}

Manage your academy, track student progress, and streamline @@ -40,15 +59,12 @@ export function ForgotPasswordPage() {

- {/* Right panel – form */}
- {/* Mobile-only logo */}
- {/* Back link */} {submitted ? ( - /* Success state */

Check your email

- We've sent a password reset link to{" "} - {email}. - Please check your inbox and follow the instructions. + We've sent a password reset link to{" "} + {email.trim()}. + The link expires in 5 minutes — open it promptly to set a new password.

) : ( - /* Form state */ <> - {/* Header */}

Account Recovery @@ -90,7 +106,7 @@ export function ForgotPasswordPage() { Forgot password?

- No worries — enter your email and we'll send you a reset link. + Enter your team email and we'll send you a reset link.

@@ -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" />
+ {error ? ( +

+ {error} +

+ ) : null} + )} - {/* Footer */}

© {new Date().getFullYear()} Yimaru Academy · All rights reserved

diff --git a/src/pages/auth/LoginPage.tsx b/src/pages/auth/LoginPage.tsx index 5e9fc34..dc02fc6 100644 --- a/src/pages/auth/LoginPage.tsx +++ b/src/pages/auth/LoginPage.tsx @@ -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", { diff --git a/src/pages/auth/ResetPasswordPage.tsx b/src/pages/auth/ResetPasswordPage.tsx new file mode 100644 index 0000000..9d3fbd8 --- /dev/null +++ b/src/pages/auth/ResetPasswordPage.tsx @@ -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(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 ( +
+
+
+
+
+
+
+
+ +

+ Choose a strong password to secure your admin account. +

+
+
+ +
+
+
+ +
+ + + + Back to sign in + + +
+

+ Account Recovery +

+

+ Reset password +

+

+ {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."} +

+
+ + {!hasLinkParams ? ( +
+ Missing reset link details. You can enter them manually, or{" "} + + request a new link + + . +
+ ) : null} + +
+
+ + setEmail(e.target.value)} + readOnly={emailLocked} + required + autoComplete="email" + disabled={submitting} + className="h-11 rounded-xl" + /> +
+ +
+ + setOtp(e.target.value)} + readOnly={otpLocked} + required + autoComplete="one-time-code" + disabled={submitting} + className="h-11 rounded-xl" + /> +
+ +
+ +
+ setPassword(e.target.value)} + required + minLength={8} + autoComplete="new-password" + disabled={submitting} + className="h-11 rounded-xl pr-11" + /> + +
+

At least 8 characters

+
+ +
+ + setConfirmPassword(e.target.value)} + required + minLength={8} + autoComplete="new-password" + disabled={submitting} + className="h-11 rounded-xl" + /> +
+ + {error ? ( +
+

{error}

+ {(error.toLowerCase().includes("expired") || + error.toLowerCase().includes("already used") || + error.toLowerCase().includes("invalid reset")) && ( + + Request a new reset link + + )} +
+ ) : null} + + +
+ +

+ © {new Date().getFullYear()} Yimaru Academy · All rights reserved +

+
+
+
+ ) +} diff --git a/src/types/team.types.ts b/src/types/team.types.ts index 70150fb..ef6dbf0 100644 --- a/src/types/team.types.ts +++ b/src/types/team.types.ts @@ -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 +}