diff --git a/frontend/src/public-components/Login.jsx b/frontend/src/public-components/Login.jsx index bae7014a..862fbf71 100644 --- a/frontend/src/public-components/Login.jsx +++ b/frontend/src/public-components/Login.jsx @@ -1,163 +1,205 @@ -import * as React from 'react'; -import Box from '@mui/material/Box'; -import TextField from '@mui/material/TextField'; -import AccountCircle from '@mui/icons-material/AccountCircle'; -import Lock from '@mui/icons-material/Lock'; -import { Button, Grid, FormControlLabel, Typography, Avatar, Paper, Checkbox, LoadingButton } from '@mui/material'; -import LockOutlinedIcon from '@mui/icons-material/LockOutlined'; -import { Link, useNavigate } from 'react-router-dom'; +import * as React from "react"; +import Box from "@mui/material/Box"; +import TextField from "@mui/material/TextField"; +import { + Button, + Grid, + FormControlLabel, + Typography, + Avatar, + Paper, + Checkbox, +} from "@mui/material"; +import LockOutlinedIcon from "@mui/icons-material/LockOutlined"; +import { Link, useNavigate } from "react-router-dom"; import { supabase } from "../supabase"; -import { useState} from 'react'; -import useGetAccountTypeByUUID from '../queries/Account Setup/useGetAccountTypeByUUID'; -import { useSelector } from 'react-redux'; +import { useState, useEffect } from "react"; +import useGetAccountTypeByUUID from "../queries/Account Setup/useGetAccountTypeByUUID"; function Copyright(props) { return ( - - {'Copyright © '} + + {"Copyright © "} RentConnect - {' '} + {" "} {new Date().getFullYear()} - {'.'} + {"."} ); } - - -function LogIn(){ - const [loading, setLoading] = useState(false) - const [email, setEmail] = useState(''); - const handleEmailChange = f => { - setEmail(f.target.value); - } - const [password, setPassword] = useState(''); - const handlePasswordChange = f => { - setPassword(f.target.value); - } +function LogIn() { + const [loading, setLoading] = useState(false); + const [email, setEmail] = useState(""); + const [rememberMe, setRememberMe] = useState(false); + const [password, setPassword] = useState(""); const [errorText, setErrorText] = useState(false); const navigate = useNavigate(); - const fetchAccountSetup = useGetAccountTypeByUUID(); + + useEffect(() => { + const savedEmail = localStorage.getItem("email"); + const savedPassword = localStorage.getItem("password"); + if (savedEmail && savedPassword) { + setEmail(savedEmail); + setPassword(savedPassword); + setRememberMe(true); + } + }, []); + + const handleRememberMeChange = (f) => setRememberMe(f.target.checked); + const handleEmailChange = (f) => setEmail(f.target.value); + const handlePasswordChange = (f) => setPassword(f.target.value); + const attemptLogIn = (e) => { - e.preventDefault() - supabase.auth.signInWithPassword({ + e.preventDefault(); + setLoading(true); + + supabase.auth + .signInWithPassword({ email: email, password: password, - }).then( async data=> { - if (!data.error){ - var account_type = await fetchAccountSetup(data.data.user.id) - if (account_type.data[0]){ - switch(account_type.data[0].account_type){ - case 'Property Manager': - navigate('/AccountSetUpPM'); - break; - case 'Renter': - navigate('/AccountSetUpR'); - break; - default: - console.log('invalid account type found'); - return; - } - } - else{ - navigate('/dashboard'); + }) + .then(async (data) => { + setLoading(false); + if (!data.error) { + const user = data.data.user; + var account_type = await fetchAccountSetup(data.data.user.id); + + if (rememberMe) { + localStorage.setItem("email", email); + localStorage.setItem("password", password); + localStorage.setItem("user", JSON.stringify(user)); + } else { + localStorage.removeItem("email"); + localStorage.removeItem("password"); + sessionStorage.setItem("user", JSON.stringify(user)); + } + + if (account_type.data[0]) { + switch (account_type.data[0].account_type) { + case "Property Manager": + navigate("/AccountSetUpPM"); + break; + case "Renter": + navigate("/AccountSetUpR"); + break; + default: + console.log("Invalid account type found"); + return; } + } else { + navigate("/dashboard"); + } + } else { + setErrorText(true); + setPassword(""); } - else{ - setErrorText(true); - setPassword(''); - } - }) - .catch(error => { + }) + .catch((error) => { + setLoading(false); setErrorText(true); - setPassword(''); + setPassword(""); console.log(error); - }) + }); }; + return ( - - + + + - - - - - - - Sign in - - - - + + + + + Sign in + + + + + - } - label="Remember me" - /> - - - - - Don't have an account? Sign Up - - - - - - - + } + label="Remember me" + /> + + + + + Don't have an account? Sign Up + + + + + + + ); } -export default LogIn; \ No newline at end of file +export default LogIn;