diff --git a/frontend/src/components/partials/LoginForm.tsx b/frontend/src/components/partials/LoginForm.tsx index 313f3d5..710ee19 100644 --- a/frontend/src/components/partials/LoginForm.tsx +++ b/frontend/src/components/partials/LoginForm.tsx @@ -1,76 +1,97 @@ -import React, { useState } from "react"; +import { useEffect, useState } from "react"; +import { useNavigate } from "react-router-dom"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button" import { Card, CardContent } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" +import { CircleAlert } from "lucide-react"; import { login } from "@/services/auth"; export function LoginForm({ - className, - ...props + className, + ...props }: React.ComponentProps<"div">) { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); - const handelSubmit = async (e) => { + const navigate = useNavigate(); + const [loading, setLoading] = useState(false); + const [error, setError] = useState(null); + + + const handelLogin = async (e) => { e.preventDefault(); try { + setLoading(true) await login(username, password); - window.location.href = "/"; - } catch (error) { - alert("Login failed!"); + + // Get the stored redirect path from localStorage + const redirectPath = localStorage.getItem("redirect_after_login") || "/"; + localStorage.removeItem("redirect_after_login"); // clear the redirect path after use + + // Redirect to the stored path + navigate(redirectPath, { replace: true }); + } catch (err) { + setError("Invalid credentials. Please try again."); + console.log(err) + } finally { + setLoading(false); } }; - return ( -
- - -
-
-
-

Welcome back

-

- Login to your Gallery Archive account -

-
-
- - setUsername(e.target.value)} - /> -
-
- - setPassword(e.target.value)} - /> -
- - {/*
+ return ( +
+ + + +
+
+

Welcome back

+

+ Login to your {__SITE_NAME__} account +

+
+ {error &&
+ +

{error}

+
} +
+ + setUsername(e.target.value)} + /> +
+
+ + setPassword(e.target.value)} + /> +
+ + {/*
Or continue with @@ -104,27 +125,27 @@ export function LoginForm({ Login with Meta
*/} -
- Don't have an account?{" "} - - Sign up - -
+
+ Don't have an account?{" "} + + Sign up + +
+
+ +
+ Image +
+
+
+
+ By clicking continue, you agree to our Terms of Service{" "} + and Privacy Policy.
- -
- Image -
- - -
- By clicking continue, you agree to our Terms of Service{" "} - and Privacy Policy. -
-
- ) +
+ ) }