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

View File

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