feat: implement backend auth and fix pet profile payload
This commit is contained in:
1 parent
82ab454647
commit
6027e635db
13 files changed
+493
-114
No files matched your search
@@ -14,8 +14,12 @@ interface AuthModalProps {
|
||||
|
||||
export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
const { setRole, setLoggedIn, fetchProfile } = useUserStore();
|
||||
const [view, setView] = useState<"quick" | "phone" | "otp">("quick");
|
||||
const [view, setView] = useState<"login" | "register" | "otp-phone" | "otp">("login");
|
||||
const [phoneNumber, setPhoneNumber] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [email, setEmail] = useState("");
|
||||
const [firstName, setFirstName] = useState("");
|
||||
const [lastName, setLastName] = useState("");
|
||||
const [otpCode, setOtpCode] = useState("");
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [countdown, setCountdown] = useState(0);
|
||||
@@ -33,8 +37,12 @@ export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
// Reset modal state on open/close
|
||||
useEffect(() => {
|
||||
if (!isOpen) {
|
||||
setView("quick");
|
||||
setView("login");
|
||||
setPhoneNumber("");
|
||||
setPassword("");
|
||||
setEmail("");
|
||||
setFirstName("");
|
||||
setLastName("");
|
||||
setOtpCode("");
|
||||
setIsLoading(false);
|
||||
setCountdown(0);
|
||||
@@ -49,37 +57,41 @@ export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
}
|
||||
}, [countdown]);
|
||||
|
||||
const handleQuickLogin = async (targetRole: UserRole) => {
|
||||
if (targetRole === "User_Guest") {
|
||||
setRole("User_Guest");
|
||||
setLoggedIn(false);
|
||||
toast.success("به عنوان کاربر مهمان وارد شدید.");
|
||||
onClose();
|
||||
return;
|
||||
}
|
||||
|
||||
const handleLogin = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!phoneNumber || !password) return toast.error("شماره موبایل و رمز عبور الزامی است");
|
||||
|
||||
setIsLoading(true);
|
||||
const testPhone = targetRole === "User_PetOwner" ? "09121111111" : "09122222222";
|
||||
|
||||
try {
|
||||
// Step 1: Send OTP to test phone
|
||||
const sendRes = await authService.sendOtp(testPhone);
|
||||
const code = (sendRes as any).code;
|
||||
if (!code) {
|
||||
throw new Error("کد تایید تستی تولید نشد");
|
||||
}
|
||||
|
||||
// Step 2: Verify OTP
|
||||
const verifyRes = await authService.verifyOtp(testPhone, code);
|
||||
if (verifyRes.success) {
|
||||
// Force state sync and fetch profile
|
||||
setRole(targetRole);
|
||||
const res = await authService.login(phoneNumber, password);
|
||||
if (res.success) {
|
||||
await fetchProfile();
|
||||
toast.success(`ورود سریع موفق! در نقش ${targetRole === "User_Partner" ? 'همکار' : 'صاحب پت'} وارد شدید.`);
|
||||
toast.success("با موفقیت وارد شدید");
|
||||
onClose();
|
||||
}
|
||||
} catch (err: any) {
|
||||
toast.error(`خطا در ورود سریع: ${err.message}`);
|
||||
toast.error(err.message || "خطا در ورود");
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handleRegister = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
if (!firstName || !lastName || !phoneNumber || !password) {
|
||||
return toast.error("لطفاً فیلدهای الزامی را پر کنید");
|
||||
}
|
||||
|
||||
setIsLoading(true);
|
||||
try {
|
||||
const res = await authService.register({ firstName, lastName, mobile: phoneNumber, email, password });
|
||||
if (res.success) {
|
||||
await fetchProfile();
|
||||
toast.success("ثبتنام با موفقیت انجام شد");
|
||||
onClose();
|
||||
}
|
||||
} catch (err: any) {
|
||||
toast.error(err.message || "خطا در ثبتنام");
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
@@ -197,68 +209,155 @@ export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
|
||||
{/* Content Panel */}
|
||||
<AnimatePresence mode="wait">
|
||||
{view === "quick" && (
|
||||
<motion.div
|
||||
key="quick-view"
|
||||
initial={{ opacity: 0, y: 10 }}
|
||||
animate={{ opacity: 1, y: 0 }}
|
||||
exit={{ opacity: 0, y: -10 }}
|
||||
className="space-y-6"
|
||||
{view === "login" && (
|
||||
<motion.form
|
||||
key="login-view"
|
||||
initial={{ opacity: 0, x: 20 }}
|
||||
animate={{ opacity: 1, x: 0 }}
|
||||
exit={{ opacity: 0, x: -20 }}
|
||||
onSubmit={handleLogin}
|
||||
className="space-y-4"
|
||||
>
|
||||
<button
|
||||
onClick={() => setView("phone")}
|
||||
className="w-full bg-medical-gray-50 border border-medical-gray-200 rounded-2xl p-5 flex items-center justify-between group hover:border-canina-blue transition-all cursor-pointer shadow-sm"
|
||||
>
|
||||
<div className="flex items-center gap-3">
|
||||
<Phone className="w-5 h-5 text-canina-blue" />
|
||||
<span className="font-black text-sm text-medical-gray-700">ورود با شماره موبایل</span>
|
||||
</div>
|
||||
<div className="w-8 h-8 rounded-xl bg-white border border-medical-gray-100 flex items-center justify-center shadow-sm text-canina-blue group-hover:bg-canina-blue group-hover:text-white transition-all">←</div>
|
||||
</button>
|
||||
|
||||
<div className="pt-6 border-t border-medical-gray-100">
|
||||
<p className="text-[10px] font-black text-medical-gray-400 uppercase tracking-widest text-center mb-4">ورود سریع تستی (با دیتابیس واقعی)</p>
|
||||
<div className="grid grid-cols-3 gap-3">
|
||||
<button
|
||||
onClick={() => handleQuickLogin("User_Guest")}
|
||||
disabled={isLoading}
|
||||
className="flex flex-col items-center gap-2 p-3 rounded-2xl border border-medical-gray-100 hover:bg-medical-gray-50 transition-all group cursor-pointer disabled:opacity-50"
|
||||
>
|
||||
<User className="w-6 h-6 text-medical-gray-400 group-hover:text-canina-blue" />
|
||||
<span className="text-[10px] font-black text-medical-gray-700">مهمان</span>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleQuickLogin("User_PetOwner")}
|
||||
disabled={isLoading}
|
||||
className="flex flex-col items-center gap-2 p-3 rounded-2xl border border-medical-gray-100 hover:bg-medical-gray-50 transition-all group cursor-pointer disabled:opacity-50"
|
||||
>
|
||||
{isLoading ? (
|
||||
<RefreshCw className="w-6 h-6 text-canina-blue animate-spin" />
|
||||
) : (
|
||||
<Heart className="w-6 h-6 text-medical-gray-400 group-hover:text-canina-blue" />
|
||||
)}
|
||||
<span className="text-[10px] font-black text-medical-gray-700">صاحب پت</span>
|
||||
</button>
|
||||
<button
|
||||
onClick={() => handleQuickLogin("User_Partner")}
|
||||
disabled={isLoading}
|
||||
className="flex flex-col items-center gap-2 p-3 rounded-2xl border border-medical-gray-100 hover:bg-medical-gray-50 transition-all group cursor-pointer disabled:opacity-50"
|
||||
>
|
||||
{isLoading ? (
|
||||
<RefreshCw className="w-6 h-6 text-canina-blue animate-spin" />
|
||||
) : (
|
||||
<Building2 className="w-6 h-6 text-medical-gray-400 group-hover:text-canina-blue" />
|
||||
)}
|
||||
<span className="text-[10px] font-black text-medical-gray-700">همکار</span>
|
||||
</button>
|
||||
</div>
|
||||
<div className="space-y-2">
|
||||
<label className="text-xs font-bold text-medical-gray-500">شماره موبایل</label>
|
||||
<input
|
||||
type="tel"
|
||||
maxLength={11}
|
||||
placeholder="مثال: ۰۹۱۲۳۴۵۶۷۸۹"
|
||||
value={phoneNumber}
|
||||
onChange={(e) => setPhoneNumber(e.target.value.replace(/[^0-9]/g, ''))}
|
||||
disabled={isLoading}
|
||||
className="w-full bg-medical-gray-50 border border-medical-gray-200 rounded-xl py-3 px-4 focus:ring-2 focus:ring-canina-blue/20 outline-none font-bold text-left tracking-widest"
|
||||
/>
|
||||
</div>
|
||||
</motion.div>
|
||||
<div className="space-y-2">
|
||||
<label className="text-xs font-bold text-medical-gray-500">رمز عبور</label>
|
||||
<input
|
||||
type="password"
|
||||
placeholder="رمز عبور خود را وارد کنید"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
disabled={isLoading}
|
||||
className="w-full bg-medical-gray-50 border border-medical-gray-200 rounded-xl py-3 px-4 focus:ring-2 focus:ring-canina-blue/20 outline-none text-left"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between pt-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setView("otp-phone")}
|
||||
className="text-xs font-bold text-canina-blue hover:underline"
|
||||
>
|
||||
ورود با کد یکبار مصرف (OTP)
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setView("register")}
|
||||
className="text-xs font-bold text-canina-blue hover:underline"
|
||||
>
|
||||
حساب ندارید؟ ثبتنام
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isLoading || !phoneNumber || !password}
|
||||
className="w-full py-4 mt-4 bg-canina-blue text-white rounded-xl font-black hover:bg-indigo-700 disabled:opacity-50 transition-all shadow-lg shadow-canina-blue/20 flex items-center justify-center gap-2 cursor-pointer"
|
||||
>
|
||||
{isLoading ? <RefreshCw className="w-5 h-5 animate-spin" /> : <>ورود به حساب</>}
|
||||
</button>
|
||||
</motion.form>
|
||||
)}
|
||||
|
||||
{view === "phone" && (
|
||||
{view === "register" && (
|
||||
<motion.form
|
||||
key="phone-view"
|
||||
key="register-view"
|
||||
initial={{ opacity: 0, x: 20 }}
|
||||
animate={{ opacity: 1, x: 0 }}
|
||||
exit={{ opacity: 0, x: -20 }}
|
||||
onSubmit={handleRegister}
|
||||
className="space-y-3"
|
||||
>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<div className="space-y-1">
|
||||
<label className="text-xs font-bold text-medical-gray-500">نام</label>
|
||||
<input
|
||||
type="text"
|
||||
value={firstName}
|
||||
onChange={(e) => setFirstName(e.target.value)}
|
||||
disabled={isLoading}
|
||||
className="w-full bg-medical-gray-50 border border-medical-gray-200 rounded-xl py-2 px-3 focus:ring-2 focus:ring-canina-blue/20 outline-none text-sm"
|
||||
/>
|
||||
</div>
|
||||
<div className="space-y-1">
|
||||
<label className="text-xs font-bold text-medical-gray-500">نام خانوادگی</label>
|
||||
<input
|
||||
type="text"
|
||||
value={lastName}
|
||||
onChange={(e) => setLastName(e.target.value)}
|
||||
disabled={isLoading}
|
||||
className="w-full bg-medical-gray-50 border border-medical-gray-200 rounded-xl py-2 px-3 focus:ring-2 focus:ring-canina-blue/20 outline-none text-sm"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1">
|
||||
<label className="text-xs font-bold text-medical-gray-500">شماره موبایل *</label>
|
||||
<input
|
||||
type="tel"
|
||||
maxLength={11}
|
||||
value={phoneNumber}
|
||||
onChange={(e) => setPhoneNumber(e.target.value.replace(/[^0-9]/g, ''))}
|
||||
disabled={isLoading}
|
||||
className="w-full bg-medical-gray-50 border border-medical-gray-200 rounded-xl py-2 px-3 focus:ring-2 focus:ring-canina-blue/20 outline-none text-left tracking-widest text-sm"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1">
|
||||
<label className="text-xs font-bold text-medical-gray-500">ایمیل (اختیاری)</label>
|
||||
<input
|
||||
type="email"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
disabled={isLoading}
|
||||
className="w-full bg-medical-gray-50 border border-medical-gray-200 rounded-xl py-2 px-3 focus:ring-2 focus:ring-canina-blue/20 outline-none text-left text-sm"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1">
|
||||
<label className="text-xs font-bold text-medical-gray-500">رمز عبور *</label>
|
||||
<input
|
||||
type="password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
disabled={isLoading}
|
||||
className="w-full bg-medical-gray-50 border border-medical-gray-200 rounded-xl py-2 px-3 focus:ring-2 focus:ring-canina-blue/20 outline-none text-left text-sm"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-start pt-1">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setView("login")}
|
||||
className="text-xs font-bold text-canina-blue hover:underline"
|
||||
>
|
||||
بازگشت به ورود
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isLoading || !phoneNumber || !password || !firstName || !lastName}
|
||||
className="w-full py-3 mt-2 bg-canina-blue text-white rounded-xl font-black hover:bg-indigo-700 disabled:opacity-50 transition-all flex items-center justify-center gap-2 cursor-pointer"
|
||||
>
|
||||
{isLoading ? <RefreshCw className="w-4 h-4 animate-spin" /> : <>ثبتنام</>}
|
||||
</button>
|
||||
</motion.form>
|
||||
)}
|
||||
|
||||
{view === "otp-phone" && (
|
||||
<motion.form
|
||||
key="otp-phone-view"
|
||||
initial={{ opacity: 0, x: 20 }}
|
||||
animate={{ opacity: 1, x: 0 }}
|
||||
exit={{ opacity: 0, x: -20 }}
|
||||
@@ -270,10 +369,10 @@ export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
<label className="text-[10px] font-black text-medical-gray-400 uppercase tracking-widest">شماره موبایل</label>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setView("quick")}
|
||||
onClick={() => setView("login")}
|
||||
className="text-[10px] font-black text-canina-blue hover:underline"
|
||||
>
|
||||
برگشت به ورود سریع
|
||||
برگشت به صفحه ورود
|
||||
</button>
|
||||
</div>
|
||||
<div className="relative">
|
||||
@@ -322,7 +421,7 @@ export default function AuthModal({ isOpen, onClose }: AuthModalProps) {
|
||||
<label className="text-[10px] font-black text-medical-gray-400 uppercase tracking-widest">کد تایید پیامکی</label>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setView("phone")}
|
||||
onClick={() => setView("otp-phone")}
|
||||
className="text-[10px] font-black text-canina-blue flex items-center gap-1 hover:underline"
|
||||
>
|
||||
<ArrowRight className="w-3 h-3" />
|
||||
|
||||
@@ -65,6 +65,40 @@ export class AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register with email/mobile and password
|
||||
*/
|
||||
public async register(data: any): Promise<AuthResponse> {
|
||||
try {
|
||||
const response = await api.post('/auth/register', data);
|
||||
const { data: resData } = response.data;
|
||||
if (resData?.accessToken) {
|
||||
localStorage.setItem('accessToken', resData.accessToken);
|
||||
}
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
const message = error.response?.data?.message || 'خطا در ثبتنام';
|
||||
throw new Error(Array.isArray(message) ? message[0] : message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Login with mobile and password
|
||||
*/
|
||||
public async login(mobile: string, password: string): Promise<AuthResponse> {
|
||||
try {
|
||||
const response = await api.post('/auth/login', { mobile, password });
|
||||
const { data } = response.data;
|
||||
if (data?.accessToken) {
|
||||
localStorage.setItem('accessToken', data.accessToken);
|
||||
}
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
const message = error.response?.data?.message || 'نام کاربری یا رمز عبور اشتباه است';
|
||||
throw new Error(Array.isArray(message) ? message[0] : message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current user profile
|
||||
*/
|
||||
|
||||
@@ -72,7 +72,10 @@ export const usePetStore = create<PetStore>()(
|
||||
name: pet.name,
|
||||
type: pet.type,
|
||||
breed: pet.breed || "",
|
||||
weight: Number(pet.weight) || 0
|
||||
weight: Number(pet.weight) || 0,
|
||||
age: Number(pet.age) || 0,
|
||||
activityLevel: pet.activityLevel || "متوسط",
|
||||
medicalConditions: pet.medicalConditions || []
|
||||
});
|
||||
await useUserStore.getState().fetchProfile();
|
||||
} catch (error) {
|
||||
@@ -131,7 +134,10 @@ export const usePetStore = create<PetStore>()(
|
||||
name: updates.name,
|
||||
type: updates.type,
|
||||
breed: updates.breed,
|
||||
weight: updates.weight ? Number(updates.weight) : undefined
|
||||
weight: updates.weight ? Number(updates.weight) : undefined,
|
||||
age: updates.age ? Number(updates.age) : undefined,
|
||||
activityLevel: updates.activityLevel,
|
||||
medicalConditions: updates.medicalConditions
|
||||
});
|
||||
await useUserStore.getState().fetchProfile();
|
||||
} catch (error) {
|
||||
|
||||
Reference in new issue
Block a user