Spaces:
Runtime error
Runtime error
feat: add Forgot Password & Reset Password pages
Browse filesFiles changed (4)
1 unmodified line23456737 unmodified lines4546474849501 unmodified lineimport { AuthProvider, useAuth } from "@/context/AuthContext";import Login from "@/pages/Login";import Signup from "@/pages/Signup";import { QueryClient, QueryClientProvider } from "@tanstack/react-query";import { Toaster } from "@/components/ui/toaster";import { Toaster as SonnerToaster } from "@/components/ui/sonner";37 unmodified lines<Switch><Route path="/login" component={Login} /><Route path="/signup" component={Signup} /><Route path="/"><ProtectedRoute component={Dashboard} /></Route>1 unmodified line2345678937 unmodified lines47484950515253541 unmodified lineimport { AuthProvider, useAuth } from "@/context/AuthContext";import Login from "@/pages/Login";import Signup from "@/pages/Signup";import ForgotPassword from "@/pages/ForgotPassword";import ResetPassword from "@/pages/ResetPassword";import { QueryClient, QueryClientProvider } from "@tanstack/react-query";import { Toaster } from "@/components/ui/toaster";import { Toaster as SonnerToaster } from "@/components/ui/sonner";37 unmodified lines<Switch><Route path="/login" component={Login} /><Route path="/signup" component={Signup} /><Route path="/forgot-password" component={ForgotPassword} /><Route path="/reset-password" component={ResetPassword} /><Route path="/"><ProtectedRoute component={Dashboard} /></Route>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119import React, { useState } from "react";import { useLocation } from "wouter";import { supabase } from "../lib/supabase";import { Button } from "../components/ui/button";import { Input } from "../components/ui/input";import { Label } from "../components/ui/label";import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle,} from "../components/ui/card";import { toast } from "sonner";
export default function ForgotPassword() { const [, setLocation] = useLocation(); const [email, setEmail] = useState(""); const [isLoading, setIsLoading] = useState(false); const [sent, setSent] = useState(false);
const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setIsLoading(true);
try { const redirectTo = `${window.location.origin}${ import.meta.env.BASE_URL?.replace(/\/$/, "") ?? "" }/reset-password`;
const { error } = await supabase.auth.resetPasswordForEmail(email, { redirectTo, });
if (error) { toast.error(error.message); } else { setSent(true); toast.success("تم إرسال رابط إعادة التعيين على إيميلك"); } } catch { toast.error("حدث خطأ في الاتصال. حاول مرة أخرى."); } finally { setIsLoading(false); } };
return ( <div className="min-h-screen flex items-center justify-center bg-muted/30 p-4"> <Card className="w-full max-w-md"> <CardHeader className="space-y-1 text-center"> <CardTitle className="text-2xl font-bold">نسيت كلمة المرور؟</CardTitle> <CardDescription> {sent ? "تم الإرسال! تحقق من بريدك الإلكتروني واضغط على الرابط لإعادة تعيين كلمة المرور." : "أدخل إيميلك وسنبعتلك رابط لإعادة تعيين كلمة المرور."} </CardDescription> </CardHeader>
{!sent && ( <form onSubmit={handleSubmit}> <CardContent className="space-y-4"> <div className="space-y-2"> <Label htmlFor="forgot-email">البريد الإلكتروني</Label> <Input id="forgot-email" type="email" placeholder="example@email.com" value={email} onChange={(e) => setEmail(e.target.value)} required dir="ltr" /> </div> </CardContent> <CardFooter className="flex flex-col space-y-4"> <Button type="submit" className="w-full" disabled={isLoading}> {isLoading ? "جاري الإرسال..." : "إرسال رابط إعادة التعيين"} </Button> <Button type="button" variant="link" className="p-0 text-muted-foreground" onClick={() => setLocation("/login")} > رجوع لتسجيل الدخول </Button> </CardFooter> </form> )}
{sent && ( <CardFooter className="flex flex-col space-y-4 pt-0"> <Button type="button" variant="outline" className="w-full" onClick={() => { setSent(false); setEmail(""); }} > إعادة الإرسال </Button> <Button type="button" variant="link" className="p-0 text-muted-foreground" onClick={() => setLocation("/login")} > رجوع لتسجيل الدخول </Button> </CardFooter> )} </Card> </div> );}86 unmodified lines87888990919286 unmodified linesSign up</Button></div></CardFooter></form></Card>86 unmodified lines878889909192939495969786 unmodified linesSign up</Button></div><div className="text-sm text-center text-muted-foreground"><Button variant="link" className="p-0" onClick={() => setLocation("/forgot-password")}>نسيت كلمة المرور؟</Button></div></CardFooter></form></Card>
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133import React, { useState, useEffect } from "react";import { useLocation } from "wouter";import { supabase } from "../lib/supabase";import { Button } from "../components/ui/button";import { Input } from "../components/ui/input";import { Label } from "../components/ui/label";import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle,} from "../components/ui/card";import { toast } from "sonner";
export default function ResetPassword() { const [, setLocation] = useLocation(); const [password, setPassword] = useState(""); const [confirmPassword, setConfirmPassword] = useState(""); const [isLoading, setIsLoading] = useState(false); const [isReady, setIsReady] = useState(false);
useEffect(() => { // Supabase يُرسل access_token في الـ hash عند الضغط على رابط الإيميل // نستمع لـ PASSWORD_RECOVERY event للتأكد إن الرابط صحيح const { data: { subscription }, } = supabase.auth.onAuthStateChange((event) => { if (event === "PASSWORD_RECOVERY") { setIsReady(true); } }); return () => subscription.unsubscribe(); }, []);
const handleReset = async (e: React.FormEvent) => { e.preventDefault();
if (password !== confirmPassword) { toast.error("كلمتا المرور غير متطابقتين"); return; }
if (password.length < 6) { toast.error("كلمة المرور يجب أن تكون 6 أحرف على الأقل"); return; }
setIsLoading(true); try { const { error } = await supabase.auth.updateUser({ password });
if (error) { toast.error(error.message); } else { toast.success("تم تغيير كلمة المرور بنجاح!"); await supabase.auth.signOut(); setLocation("/login"); } } catch { toast.error("حدث خطأ. حاول مرة أخرى."); } finally { setIsLoading(false); } };
if (!isReady) { return ( <div className="min-h-screen flex items-center justify-center bg-muted/30 p-4"> <Card className="w-full max-w-md"> <CardHeader className="space-y-1 text-center"> <CardTitle className="text-2xl font-bold">جاري التحقق...</CardTitle> <CardDescription> يرجى الانتظار بينما نتحقق من رابط إعادة التعيين. </CardDescription> </CardHeader> <CardFooter> <Button variant="link" className="w-full" onClick={() => setLocation("/forgot-password")} > طلب رابط جديد </Button> </CardFooter> </Card> </div> ); }
return ( <div className="min-h-screen flex items-center justify-center bg-muted/30 p-4"> <Card className="w-full max-w-md"> <CardHeader className="space-y-1 text-center"> <CardTitle className="text-2xl font-bold"> تعيين كلمة مرور جديدة </CardTitle> <CardDescription>أدخل كلمة المرور الجديدة</CardDescription> </CardHeader> <form onSubmit={handleReset}> <CardContent className="space-y-4"> <div className="space-y-2"> <Label htmlFor="new-password">كلمة المرور الجديدة</Label> <Input id="new-password" type="password" value={password} onChange={(e) => setPassword(e.target.value)} required /> </div> <div className="space-y-2"> <Label htmlFor="confirm-new-password">تأكيد كلمة المرور</Label> <Input id="confirm-new-password" type="password" value={confirmPassword} onChange={(e) => setConfirmPassword(e.target.value)} required /> </div> </CardContent> <CardFooter> <Button type="submit" className="w-full" disabled={isLoading}> {isLoading ? "جاري الحفظ..." : "حفظ كلمة المرور الجديدة"} </Button> </CardFooter> </form> </Card> </div> );}artifacts/design-studio/src/App.tsx+4
artifacts/design-studio/src/pages/ForgotPassword.tsxAdded+119
artifacts/design-studio/src/pages/Login.tsx+5
artifacts/design-studio/src/pages/ResetPassword.tsxAdded+133