-
Notifications
You must be signed in to change notification settings - Fork 0
Feat/forgot password #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
302edb1
parents translate, components receive plain strings
a-elkhiraooui-ciscode 0258a6e
1.0.3
a-elkhiraooui-ciscode a62f9b7
tested in local, bug fixed
a-elkhiraooui-ciscode 3bd91d4
1.0.4
a-elkhiraooui-ciscode 68a2b0a
Merge branch 'master' of https://github.com/CISCODE-MA/AuthKit-UI int…
a-elkhiraooui-ciscode 91f6973
Merge branch 'master' of https://github.com/CISCODE-MA/AuthKit-UI int…
a-elkhiraooui-ciscode b13ea31
forgot and reset password done
a-elkhiraooui-ciscode c20676e
1.0.6
a-elkhiraooui-ciscode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import React, { useState } from "react"; | ||
| import { useT } from "@ciscode/ui-translate-core"; | ||
| import { useNavigate } from "react-router-dom"; | ||
| import { InputField } from "../../components/actions/InputField"; | ||
| import { InlineError } from "../../components/InlineError"; | ||
| import { useAuthConfig } from "../../context/AuthConfigContext"; | ||
| import { useAuthState } from "../../context/AuthStateContext"; | ||
| import { toTailwindColorClasses } from "../../utils/colorHelpers"; | ||
|
|
||
| export const ForgotPasswordPage: React.FC = () => { | ||
| const t = useT("authLib"); | ||
| const navigate = useNavigate(); | ||
| const { colors, brandName = t("brandName", { defaultValue: "MyBrand" }), logoUrl } = useAuthConfig(); | ||
| const { api } = useAuthState(); | ||
|
|
||
| const { bgClass, textClass, borderClass } = toTailwindColorClasses(colors); | ||
| const gradientClass = `${bgClass} bg-gradient-to-r from-white/10 via-white/0 to-white/0`; | ||
|
|
||
| const [email, setEmail] = useState(""); | ||
| const [pending, setPending] = useState(false); | ||
| const [error, setError] = useState<string | null>(null); | ||
| const [sent, setSent] = useState(false); | ||
|
|
||
| async function handleSubmit(e: React.FormEvent) { | ||
| e.preventDefault(); | ||
| if (pending) return; | ||
| setError(null); | ||
| setPending(true); | ||
| try { | ||
| await api.post("/api/auth/forgot-password", { email }); | ||
| // Always show generic success regardless of user existence | ||
| setSent(true); | ||
| } catch (err) { | ||
| // Do not enumerate users; still show success message | ||
| setSent(true); | ||
| console.error("Forgot password request failed", err); | ||
| } finally { | ||
| setPending(false); | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <div className={`flex items-center justify-center min-h-screen p-4 ${gradientClass}`}> | ||
| <div className="flex w-full max-w-xl bg-white rounded-2xl shadow-2xl overflow-hidden"> | ||
| <div className="w-full p-6 md:p-8"> | ||
| <div className="flex items-center justify-between mb-6"> | ||
| {logoUrl ? ( | ||
| <img | ||
| loading="lazy" | ||
| src={logoUrl} | ||
| alt="Brand Logo" | ||
| className={`h-10 rounded-lg border ${borderClass}`} | ||
| /> | ||
| ) : ( | ||
| <h2 className="text-xl font-bold">{brandName}</h2> | ||
| )} | ||
| <button type="button" onClick={() => navigate("/login")} className={`text-sm ${textClass}`}> | ||
| {t("ForgotPasswordPage.backToLogin", { defaultValue: "Back to Sign In" })} | ||
| </button> | ||
| </div> | ||
|
|
||
| <h1 className="text-2xl md:text-3xl font-bold text-gray-800"> | ||
| {t("ForgotPasswordPage.title", { defaultValue: "Forgot your password?" })} | ||
| </h1> | ||
| <p className="mt-2 text-sm text-gray-600"> | ||
| {t("ForgotPasswordPage.subtitle", { defaultValue: "Enter your email to receive a reset link." })} | ||
| </p> | ||
|
|
||
| {error && <InlineError message={error} />} | ||
|
|
||
| {sent ? ( | ||
| <div className="mt-6 rounded-lg border border-green-300 bg-green-50 p-4 text-green-800 text-sm"> | ||
| {t("ForgotPasswordPage.sent", { | ||
| defaultValue: "If the email exists, we’ve sent a reset link. Please check your inbox." | ||
| })} | ||
| </div> | ||
| ) : ( | ||
| <form className="space-y-6 mt-4" onSubmit={handleSubmit}> | ||
| <InputField | ||
| label={t("form.emailLabel")} | ||
| type="email" | ||
| placeholder={t("form.emailPlaceholder")} | ||
| color={borderClass} | ||
| value={email} | ||
| onChange={setEmail} | ||
| /> | ||
| <button | ||
| type="submit" | ||
| disabled={pending || !email} | ||
| className={`relative flex w-full items-center justify-center gap-2 py-3 rounded-lg font-medium transition-colors ${ | ||
| pending ? "opacity-60 cursor-not-allowed" : "" | ||
| } ${bgClass} text-white`} | ||
| > | ||
| {pending && ( | ||
| <svg className="h-4 w-4 animate-spin stroke-current" viewBox="0 0 24 24" fill="none"> | ||
| <circle className="opacity-25" cx="12" cy="12" r="10" strokeWidth="4" /> | ||
| <path className="opacity-75" d="M4 12a8 8 0 018-8" strokeWidth="4" strokeLinecap="round" /> | ||
| </svg> | ||
| )} | ||
| {t("ForgotPasswordPage.sendLink", { defaultValue: "Send Reset Link" })} | ||
| </button> | ||
| </form> | ||
| )} | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import React, { useMemo, useState } from "react"; | ||
| import { useT } from "@ciscode/ui-translate-core"; | ||
| import { useLocation, useNavigate } from "react-router-dom"; | ||
| import { InputField } from "../../components/actions/InputField"; | ||
| import { InlineError } from "../../components/InlineError"; | ||
| import { useAuthConfig } from "../../context/AuthConfigContext"; | ||
| import { useAuthState } from "../../context/AuthStateContext"; | ||
| import { toTailwindColorClasses } from "../../utils/colorHelpers"; | ||
|
|
||
| export const ResetPasswordPage: React.FC = () => { | ||
| const t = useT("authLib"); | ||
| const navigate = useNavigate(); | ||
| const location = useLocation(); | ||
|
|
||
| const { colors, brandName = t("brandName", { defaultValue: "MyBrand" }), logoUrl } = useAuthConfig(); | ||
| const { api } = useAuthState(); | ||
|
|
||
| const { bgClass, textClass, borderClass } = toTailwindColorClasses(colors); | ||
| const gradientClass = `${bgClass} bg-gradient-to-r from-white/10 via-white/0 to-white/0`; | ||
|
|
||
| const token = useMemo(() => new URLSearchParams(location.search).get("token"), [location.search]); | ||
| const [newPassword, setNewPassword] = useState(""); | ||
| const [confirmPassword, setConfirmPassword] = useState(""); | ||
| const [pending, setPending] = useState(false); | ||
| const [error, setError] = useState<string | null>(null); | ||
|
|
||
| const minLength = 6; | ||
| const valid = token && newPassword.length >= minLength && newPassword === confirmPassword; | ||
|
|
||
| async function handleSubmit(e: React.FormEvent) { | ||
| e.preventDefault(); | ||
| if (pending) return; | ||
| setError(null); | ||
|
|
||
| if (!token) { | ||
| setError(t("ResetPasswordPage.invalidLink", { defaultValue: "Invalid reset link." })); | ||
| return; | ||
| } | ||
| if (newPassword.length < minLength) { | ||
| setError( | ||
| t("ResetPasswordPage.tooShort", { defaultValue: `Password must be at least ${minLength} characters.` }) | ||
| ); | ||
| return; | ||
| } | ||
| if (newPassword !== confirmPassword) { | ||
| setError(t("ResetPasswordPage.mismatch", { defaultValue: "Passwords do not match." })); | ||
| return; | ||
| } | ||
|
|
||
| setPending(true); | ||
| try { | ||
| await api.post("/api/auth/reset-password", { token, newPassword }); | ||
| // On success, show brief confirmation then navigate to login | ||
| navigate("/login", { replace: true }); | ||
| } catch (err: any) { | ||
| const status = err?.response?.status; | ||
| if (status === 400 || status === 401 || status === 410) { | ||
| setError( | ||
| t("ResetPasswordPage.invalidOrExpired", { | ||
| defaultValue: "Reset link is invalid or has expired. Request a new one.", | ||
| }) | ||
| ); | ||
| } else { | ||
| setError(t("errors.generic", { defaultValue: "Something went wrong. Please try again." })); | ||
| } | ||
| } finally { | ||
| setPending(false); | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <div className={`flex items-center justify-center min-h-screen p-4 ${gradientClass}`}> | ||
| <div className="flex w-full max-w-xl bg-white rounded-2xl shadow-2xl overflow-hidden"> | ||
| <div className="w-full p-6 md:p-8"> | ||
| <div className="flex items-center justify-between mb-6"> | ||
| {logoUrl ? ( | ||
| <img | ||
| loading="lazy" | ||
| src={logoUrl} | ||
| alt="Brand Logo" | ||
| className={`h-10 rounded-lg border ${borderClass}`} | ||
| /> | ||
| ) : ( | ||
| <h2 className="text-xl font-bold">{brandName}</h2> | ||
| )} | ||
| <button type="button" onClick={() => navigate("/login")} className={`text-sm ${textClass}`}> | ||
| {t("ResetPasswordPage.backToLogin", { defaultValue: "Back to Sign In" })} | ||
| </button> | ||
| </div> | ||
|
|
||
| <h1 className="text-2xl md:text-3xl font-bold text-gray-800"> | ||
| {t("ResetPasswordPage.title", { defaultValue: "Reset your password" })} | ||
| </h1> | ||
| <p className="mt-2 text-sm text-gray-600"> | ||
| {t("ResetPasswordPage.subtitle", { defaultValue: "Choose a new password to access your account." })} | ||
| </p> | ||
|
|
||
| {error && <InlineError message={error} />} | ||
|
|
||
| {!token && ( | ||
| <div className="mt-6 rounded-lg border border-red-300 bg-red-50 p-4 text-red-800 text-sm"> | ||
| {t("ResetPasswordPage.invalidLink", { defaultValue: "Invalid reset link." })} | ||
| </div> | ||
| )} | ||
|
|
||
| <form className="space-y-6 mt-4" onSubmit={handleSubmit}> | ||
| <InputField | ||
| label={t("form.passwordLabel")} | ||
| type="password" | ||
| placeholder={t("form.passwordPlaceholder")} | ||
| color={borderClass} | ||
| value={newPassword} | ||
| onChange={setNewPassword} | ||
| /> | ||
| <InputField | ||
| label={t("ResetPasswordPage.confirmLabel", { defaultValue: "Confirm Password" })} | ||
| type="password" | ||
| placeholder={t("ResetPasswordPage.confirmPlaceholder", { defaultValue: "Re-enter your password" })} | ||
| color={borderClass} | ||
| value={confirmPassword} | ||
| onChange={setConfirmPassword} | ||
| /> | ||
|
|
||
| <button | ||
| type="submit" | ||
| disabled={pending || !valid} | ||
| className={`relative flex w-full items-center justify-center gap-2 py-3 rounded-lg font-medium transition-colors ${ | ||
| pending ? "opacity-60 cursor-not-allowed" : "" | ||
| } ${bgClass} text-white`} | ||
| > | ||
| {pending && ( | ||
| <svg className="h-4 w-4 animate-spin stroke-current" viewBox="0 0 24 24" fill="none"> | ||
| <circle className="opacity-25" cx="12" cy="12" r="10" strokeWidth="4" /> | ||
| <path className="opacity-75" d="M4 12a8 8 0 018-8" strokeWidth="4" strokeLinecap="round" /> | ||
| </svg> | ||
| )} | ||
| {t("ResetPasswordPage.submit", { defaultValue: "Reset Password" })} | ||
| </button> | ||
| </form> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -9,7 +9,8 @@ import { useAuthState } from "../../context/AuthStateContext"; | |||||
| import { InlineError } from "../../components/InlineError"; | ||||||
| import { AuthConfigProps } from "../../models/AuthConfig"; | ||||||
| import { useT } from "@ciscode/ui-translate-core"; | ||||||
| import { useNavigate, useLocation } from "react-router"; | ||||||
| import { useNavigate, useLocation } from "react-router-dom"; | ||||||
| import { Link } from "react-router-dom" | ||||||
|
||||||
| import { Link } from "react-router-dom" | |
| import { Link } from "react-router-dom"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace detected at end of line. Remove the extra space after the closing JSX tag.