diff --git a/package-lock.json b/package-lock.json index 1064fe2..dcc6007 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,6 +7,7 @@ "": { "name": "step-forward", "version": "0.0.0", + "license": "SEE LICENSE IN LICENSE", "dependencies": { "firebase": "^12.16.0", "html2canvas-pro": "^2.2.4", @@ -2371,7 +2372,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "Apache-2.0", "optional": true, "os": [ @@ -2394,7 +2394,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "Apache-2.0", "optional": true, "os": [ @@ -2417,7 +2416,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "LGPL-3.0-or-later", "optional": true, "os": [ @@ -2434,7 +2432,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "LGPL-3.0-or-later", "optional": true, "os": [ @@ -2451,7 +2448,6 @@ "cpu": [ "arm" ], - "dev": true, "license": "LGPL-3.0-or-later", "optional": true, "os": [ @@ -2468,7 +2464,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "LGPL-3.0-or-later", "optional": true, "os": [ @@ -2485,7 +2480,6 @@ "cpu": [ "s390x" ], - "dev": true, "license": "LGPL-3.0-or-later", "optional": true, "os": [ @@ -2502,7 +2496,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "LGPL-3.0-or-later", "optional": true, "os": [ @@ -2519,7 +2512,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "LGPL-3.0-or-later", "optional": true, "os": [ @@ -2536,7 +2528,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "LGPL-3.0-or-later", "optional": true, "os": [ @@ -2553,7 +2544,6 @@ "cpu": [ "arm" ], - "dev": true, "license": "Apache-2.0", "optional": true, "os": [ @@ -2576,7 +2566,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "Apache-2.0", "optional": true, "os": [ @@ -2599,7 +2588,6 @@ "cpu": [ "s390x" ], - "dev": true, "license": "Apache-2.0", "optional": true, "os": [ @@ -2622,7 +2610,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "Apache-2.0", "optional": true, "os": [ @@ -2645,7 +2632,6 @@ "cpu": [ "arm64" ], - "dev": true, "license": "Apache-2.0", "optional": true, "os": [ @@ -2668,7 +2654,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "Apache-2.0", "optional": true, "os": [ @@ -2691,7 +2676,6 @@ "cpu": [ "wasm32" ], - "dev": true, "license": "Apache-2.0 AND LGPL-3.0-or-later AND MIT", "optional": true, "dependencies": { @@ -2711,7 +2695,6 @@ "cpu": [ "ia32" ], - "dev": true, "license": "Apache-2.0 AND LGPL-3.0-or-later", "optional": true, "os": [ @@ -2731,7 +2714,6 @@ "cpu": [ "x64" ], - "dev": true, "license": "Apache-2.0 AND LGPL-3.0-or-later", "optional": true, "os": [ diff --git a/src/components/RoleSelector.jsx b/src/components/RoleSelector.jsx new file mode 100644 index 0000000..258dd4e --- /dev/null +++ b/src/components/RoleSelector.jsx @@ -0,0 +1,53 @@ +import { Check } from "lucide-react"; +import { ROLE_META } from "../services/roles"; + +// Single source for the role-choice UI shown at signup (Register.jsx's full +// form, and Login.jsx's username-method create-on-login fallback) — not +// under components/ui since it's domain-specific (recipient/provider), +// mirroring RoleBadge.jsx's split between the generic Badge and this +// role-aware wrapper. See roles.js for label/icon; descriptions live here +// since nothing else needs them. +const ROLE_DESCRIPTIONS = { + recipient: "ממלאים תוכנית אישית לקידום מטרות ויכולים לשתף אותה עם נותני שירות", + provider: "מלווים תוכניות של מקבלי שירות שבחרו לשתף איתם", +}; + +const ROLES = Object.entries(ROLE_META).map(([value, meta]) => ({ + value, + label: meta.label, + icon: meta.icon, + description: ROLE_DESCRIPTIONS[value], +})); + +const RoleSelector = ({ value, onChange, label = "סוג המשתמש", className = "" }) => ( +
+ {label && {label}} +
+ {ROLES.map(({ value: roleValue, label: roleLabel, description, icon: Icon }) => { + const selected = value === roleValue; + return ( + + ); + })} +
+
+); + +export default RoleSelector; diff --git a/src/pages/Login.jsx b/src/pages/Login.jsx index c9e223d..0079a52 100644 --- a/src/pages/Login.jsx +++ b/src/pages/Login.jsx @@ -1,21 +1,32 @@ import React, { useState } from "react"; import { auth, provider } from "../services/firebase"; -import { signInWithPopup, signInWithEmailAndPassword } from "firebase/auth"; +import { signInWithPopup, signInWithEmailAndPassword, createUserWithEmailAndPassword } from "firebase/auth"; import { useNavigate, Link } from "react-router-dom"; import { toast } from "react-toastify"; import { Footprints, Lock, LogIn, Mail, AtSign } from "lucide-react"; import Button from "../components/ui/Button"; import TextField from "../components/ui/TextField"; import SegmentedToggle from "../components/ui/SegmentedToggle"; -import { fetchUserProfile } from "../services/userProfile"; -import { syntheticEmailForUsername } from "../services/anonymousAccount"; +import { useAuth } from "../context/useAuth"; +import RoleSelector from "../components/RoleSelector"; +import { fetchUserProfile, createUserProfile } from "../services/userProfile"; +import { claimUsername } from "../services/usernameIndex"; +import { USERNAME_REGEX, normalizeUsername, syntheticEmailForUsername } from "../services/anonymousAccount"; const Login = () => { const navigate = useNavigate(); + const { refreshProfile } = useAuth(); const [loading, setLoading] = useState(false); const [method, setMethod] = useState("email"); // "email" | "username" const [identifier, setIdentifier] = useState(""); // email or username, depending on method const [password, setPassword] = useState(""); + // Set once a username-method sign-in comes back user-not-found — asks + // for the one piece of information account creation still needs (role) + // before creating the account, instead of guessing it. + const [pendingSignup, setPendingSignup] = useState(false); + const [signupRole, setSignupRole] = useState("recipient"); + + const resetPendingSignup = () => setPendingSignup(false); // Google sign-in auto-creates the Firebase Auth account for a brand-new // user too — if there's no users/{uid} profile doc yet, or the profile @@ -58,10 +69,19 @@ const Login = () => { toast.success("ההתחברות בוצעה בהצלחה", { position: "bottom-center" }); await goToAppOrFinishRegistration(user); } catch (error) { + // The username method doubles as signup for anonymous (no-email) + // accounts — nobody needs a separate /register trip just to pick + // a username and password. A username that doesn't exist yet + // isn't created immediately though: role (recipient/provider) is + // still required on every account and isn't guessable, so this + // only reveals the role picker below and waits for + // handleConfirmSignup instead of creating anything yet. + if (method === "username" && error.code === "auth/user-not-found") { + setPendingSignup(true); + return; + } if (error.code === "auth/user-not-found") { - toast.error(method === "username" ? "שם משתמש זה לא קיים" : "משתמש זה לא קיים", { - position: "bottom-center", - }); + toast.error("משתמש זה לא קיים", { position: "bottom-center" }); } else if (error.code === "auth/wrong-password") { toast.error("הסיסמה שגויה", { position: "bottom-center" }); } else { @@ -72,6 +92,55 @@ const Login = () => { } }; + // Confirms the pending signup (see handleFormSubmit above) once a role + // has been chosen — creates a fresh anonymous account for the typed + // username/password and enters directly, mirroring Register.jsx's + // anonymous signup (see claimUsername/createUserProfile there). + const handleConfirmSignup = async () => { + const normalizedUsername = normalizeUsername(identifier); + if (!USERNAME_REGEX.test(normalizedUsername)) { + toast.error("שם משתמש יכול להכיל רק אותיות אנגליות קטנות, ספרות וקו תחתון, באורך 3-20 תווים", { + position: "bottom-center", + }); + return; + } + + const syntheticEmail = syntheticEmailForUsername(normalizedUsername); + try { + setLoading(true); + const { user } = await createUserWithEmailAndPassword(auth, syntheticEmail, password); + try { + await claimUsername(user, signupRole, normalizedUsername); + } catch { + // The username is baked permanently into this account's Auth + // email, so retrying with a different username on the same + // account isn't possible — delete it (safe: nothing was + // written to Firestore yet) and let the user submit again. + await user.delete(); + toast.error("שם המשתמש הזה כבר תפוס, יש לנסות שוב", { position: "bottom-center" }); + return; + } + await createUserProfile(user, signupRole, { username: normalizedUsername, isAnonymous: true }); + await refreshProfile(user); + toast.success("נוצר חשבון חדש והתחברת בהצלחה", { position: "bottom-center" }); + toast.info("זהו חשבון ללא מייל — לא ניתן לשחזר אותו אם הסיסמה תישכח", { + position: "bottom-center", + autoClose: 8000, + }); + navigate("/form"); + } catch (error) { + if (error.code === "auth/email-already-in-use") { + toast.error("שם המשתמש הזה כבר תפוס, יש לנסות שוב", { position: "bottom-center" }); + } else if (error.code === "auth/weak-password") { + toast.error("הסיסמה חלשה מדי, נדרשים לפחות 6 תווים", { position: "bottom-center" }); + } else { + toast.error("שגיאה: " + error.message, { position: "bottom-center" }); + } + } finally { + setLoading(false); + } + }; + return (
@@ -103,6 +172,7 @@ const Login = () => { onChange={(value) => { setMethod(value); setIdentifier(""); + resetPendingSignup(); }} options={[ { value: "email", label: "אימייל" }, @@ -119,8 +189,16 @@ const Login = () => { type={method === "email" ? "email" : "text"} placeholder={method === "email" ? "example@email.com" : "שם משתמש"} value={identifier} - onChange={(e) => setIdentifier(e.target.value)} + onChange={(e) => { + setIdentifier(e.target.value); + resetPendingSignup(); + }} disabled={loading} + hint={ + method === "username" + ? "שם משתמש שלא קיים עדיין ייצור עבורו חשבון חדש (ללא מייל, לא ניתן לשחזור אם הסיסמה תישכח)" + : undefined + } /> { type="password" placeholder="יש להזין סיסמה" value={password} - onChange={(e) => setPassword(e.target.value)} + onChange={(e) => { + setPassword(e.target.value); + resetPendingSignup(); + }} disabled={loading} /> - + {pendingSignup ? ( + <> +

+ שם המשתמש "{identifier}" עדיין לא קיים — ליצירת חשבון חדש יש לבחור סוג משתמש: +

+ +
+ + +
+ + ) : ( + + )}
diff --git a/src/pages/Register.jsx b/src/pages/Register.jsx index 6847bcf..ed868ef 100644 --- a/src/pages/Register.jsx +++ b/src/pages/Register.jsx @@ -14,24 +14,12 @@ import TextField from "../components/ui/TextField"; import Spinner from "../components/ui/Spinner"; import SegmentedToggle from "../components/ui/SegmentedToggle"; import ConfirmDialog from "../components/ui/ConfirmDialog"; +import RoleSelector from "../components/RoleSelector"; import { useAuth } from "../context/useAuth"; import { createUserProfile, fetchUserProfile, updateUserProfile } from "../services/userProfile"; import { claimUsername } from "../services/usernameIndex"; import { resolveUsernameToUser } from "../services/resolveUsernameToUser"; import { USERNAME_REGEX, normalizeUsername, syntheticEmailForUsername } from "../services/anonymousAccount"; -import { ROLE_META } from "../services/roles"; - -const ROLE_DESCRIPTIONS = { - recipient: "ממלאים תוכנית אישית לקידום מטרות ויכולים לשתף אותה עם נותני שירות", - provider: "מלווים תוכניות של מקבלי שירות שבחרו לשתף איתם", -}; - -const ROLES = Object.entries(ROLE_META).map(([value, meta]) => ({ - value, - label: meta.label, - icon: meta.icon, - description: ROLE_DESCRIPTIONS[value], -})); const validateUsernameFormat = (value) => { const normalized = normalizeUsername(value); @@ -361,36 +349,7 @@ const Register = () => { } }; - const roleSelector = ( -
- סוג המשתמש -
- {ROLES.map(({ value, label, description, icon: Icon }) => { - const selected = role === value; - return ( - - ); - })} -
-
- ); + const roleSelector = ; const needsCompletion = needsRole || needsUsername;