使用云函数时,Firebase身份验证未显示displayName
我正在开发一个应用程序,在这个应用程序中,我需要向用户注册用户名等信息。Firebase身份验证工作正常,Firebase云功能也在一定程度上正常工作。现在,我想检索user.uid,以便在他们注册时添加一些我不知道如何添加的更多信息 到目前为止,我只能将user.uid和电子邮件放入firebase数据库,但我需要访问该uid以向同一用户添加更多信息,如姓名、地址、电话号码等使用云函数时,Firebase身份验证未显示displayName,firebase,react-native,firebase-realtime-database,firebase-authentication,google-cloud-functions,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 {
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;
正如您所看到的,电子邮件和uid显示在数据库中,而不是displayName。如果有人可以的话,我需要帮助。
在您的代码调用
firebase.auth().createUserWithEmailAndPassword(this.state.email,this.state.password)
之后,将立即调用functions.auth.user().onCreate(
触发器)。因此,在您的云函数代码中只有最小的属性,而不是显示名称可用
如果还希望写入显示名称,则有两个主要选项:
这两个选项都很好,您选择哪一个取决于应用程序的要求。我觉得我知道该做什么,但不知道该放在哪里。(()=>{firebase.auth().currentUser.updateProfile({displayName:displayName})我的猜测是,在用户创建之后,您就可以这样做了,离现在的
if(response){
非常近。但最终这是您的电话,因为您可以在许多地方放置它。
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;
}
});