From 77beb0b8d24ef176ca429d48edfd90619cbe6c76 Mon Sep 17 00:00:00 2001 From: Tkalex32 <59170674+Tkalex32@users.noreply.github.com> Date: Sat, 22 Apr 2023 12:44:21 +0200 Subject: [PATCH] adds show/hide password button --- frontend/src/Doctorlogin/LoginForm.js | 218 +++++++++++++++----------- 1 file changed, 127 insertions(+), 91 deletions(-) diff --git a/frontend/src/Doctorlogin/LoginForm.js b/frontend/src/Doctorlogin/LoginForm.js index 660daec..4990731 100644 --- a/frontend/src/Doctorlogin/LoginForm.js +++ b/frontend/src/Doctorlogin/LoginForm.js @@ -1,100 +1,136 @@ -import React, { useContext, useState } from 'react'; +import React, { useContext, useState } from "react"; import { Redirect, useHistory } from "react-router-dom"; -import { Container, Row, Col, Card, Form, CardHeader, CardBody, FormGroup, CardFooter, Button, Label, Input } from 'reactstrap' -import axios from 'axios'; -import { AuthContext } from '../Auth/AuthContext'; +import { + Container, + Row, + Col, + Card, + Form, + CardHeader, + CardBody, + FormGroup, + CardFooter, + Button, + Label, + Input, + InputGroup, +} from "reactstrap"; +import { AiOutlineEye, AiOutlineEyeInvisible } from "react-icons/ai"; +import axios from "axios"; +import { AuthContext } from "../Auth/AuthContext"; const LoginForm = () => { - const [username, setUsername] = useState(''); - const [password, setPassword] = useState(''); - const [status, setStatus] = useState(0); - const { token, setToken, googleId, setGoogleId } = useContext(AuthContext); - const history = useHistory(); + const [username, setUsername] = useState(""); + const [password, setPassword] = useState(""); + const [status, setStatus] = useState(0); + const [passwordShown, setPasswordShown] = useState(false); + const { token, setToken, googleId, setGoogleId } = useContext(AuthContext); + const history = useHistory(); - async function login() { - try { - const res = await axios.post( - `${process.env.REACT_APP_SERVER_URL}/doctors/login/`, - { - username: username, - password: password - } - ); - setStatus(res.status); + const togglePassword = () => { + setPasswordShown(!passwordShown); + }; - const token = res.data.token; + async function login() { + try { + const res = await axios.post( + `${process.env.REACT_APP_SERVER_URL}/doctors/login/`, + { + username: username, + password: password, + } + ); + setStatus(res.status); - if (res.status === 200) { - window.localStorage.setItem("token", token); + const token = res.data.token; - // Remove the googleId if it exisits in the local storage - window.localStorage.removeItem("googleId"); - setGoogleId(null); - setToken(token); - history.push('/doctor'); - } - } catch (err) { - console.log(err); - } - } + if (res.status === 200) { + window.localStorage.setItem("token", token); - if (token && !googleId) { - return - } - return ( - - - - -
- Welcome back, Doc - - - - - setUsername(e.target.value)} - /> - - - - - - setPassword(e.target.value)} - onKeyPress={(target) => { - if (target.charCode === 13) { - login(); - } - } } - /> - - - {status === 201 &&

Wrong username or password! Please try again

} -
- - - -
-
- -
-
- ); -} + // Remove the googleId if it exisits in the local storage + window.localStorage.removeItem("googleId"); + setGoogleId(null); + setToken(token); + history.push("/doctor"); + } + } catch (err) { + console.log(err); + } + } -export default LoginForm; \ No newline at end of file + if (token && !googleId) { + return ; + } + return ( + + + + +
+ Welcome back, Doc + + + + + setUsername(e.target.value)} + /> + + + + + + + setPassword(e.target.value)} + onKeyPress={(target) => { + if (target.charCode === 13) { + login(); + } + }} + /> + + + + + {status === 201 && ( +

+ Wrong username or password! Please try again +

+ )} +
+ + + +
+
+ +
+
+ ); +}; + +export default LoginForm;