React + Firebase 身份验证:在不更改当前登录用户的情况下创建新用户

时间:2021-01-21 05:19:25

标签: reactjs firebase redux firebase-authentication

我的 ReactJS 应用中有一个管理区域,我需要允许管理员在不更改当前登录用户的情况下创建用户。

这个问题已经有人问过了,这里有答案:

Firebase Auth state changes after creating new user

但是上面不是为ReactJS量身定做的,我也不太明白。我也没有使用 Firestore 数据库,所以我不确定上面的答案是否适合我。

这是我用于正常注册的当前功能:

import firebase from "../../firebase"; //a file where we have firebase initialization with the SDK


// Signing up with Firebase
export const signup = (email, password) => async dispatch => {
  try {
    dispatch(beginApiCall());
    firebase
      .auth()
      .createUserWithEmailAndPassword(email, password)
      .then(dataBeforeEmail => {
        console.log('checking for state')
        firebase.auth().onAuthStateChanged(function(user) {
          console.log('sending email verification')
          user.sendEmailVerification().then(function(){
                   console.log("email successfully sent");
                 });
        });
      })
      .then(dataAfterEmail => {
        firebase.auth().onAuthStateChanged(function(user) {
          if (user) {
            // Sign up successful
            dispatch({
              type: SIGNUP_SUCCESS,
              payload:
                "Your account was successfully created! Now you need to verify your e-mail address, please go check your inbox."
            });
          } else {
            // Signup failed
            dispatch({
              type: SIGNUP_ERROR,
              payload:
                "Something went wrong, we couldn't create your account. Please try again."
            });
          }
        });
      })
      .catch(() => {
        dispatch(apiCallError());
        dispatch({
          type: SIGNUP_ERROR,
          payload:
            "Something went wrong, we couldn't create your account. Please try again."
        });
      });
  } catch (err) {
    dispatch(apiCallError());
    dispatch({
      type: SIGNUP_ERROR,
      payload:
        "Something went wrong, we couldn't create your account. Please try again."
    });
  }
};

我不知道如何将上述功能“转换”为无需登录即可创建用户的功能(我知道我不会使用 .createUserWithEmailAndPassword(),但这是我所能做的).

非常感谢任何帮助。

0 个答案:

没有答案