javascript Firebase错误:Firebase:错误(授权/无效电子邮件)

lf5gs5x2  于 2023-04-04  发布在  Java
关注(0)|答案(3)|浏览(85)

我正在做一个React项目,当我试图通过电子邮件和密码使用Firebase身份验证注册时,显示此👇your text错误
This is the error I am getting

这是整个代码:

import React,{useState} from 'react';
import { Link, useNavigate } from 'react-router-dom';
import Layout from '../components/layout/Layout';
import {BsFillEyeFill} from "react-icons/bs";
import { getAuth, createUserWithEmailAndPassword , updateProfile} from "firebase/auth";
import {db} from "../firebase.config"

const SignUp = () => {

  //  to hide password
  const [showPassword, setShowPassword] = useState(false);

  const [formData, setFormData] = useState({
    email:"",
    name: "",
    password: "",
  })

  const {name, email, password} = formData;
  const navigate = useNavigate();

  const onChange = (e) => {
    setFormData((prevState) => ({
      ...prevState,
      [e.target.id]: e.target.value,
    }));
  };

这是我的注册功能

const onSubmitHandler= async (e) =>{
    e.preventDefault();
    try{
      const auth= getAuth()
      const userCredential = await createUserWithEmailAndPassword(auth,email,password);
      const user = userCredential.user
      updateProfile(auth.currentUser,{displayName: name})
     navigate("/")
     alert("Signup Success")
     }catch(error){
      console.log(error);
    }
  }

  return (
    <Layout>
       <div className="d-flex align-items-center justify-content-center w-100 mt-5 mb-5">
         <form className='bg-light p-4 rounded-3 border border-dark border-3' onSubmit={onSubmitHandler}>
            <h4 className='bg-dark p-2 mt-2 text-light rounded-2 text-center'>Sign Up</h4>
            <div className="mb-3">
               <label htmlFor="exampleInputEmail1" className="form-label">Enter Name</label>
               <input
                  type="text"
                  defaultValue={name}
                  id="name"
                  onChange={onChange} 
                  className="form-control"   
                  aria-describedby="nameHelp" 
                />
            </div>

            <div className="mb-3">
           <label htmlFor="exampleInputEmail1" className="form-label">Email address</label>
           <input
              type="email" 
              defaultValue={email}
              onChange={onchange}
              className="form-control"
              id="email" 
              aria-describedby="emailHelp" 
            />
           <div id="emailHelp" className="form-text">We'll never share your email with anyone else.</div>
        </div>

        <div className="mb-3">
               <label htmlFor="exampleInputPassword1" className="form-label">Password</label>
               <input
                  type={showPassword? "text" : "password"}
                  defaultValue={password}
                  onChange={onchange}
                  className="form-control" 
                  id="password"
                />
                <span >Show Password <BsFillEyeFill 
                 className='text-primary ms-2'
                 style={{cursor: "pointer"}}
                 onClick={()=> setShowPassword((prevState)=> !prevState)}
                /></span>
            </div>

            <button
              type="submit" 
              className="btn btn-primary">Sign Up</button>

              <div>
                <h6>Login with Google</h6>
                <span>Already User?</span> <Link to="/signin">Login</Link>
              </div>
         </form>

       </div>
    </Layout>
  )
}

export default SignUp

在浏览firebase文档后,我尝试了各种电子邮件,但仍然显示相同的错误。This is the email I had been using for signUp。我还尝试了不同的电子邮件,如:piyush@gmail.com,和我的个人电子邮件太,但它仍然显示相同的错误

jljoyd4f

jljoyd4f1#

这是您的错误的错误信息

auth/invalid-emailThe provided value for the email user property is invalid. It must be a string email address.

检查以确保您的onSubmitHandler在您的formData状态范围内,因为您的emailpassword可能未定义。

fnvucqvd

fnvucqvd2#

您的代码有2个问题,1:检查电子邮件和密码字段中onChange的拼写。2:将defaultValue更改为value
下面是工作代码:

import React, { useState } from 'react';
import { Link, useNavigate } from 'react-router-dom';
import { BsFillEyeFill } from 'react-icons/bs';
import {
  getAuth,
  createUserWithEmailAndPassword,
  updateProfile,
} from 'firebase/auth';

const Signup = () => {
  const [showPassword, setShowPassword] = useState(false);

  const [formData, setFormData] = useState({
    email: '',
    name: '',
    password: '',
  });

  const { name, email, password } = formData;
  const navigate = useNavigate();

  const onChange = (e: any) => {
    setFormData((prevState) => ({
      ...prevState,
      [e.target.id]: e.target.value,
    }));
  };

  const onSubmitHandler = async (e) => {
    e.preventDefault();
    console.log('email is ', email, password);
    try {
      const auth = getAuth();
      const userCredential = await createUserWithEmailAndPassword(
        auth,
        email,
        password,
      );
      const user = userCredential.user;
      alert('Signup Success');
    } catch (error) {
      console.log(error);
    }
  };

  return (
    <div className="d-flex align-items-center justify-content-center w-100 mt-5 mb-5">
      <form
        className="bg-light p-4 rounded-3 border border-dark border-3"
        onSubmit={onSubmitHandler}
      >
        <h4 className="bg-dark p-2 mt-2 text-light rounded-2 text-center">
          Sign Up
        </h4>
        <div className="mb-3">
          <label htmlFor="exampleInputEmail1" className="form-label">
            Enter Name
          </label>
          <input
            type="text"
            value={name}
            id="name"
            onChange={onChange}
            className="form-control"
            aria-describedby="nameHelp"
          />
        </div>

        <div className="mb-3">
          <label htmlFor="exampleInputEmail1" className="form-label">
            Email address
          </label>
          <input
            type="email"
            value={email}
            onChange={onChange}
            className="form-control"
            id="email"
            aria-describedby="emailHelp"
          />
          <div id="emailHelp" className="form-text">
            We'll never share your email with anyone else.
          </div>
        </div>

        <div className="mb-3">
          <label htmlFor="exampleInputPassword1" className="form-label">
            Password
          </label>
          <input
            type={showPassword ? 'text' : 'password'}
            value={password}
            onChange={onChange}
            className="form-control"
            id="password"
          />
          <span>
            Show Password{' '}
            <BsFillEyeFill
              className="text-primary ms-2"
              style={{ cursor: 'pointer' }}
              onClick={() => setShowPassword((prevState) => !prevState)}
            />
          </span>
        </div>

        <button type="submit" className="btn btn-primary">
          Sign Up
        </button>

        <div>
          <h6>Login with Google</h6>
          <span>Already User?</span> <Link to="/signin">Login</Link>
        </div>
      </form>
    </div>
  );
};

export default Signup;
mzillmmw

mzillmmw3#

const auth = getAuth();

我认为这行代码是错误的。你想用getAuth进行身份验证,但身份验证是什么?
必须将app传递给getAuth(app)

相关问题