diff --git a/webapp/src/context/AuthContext.js b/webapp/src/context/AuthContext.js index e78b26f..1853ff0 100644 --- a/webapp/src/context/AuthContext.js +++ b/webapp/src/context/AuthContext.js @@ -10,7 +10,7 @@ export const useAuthContext = () => useContext(AuthContext) //create provider export const AuthProvider = ({children})=>{ //Todo - const [auth,setAuth] = useState(null) + const [auth,setAuth] = useState("") return ( diff --git a/webapp/src/views/Login.js b/webapp/src/views/Login.js index 4a0f674..74fc63d 100644 --- a/webapp/src/views/Login.js +++ b/webapp/src/views/Login.js @@ -3,6 +3,8 @@ import FloatingLabel from 'react-bootstrap/FloatingLabel'; import Form from 'react-bootstrap/Form'; import '../assets/CSS/signUp.css' import Button from 'react-bootstrap/Button'; +import Spinner from 'react-bootstrap/Spinner'; +import Alert from 'react-bootstrap/Alert'; import { Link, useNavigate } from "react-router-dom"; import { useAuthContext } from "../context/AuthContext"; import axios from 'axios'; @@ -17,26 +19,47 @@ export default function Login() { const [email,setEmail] = useState("") const [password,setPassword] = useState("") + const [validated, setValidated] = useState(false) + const [loading, setLoading] = useState(false) + const [feedBack, setFeedback] = useState() + + useEffect(()=>{ if(getSessionCookie("token")){ navigate("/dashboard", {replace:true}) - } - }) + } + },[]) + const handleSubmit = (e)=>{ - e.preventDefault() + + setLoading(true) + + e.preventDefault(); + const form = e.currentTarget; + if (form.checkValidity() === false) { + e.preventDefault(); + e.stopPropagation(); + setValidated(true) + setLoading(false) + return + } + + setValidated(true) axios.defaults.withCredentials = true axios.post('http://localhost:5000/api/auth/login', {email:email, password:password}) .then(response => { // Handle the successful response here - - console.log(jwt_decode(getSessionCookie("token"))) - // setAuth(true) + + setEmail("") + setPassword("") + + if(jwt_decode(getSessionCookie("token")).isVerified){ navigate("/dashboard",{replace:true}) @@ -51,10 +74,15 @@ export default function Login() { }) .catch(error => { // Handle any errors that occur during the request - console.log('Login failed!') + if(error.response){ + setFeedback( error.response.data.error) + }else{ + setFeedback("Network Error!") + } + + }).finally(()=>{ - setEmail("") - setPassword("") + setLoading(false) }) @@ -70,7 +98,7 @@ export default function Login() {

LOGIN

-
+ - setEmail(e.target.value)} value={email} type="email" size="sm" placeholder="name@example.com" /> + setEmail(e.target.value)} value={email} type="email" size="sm" placeholder="name@example.com" required /> + Please put a valid email! - setPassword(e.target.value)} value={password} type="password" placeholder="Password" /> + setPassword(e.target.value)} value={password} type="password" placeholder="Password" /> + Password cann't be empty! @@ -97,8 +127,12 @@ export default function Login() {
+ {feedBack && setFeedback("") } variant="danger" className=" text-center" dismissible >{feedBack} } +
- + {/*

OR

*/}

OR

@@ -115,14 +149,14 @@ export default function Login() { */} - + */}