// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 import React, {useEffect, useState} from 'react'; import Avatar from '@mui/material/Avatar'; import Button from '@mui/material/Button'; import CssBaseline from '@mui/material/CssBaseline'; import TextField from '@mui/material/TextField'; import FormControlLabel from '@mui/material/FormControlLabel'; import Checkbox from '@mui/material/Checkbox'; import Link from '@mui/material/Link'; import Grid from '@mui/material/Grid'; import Box from '@mui/material/Box'; import LockOutlinedIcon from '@mui/icons-material/LockOutlined'; import Typography from '@mui/material/Typography'; import Container from '@mui/material/Container'; import { createTheme, ThemeProvider } from '@mui/material/styles'; import { useAuth } from '../commons/use-auth'; import {useNavigate} from 'react-router-dom'; import { useLocalStorage } from '../commons/localStorage'; function Copyright(props) { return ( {'Copyright © '} My Website {' '} {new Date().getFullYear()} {'.'} ); } const theme = createTheme(); const SignIn = () => { const auth = useAuth(); const [checked, setChecked] = useState(false); const [local_stored_crediential,setLocalStoredCred] = useLocalStorage('chat-login-token',null) const [errorstate, setErrorState] = useState(false); const [errormsg, setErrMsg] = useState(''); const [username, setUsername] = useState(); const [password, setPassword] = useState(); const navigate = useNavigate(); const isAuthenticated = auth.user && auth.user.isAuthorized; useEffect(()=>{ if(isAuthenticated){ navigate('/chat'); } },[navigate,isAuthenticated]); useEffect(()=>{ if (local_stored_crediential) { setChecked(local_stored_crediential.checked); if (local_stored_crediential.checked) { setUsername(local_stored_crediential.username); setPassword(local_stored_crediential.password); } } },[checked,local_stored_crediential]); const handleSubmit = (event) => { event.preventDefault(); const formdata = new FormData(event.currentTarget); auth.signin(formdata.get('username'),formdata.get('password')) .then((data)=>{ // console.log(data); setLocalStoredCred({username:formdata.get('username'), password:formdata.get('password'), checked:checked}); if (!(data?data.isAuthorized:false)){ setErrorState(true); setErrMsg(data.body.message); } }) .catch(error =>{ setErrorState(true); setErrMsg(error.message); }) }; return ( Sign in {/* */} { setUsername(event.target.value);}} autoFocus /> { setPassword(event.target.value);}} autoComplete="current-password" /> { setChecked(event.target.checked); setLocalStoredCred({checked:event.target.checked}); }} color="primary" />} label="Remember me" /> {/* Forgot password? {"Don't have an account? Sign Up"} */} {/* */} ); } export default SignIn;