Add sign-in redirect to home

This commit is contained in:
Ray 2023-09-19 10:32:12 +00:00 committed by GitHub
parent 29388ce96d
commit 75ab74df27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 5 deletions

View File

@ -18,6 +18,7 @@ import { Signout } from "./pages/Signout"
import { Signin } from "./pages/Signin"
function App() {
const FBapp = initializeApp(FirebaseConfig)
const FBauth = getAuth()
@ -42,6 +43,7 @@ function App() {
/// application states
const [nav, setNav] = useState(navItems)
// authentication observer
onAuthStateChanged(FBauth, (user) => {
if( user ) {
@ -93,7 +95,7 @@ const signIn = ( email, password ) => {
<Route path="/contact" element={<Contact greeting="Hey you, this is contact page!" />} />
<Route path="/signup" element={ <Signup handler={signUp}/> } />
<Route path="/signout" element={ <Signout handler={logOut}/> } />
<Route path="/signin" element={ <Signin handler={signIn}/> } />
<Route path="/signin" element={ <Signin handler={signIn} authstate={auth}/> } />
</Routes>
</div>
);

View File

@ -2,14 +2,16 @@ import Form from 'react-bootstrap/Form';
import Container from 'react-bootstrap/Container';
import Row from 'react-bootstrap/Row';
import Col from 'react-bootstrap/Col';
import Button from "react-bootstrap/Button"
import Button from "react-bootstrap/Button";
import {useState, useEffect} from 'react';
import {useNavigate} from 'react-router-dom';
export function Signin ( props ) {
const [email, setEmail] = useState('')
const [validemail, setValidemail] = useState(false)
const [password, setPassword] = useState('')
const [validpassword, setValidpassword] = useState(false)
const navigate = useNavigate()
useEffect( () => {
if(email.indexOf('@') > 0) {
@ -29,11 +31,22 @@ export function Signin ( props ) {
}
}, [password])
useEffect( () => {
if( props.authstate ) {
navigate('/')
}
}, [props.authstate])
const submitHandler = (evt) => {
evt.preventDefault()
props.handler( email, password )
}
return (
<Container>
<Row>
<Col md={ {span: 4, offset: 4} }>
<Form>
<Form onSubmit={ submitHandler }>
<Form.Group>
<Form.Label>Email</Form.Label>
@ -45,6 +58,9 @@ export function Signin ( props ) {
placeholder="you@example.com"
value={email}
onChange={ (evt) => setEmail(evt.target.value)}
/>
@ -63,6 +79,10 @@ export function Signin ( props ) {
placeholder="minimum 8 characters"
value={ password }
onChange={ (evt) => setPassword(evt.target.value)}
/>
@ -71,7 +91,10 @@ export function Signin ( props ) {
<Button
variant="primary"
className="mt-3 w-100"
type="submit">
type="submit"
disabled={ (validemail && validpassword) ? false : true }
>
Sign in
</Button>