管理员使用Reactjs和Firebase创建用户和身份验证

时间:2018-10-24 05:18:33

标签: reactjs firebase firebase-authentication google-cloud-firestore firebase-admin

我是Reactjs的初学者。我为管理员创建了一个页面来创建用户,用户信息存储在Firestore中,我需要在身份验证中对该用户进行身份验证。在身份验证部分中,我使用了'admin.auth()createUser'函数,但我不知道该身份验证函数是true还是false。当我删除此身份验证功能并仅使用它来保存用户信息时,它就可以工作。但是我只想保存用户信息并对该用户进行身份验证。

这是我的代码

此userActions.js

import admin from "./fbAdmin";   
 export const createUser = (user) => {
    return(dispatch, getState, { getFirebase, getFirestore }) => {
        const firestore = getFirestore();

        admin.auth().createUser(
            user.email,
            user.password
        ).then((resp) => {
            return firestore.collection('users').doc(resp.user.uid).set({
                ...user,

            })
        }).then(() => {
            dispatch({ type: "CREATE_USER", user });
        }).catch(err => {
            dispatch({ type: 'CREATE_USER_ERROR', err });
        });
    }
}

此fbAdmin.js

var admin = require('firebase-admin');
var config = { ...}
admin.initializeApp(config);
export default admin;

然后createUser.js

import React, { Component } from 'react'
import { createUser } from './../../store/actions/userActions';
import { connect } from 'react-redux';
import { Redirect } from 'react-router-dom'
class CreateUser extends Component {
    state = {
        email: "",
        password: "",
        userFirstName: "",
        userLastName: "",
        channel: "",
    }
    handleChange = (e) => {
        this.setState({
        [e.target.id]: e.target.value
        })
    }
    handleSubmit = (e) => {
        e.preventDefault();
        this.props.createUser(this.state);
        this.props.history.push('/');
    }
    render() {
      const { auth } = this.props;
      if(!auth.uid) return <Redirect to='/signin' />
  return (
    form....
  )
}
}

const mapStateToProps = (state) => {
  return {
    auth: state.firebase.auth
  }
}

const mapDispatchToProps = (dispatch) => {
  return {
    createUser: (user) => dispatch(createUser(user))
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(CreateUser);

0 个答案:

没有答案