Modify Signin.js to add error code from Firebase with invalid credentials

This commit is contained in:
Ray 2023-10-10 08:05:45 +00:00 committed by GitHub
parent 0524ba25bd
commit 58fc5fd900
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 99 additions and 108 deletions

View File

@ -82,13 +82,18 @@ function App() {
}) })
} }
const signIn = ( email, password ) => { const signIn = (email, password) => {
return new Promise((resolve, reject) => {
signInWithEmailAndPassword(FBauth, email, password) signInWithEmailAndPassword(FBauth, email, password)
.then( () => { .then(() => {
// user is signed in // user is signed in
} ) resolve(true)
.catch( (error) => { console.log(error) }) })
.catch((error) => {
console.log(error)
reject( error.code )
})
})
} }
return ( return (
<div className="App"> <div className="App">

View File

@ -1,92 +1,82 @@
import Form from 'react-bootstrap/Form'; 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'; 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 [errorCode, setErrorCode] = useState()
const navigate = useNavigate() const navigate = useNavigate()
useEffect( () => { useEffect( () => {
if(email.indexOf('@') > 0) { if( email.indexOf('@') > 0 ) {
setValidemail(true) setValidemail(true)
} }
else { else {
setValidemail( false ) setValidemail( false )
} }
}, [email]) } , [email])
useEffect( () => { useEffect( () => {
if( password.length >= 8 ) { if( password.length >= 8 ) {
setValidpassword(true) setValidpassword( true )
} }
else { else {
setValidpassword( false ) setValidpassword( false )
} }
}, [password]) } , [password])
useEffect( () => { useEffect( () => {
if( props.authstate ) { if( props.authstate ) {
navigate('/') navigate("/")
} }
}, [props.authstate]) }, [props.authstate])
const submitHandler = (evt) => { const submitHandler = (evt) => {
evt.preventDefault() evt.preventDefault()
props.handler( email, password ) props.handler( email, password )
.then((response) => {
if( response ) {
// sign in successful
}
})
.catch( (code) => {
//console.log(code)
setErrorCode( code )
})
} }
return ( return(
<Container> <Container>
<Row> <Row>
<Col md={ {span: 4, offset: 4} }> <Col md={ {span: 4, offset: 4} }>
<Form onSubmit={ submitHandler }> <Form onSubmit={ submitHandler }>
<Form.Group> <Form.Group>
<Form.Label>Email</Form.Label> <Form.Label>Email</Form.Label>
<Form.Control <Form.Control
type="email" type="email"
name="email" name="email"
placeholder="you@example.com" placeholder="you@example.com"
value={ email }
value={email} onChange={ (evt) => setEmail(evt.target.value) }
onChange={ (evt) => setEmail(evt.target.value)}
/> />
</Form.Group> </Form.Group>
<Form.Group> <Form.Group>
<Form.Label>Password</Form.Label> <Form.Label>Password</Form.Label>
<Form.Control <Form.Control
type="password" type="password"
name="password" name="password"
placeholder="your password"
placeholder="minimum 8 characters"
value={ password } value={ password }
onChange={ (evt) => setPassword(evt.target.value) }
onChange={ (evt) => setPassword(evt.target.value)}
/> />
</Form.Group> </Form.Group>
<Button <Button
variant="primary" variant="primary"
@ -94,16 +84,12 @@ export function Signin ( props ) {
type="submit" type="submit"
disabled={ (validemail && validpassword) ? false : true } disabled={ (validemail && validpassword) ? false : true }
> >
Sign in Sign in
</Button> </Button>
<Form.Text>{errorCode}</Form.Text>
</Form> </Form>
</Col> </Col>
</Row> </Row>
</Container> </Container>
) )
} }