使用云功能时,Firebase身份验证未释放displayName

时间:2019-11-07 02:22:38

标签: firebase react-native firebase-realtime-database firebase-authentication google-cloud-functions

我正在开发一个应用程序,该应用程序需要使用用户名等信息来注册用户。 Firebase身份验证工作正常,并且Firebase云功能在一定程度上也能正常工作。现在,我想检索user.uid,以便在他们注册时添加一些更多信息,而我无法弄清楚该怎么做。

到目前为止,我只能将user.uid和电子邮件放入Firebase数据库中,但我需要访问该uid才能向同一用户添加更多信息,例如姓名,地址,电话号码等。

Filename: Signup.js

import React from 'react';
import {
  View,
  Text,
  StyleSheet,
  TextInput,
  ActivityIndicator,
  ScrollView
} from 'react-native';

import colors from '../constants/colors';
import CustomActionButton from '../components/CustomButton';
import * as firebase from 'firebase';
import { Ionicons } from "@expo/vector-icons";

class SignUp extends React.Component {
  constructor() {
    super();
    this.state = {
      displayName: '',
      email: '',
      password: '',
      isLoading: false
    };
  }

  onSignUp = async () => {
    if (this.state.email && this.state.password) {
      this.setState({ isLoading: true });
      try {
        const response = await firebase
          .auth()
          .createUserWithEmailAndPassword(
            this.state.email,
            this.state.password)
        if (response) {
          this.setState({ isLoading: false });
          alert('SignUp Succesfull. Now you can go back and Login.');
        }
      } catch (error) {
        this.setState({ isLoading: false });
        if (error.code == 'auth/email-already-in-use') {
          alert('User already exists.Try loggin in');
        }
        console.log(error);
      }
    } else {
      alert('Please enter email and password');
    }
  };

  render() {
    return (
      <View style={styles.container}>
          <ScrollView>
        {this.state.isLoading ? (
          <View
            style={[
              StyleSheet.absoluteFill,
              {
                alignItems: 'center',
                justifyContent: 'center',
                zIndex: 1000,
                elevation: 1000
              }
            ]}
          >
            <ActivityIndicator size="large" color={colors.logoColor} />
          </View>
        ) : null}
        <View style={{ flex: 1, justifyContent: 'center' }}>
        <View
          style={{ flex: 1, alignItems: "center", justifyContent: "center" }}
        >
          <Ionicons name="ios-pulse" size={150} color={colors.logoColor} />
          <Text style={{ fontSize: 30, marginBottom: 20, color: colors.logoColor }}>Mind Coach</Text>
        </View>
        <TextInput
            style={styles.textInput}
            placeholder={'John Doe'}
            placeholderTextColor={colors.bgTextInputDark}
            keyboardType="default"
            onChangeText={displayName => this.setState({ displayName })}
          />
          <TextInput
            style={styles.textInput}
            placeholder={'abc@example.com'}
            placeholderTextColor={colors.bgTextInputDark}
            keyboardType="email-address"
            onChangeText={email => this.setState({ email })}
          />
          <TextInput
            style={styles.textInput}
            placeholder="enter password"
            placeholderTextColor={colors.bgTextInputDark}
            secureTextEntry
            onChangeText={password => this.setState({ password })}
          />
          <View style={{ alignItems: 'center' }}>
            <CustomActionButton
              onPress={this.onSignUp}
              style={[ styles.loginButtons, { borderColor: colors.bgError, marginBottom: 30, }]}
            >
              <Text style={{ color: 'white' }}>Sign Up</Text>
            </CustomActionButton>
          </View>
        </View>
        </ScrollView>
      </View>
    );
  }
}
export default SignUp;
Filename: index.js (firebase-cloud-functions)

const functions = require('firebase-functions');

const admin = require('firebase-admin');

admin.initializeApp();

exports.createUserInDatabase = functions.auth.user().onCreate(async user => {
  const email = user.email
  const displayName = user.displayName;


  try {
    const snapshot = await admin
      .database()
      .ref('users/' + user.uid)
      .set({ email: email, displayName: displayName, uid: user.uid });
    return snapshot;
  } catch (error) {
    console.log(error);
    return error;
  }
});

如您所见,电子邮件和uid正在数据库中显示,而不是displayName。如果有人可以,我需要帮助。 Database

1 个答案:

答案 0 :(得分:0)

在您的代码调用functions.auth.user().onCreate(之后立即调用firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)触发器。因此,您的Cloud Functions代码中只有最小属性而不是显示名称。

如果您还想输入显示名称,则有两个主要选项:

  1. 将显示名称从应用程序代码写到数据库中,紧接着还要在用户配置文件上设置它(假设您正在使用后者)。
  2. 创建一个单独的(通过HTTPS触发或可调用)云功能,将该显示名称传递给它,然后在配置文件和数据库中对其进行设置。

两个选项都很好,根据您的应用要求选择哪个。