diff --git a/frontend/src/pages/LoginPage.jsx b/frontend/src/pages/LoginPage.jsx index 91b2ce2..4fa7276 100644 --- a/frontend/src/pages/LoginPage.jsx +++ b/frontend/src/pages/LoginPage.jsx @@ -1,8 +1,37 @@ // LoginPage.js -import React from 'react'; -import { Link } from 'react-router-dom'; +import React, { useState } from 'react'; +import { useMutation } from 'react-query'; +import { Link, useNavigate } from 'react-router-dom'; +import { loginService } from '../services/loginService'; +import SpinnerLoader from '../components/Loaders/SpinnerLoader'; +import InlineTextError from '../components/Errors/InlineTextError'; const LoginPage = () => { + + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); + const navigator = useNavigate(); + + const mutation = useMutation(loginService, { + onSuccess: (data) => { + setEmail(''); + setPassword(''); + navigator('/'); + }, + onError: (error) => { + console.log(error); + }, + }); + + const handleLogin = (e) => { + e.preventDefault(); + e.stopPropagation(); + if (!email.trim() || !password.trim()) { + return; + } + mutation.mutate({ email, password }); + } + return (
@@ -16,6 +45,8 @@ const LoginPage = () => { setEmail(e.target.value)} placeholder="Enter your email" className="input input-bordered w-full bg-gray-700 text-white focus:outline-none focus:ring focus:ring-primary" required @@ -28,25 +59,40 @@ const LoginPage = () => { Password setPassword(e.target.value)} placeholder="Enter your password" className="input input-bordered w-full bg-gray-700 text-white focus:outline-none focus:ring focus:ring-primary" required />
+ {/* Error Message */} + {mutation.isError && } + + {/* Success Message */} + {mutation.isSuccess && ( +

+ 🎉 {mutation.data.message || "Login is successfull"} +

+ )} + {/* Forgot Password Link */}
Forgot password?
+ + {/* Submit Button */}
diff --git a/frontend/src/services/loginService.js b/frontend/src/services/loginService.js new file mode 100644 index 0000000..6a7b0bc --- /dev/null +++ b/frontend/src/services/loginService.js @@ -0,0 +1,6 @@ +import axiosInstance from "../helper/axiosInstance"; + +export async function loginService(data) { + const response = await axiosInstance.post("/user/signin", data); + return response.data; +} \ No newline at end of file