Added login to frontend ✔️
This commit is contained in:
@@ -1,8 +1,37 @@
|
|||||||
// LoginPage.js
|
// LoginPage.js
|
||||||
import React from 'react';
|
import React, { useState } from 'react';
|
||||||
import { Link } from 'react-router-dom';
|
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 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 (
|
return (
|
||||||
<div className="flex justify-center bg-base-200 h-screen p-4">
|
<div className="flex justify-center bg-base-200 h-screen p-4">
|
||||||
<div className="w-full max-w-md rounded-lg p-8">
|
<div className="w-full max-w-md rounded-lg p-8">
|
||||||
@@ -16,6 +45,8 @@ const LoginPage = () => {
|
|||||||
</label>
|
</label>
|
||||||
<input
|
<input
|
||||||
type="email"
|
type="email"
|
||||||
|
value={email}
|
||||||
|
onChange={(e) => setEmail(e.target.value)}
|
||||||
placeholder="Enter your email"
|
placeholder="Enter your email"
|
||||||
className="input input-bordered w-full bg-gray-700 text-white focus:outline-none focus:ring focus:ring-primary"
|
className="input input-bordered w-full bg-gray-700 text-white focus:outline-none focus:ring focus:ring-primary"
|
||||||
required
|
required
|
||||||
@@ -28,25 +59,40 @@ const LoginPage = () => {
|
|||||||
<span className="label-text text-gray-200">Password</span>
|
<span className="label-text text-gray-200">Password</span>
|
||||||
</label>
|
</label>
|
||||||
<input
|
<input
|
||||||
type="password"
|
type="password"
|
||||||
|
value={password}
|
||||||
|
onChange={(e) => setPassword(e.target.value)}
|
||||||
placeholder="Enter your password"
|
placeholder="Enter your password"
|
||||||
className="input input-bordered w-full bg-gray-700 text-white focus:outline-none focus:ring focus:ring-primary"
|
className="input input-bordered w-full bg-gray-700 text-white focus:outline-none focus:ring focus:ring-primary"
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Error Message */}
|
||||||
|
{mutation.isError && <InlineTextError mutation={mutation} />}
|
||||||
|
|
||||||
|
{/* Success Message */}
|
||||||
|
{mutation.isSuccess && (
|
||||||
|
<p className="text-green-500 text-sm md:text-base">
|
||||||
|
🎉 {mutation.data.message || "Login is successfull"}
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
|
||||||
{/* Forgot Password Link */}
|
{/* Forgot Password Link */}
|
||||||
<div className="text-right">
|
<div className="text-right">
|
||||||
<a href="#" className="text-sm text-primary hover:underline">Forgot password?</a>
|
<a href="#" className="text-sm text-primary hover:underline">Forgot password?</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
{/* Submit Button */}
|
{/* Submit Button */}
|
||||||
<div>
|
<div>
|
||||||
<button
|
<button
|
||||||
|
onClick={handleLogin}
|
||||||
type="submit"
|
type="submit"
|
||||||
className="btn btn-primary w-full text-white"
|
className="btn btn-primary w-full text-white"
|
||||||
>
|
>
|
||||||
Login
|
{mutation.isLoading ? <SpinnerLoader/> : "Login"}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
6
frontend/src/services/loginService.js
Normal file
6
frontend/src/services/loginService.js
Normal file
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user