首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当使用云函数时,Firebase身份验证不提供displayName。

当使用云函数时,Firebase身份验证不提供displayName。
EN

Stack Overflow用户
提问于 2019-11-07 02:22:38
回答 1查看 176关注 0票数 0

我正在开发一个应用程序,在那里我需要注册一个用户,包括他们的用户名等信息。Firebase身份验证工作良好,防火墙云功能也在一定程度上发挥作用。现在,我想检索user.uid,以便在他们注册时添加更多的信息,但我想不出怎么做。

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

代码语言:javascript
复制
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;
代码语言:javascript
复制
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。如果有人可以的话,我需要帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-07 03:59:40

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

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

latter).

  • Create

  • 将显示名称从应用程序代码中写入数据库,并在用户配置文件上设置它(假设您正在为创建一个单独的(HTTPS触发的或可调用的)云函数,将显示名称传递给它,然后将其设置在配置文件和数据库中。

这两个选项都很好,您选择哪一个取决于应用程序的要求。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58741087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档