javascript - 火力地堡错误 : Firebase: Error (auth/user-token-expired)

标签 javascript reactjs firebase firebase-authentication

<分区>

我在使用电话号码身份验证创建用户后创建了一个新表单(在 firebase 中),但是在提交 FirebaseError 后错误不断出现:Firebase:错误(auth/user-token-expired) 此代码中出现错误

//This Component is used to store the Name ,Phone Number Of new User  Which have Registered in SignUp With Number
import "./Auth.scss";
import React, { useState } from "react";
import { updateProfile, updateEmail } from "firebase/auth";
import { auth } from "../../firebase/config";
import { useNavigate, useLocation } from "react-router";
import usePhoneSignUp from "../../hooks/usePhoneSignUp";
import { update } from "lodash";

const SaveUserDetails = () => {
  //code to extract userType after navigating from SignUpWithNumber page
  const { state } = useLocation();
  const userType = state.userType;
  console.log(userType);

  // .......
  const {
    signUp,
    error: signupError,
    isPending: signupIsPending,
  } = usePhoneSignUp();

  const [name, setname] = useState();
  const [email, setemail] = useState();
  const navigate = useNavigate();
  const handleChange = (e) => {
    e.preventDefault();
    const { name, value } = e.target;
    switch (name) {
      case "displayname":
        setname(value);
        break;
      case "email":
        setemail(value);
        break;
      default:
        break;
    }
  };
  const updateEmailUser = () => {
    updateEmail(auth.currentUser, email)
      .then(() => {
        // Email updated!
        // ...
        console.log("email Updated");
      })
      .catch((error) => {
        // An error occurred
        console.log("email Updated");

        // ...
        console.log(error);
      });
  };
  const updateUserProfile = () => {
    updateProfile(auth.currentUser, {
      displayName: name,
    })
      .then(() => {
        console.log("profile Updated" + name + " " + email);
      })
      .catch((error) => {
        console.log(error + "In update profile");
      });
    updateEmailUser();
  };
  const handleSubmit = () => {
    // updateEmailUser();
    updateUserProfile();
    signUp(name, userType, email);
    let path =
      userType === "salonOwner" ? "/addBuisnessDetails" : "/salonsNearby";
    if (signupError) {
      console.log(signupError.message);
    }
    return navigate(path, { replace: true });
  };

  //query function for saloon

  return (
    <>
      <div className="form-wrapper ">
        <div id="register-form">
          <p className="login-title register-title">Complete Your Profile</p>
          <div className="login-hr" />

          <form onSubmit={handleSubmit}>
            <div className="form-group login-sj">
              <label htmlFor="exampleInputName1">Name:</label>
              <input
                type="text"
                className="form-control"
                id="exampleInputName1"
                aria-describedby="emailHelp"
                placeholder="Your Name"
                name="displayname"
                onChange={handleChange}
              />
            </div>
            <div className="form-group login-sj">
              <label htmlFor="exampleInputEmail2">Email address</label>
              <input
                type="email"
                className="form-control"
                id="exampleInputEmail2"
                aria-describedby="emailHelp"
                placeholder="Enter email"
                name="email"
                onChange={handleChange}
              />
            </div>
            {/* <div className="form-group login-sj">
            <label htmlFor="exampleInputPassword1"></label>
            <input
              type="password"
              className="form-control"
              id="exampleInputPassword2"
              placeholder="Password"
              value={userPassword}
              onChange={(e) => setUserPassword(e.target.value)}
            />
          </div> */}
            {signupIsPending ? (
              <>
                <button
                  type="submit"
                  className="btn-auth-sj btn btn-primary"
                  disabled
                >
                  Save Details
                </button>
              </>
            ) : (
              <>
                <button type="submit" className="btn-auth-sj btn btn-primary">
                  Save Details
                </button>
              </>
            )}
          </form>
        </div>
      </div>
    </>
  );
};

export default SaveUserDetails;

Comming错误的部分

.catch((error) => {
        console.log(error + "In update profile");
      });

由于这个原因,我的 displayName 没有被保存,并且在提交用户后自动注销。 我之前也问过这个问题并按照回答 Is their any function signupwithphonenumber in firebase just like signupwithemailandpassword? (for web) I want to make user register with his creds 实现了它 提前致谢

最佳答案

好吧,通过将 updateProfile 函数(firebase 提供的函数)包装到

auth.currentUser.reload().then(() => { /* update profile function here */ })

或者在我的例子中:-

 const updateUserProfile = () => {
    auth.currentUser.reload().then(() => {
      updateProfile(auth.currentUser, {
        displayName: name,
      })
        .then(() => {
          console.log("profile Updated" + name + " " + email);
        })
        .catch((error) => {
          console.log(error + "In update profile");
        });
      updateEmailUser();
    });
  };

关于javascript - 火力地堡错误 : Firebase: Error (auth/user-token-expired),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72754330/

相关文章:

javascript - 光滑的 slider 点重新定位 - 多个位置

javascript - 完成后在数组开头添加项目

android - 添加 react-native-firebase crashlytics 后,Android 应用程序在第二个构建中崩溃,MainApplication 出现 classNotFound 异常

javascript - 可以使用带有 textarea 行值的 CSS 转换吗?

firebase - 如何作为 Kotlin 中函数的结果从 Firestore 数据库返回列表?

Firebase:react-native:从 '@react-native-firebase/iid' 获取实例 id、iid 不起作用

javascript - 解析错误消息 "react-dom.development.js:4091 Uncaught TypeError: onItemSelect is not a function"

javascript - 根据 slider 中的不同子项计算正确的 div 高度?

javascript - 在 React JS 中设置复选框值

reactjs - Express cors 不允许凭据