skip to Main Content

I tried to create authentication on react, but now I am currently stuck as my try and catch block is not working. When I click the signup button, I am not getting any error nor any response from the site. No user is uploaded to the Firebase database.
The Code is Given Below.

import React,{useRef,useState} from 'react'
import {Form,Button,Card,Alert} from 'react-bootstrap'
import {useAuth} from '../Context/AuthContext'

function Signup() {
    const emailRef=useRef()
    const passwordRef=useRef()
    const passwordConfirmRef=useRef( )
    const {signup} =useAuth();
    const [error,setError]=useState();
    const [loading,setLoading]=useState(false); 

   async function handleSubmit(e){
        e.preventDefault()
        if(passwordRef.current.value!==passwordConfirmRef.current.value){
            return setError("Passwords Do Not Match")
        }
        try{
            setError("");
            setLoading(true);
           await signup(emailRef.current.value,passwordRef.current.value)
        }
        catch {setError("Failed To Create An Account")}
        setLoading(false);
    }
}
 
export default Signup

2

Answers


  1. Chosen as BEST ANSWER

    The Submit Button Type Was Not Mentioned In The Above Code. So It Was Not Submitting And Hence No Error Were Shown In The Console. So Just Add type='submit' To The Submit Button And The Code Will Work Properly.


  2. Try catch block looks like this:

    try {
       ...
    } catch(e) {
      console.log(e.message)
    }
    

    Are you sure you paste correct code ? Signup() don’t have closing brackets. I don’t see in your code that you’re importing signup() function. And your main function is named Signup() this is not a good practice. A good name for your function can be onSignUp() instead of Signup().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search