我正在开发一个应用程序,在那里我需要注册一个用户,包括他们的用户名等信息。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。如果有人可以的话,我需要帮助。

发布于 2019-11-07 03:59:40
在代码调用functions.auth.user().onCreate(之后立即调用firebase.auth().createUserWithEmailAndPassword(this.state.email, this.state.password)触发器。因此,云函数代码中只有最小的属性,而不是显示名称。
如果还想写入显示名称,则有两个主要选项:
latter).
这两个选项都很好,您选择哪一个取决于应用程序的要求。
https://stackoverflow.com/questions/58741087
复制相似问题