简体   繁体   English

尝试让 Firebase 电子邮件/密码身份验证与 React Native 一起使用

[英]Trying to get the Firebase Email/Password Authentication to work with React Native

So I am trying to get the Firebase Email & Password Authentication working with React Native.所以我试图让 Firebase 电子邮件和密码身份验证与 React Native 一起使用。

Manually I achieved to signup a user by inserting我手动实现了通过插入来注册用户

auth.createUserWithEmailAndPassword('name@email.com','password')

Then the connection works.然后连接工作。

But I have a hard time getting the right format from {this.state.email} & {this.state.password} into the function.但是我很难从 {this.state.email} 和 {this.state.password} 获取正确的格式到函数中。 I mostly get: Need 2 arguments and only got 1 or no error at all.我主要得到:需要 2 个参数,但只有 1 个或根本没有错误。

Maybe the code setup for the input field is wrong.也许输入字段的代码设置是错误的。 Most other examples on the web use the old examples网络上的大多数其他示例都使用旧示例

Anybody who has experience with this and can help here?任何有这方面经验并可以在这里提供帮助的人? Thanks in advance.提前致谢。

Extra info: This leads to a syntax error额外信息:这会导致语法错误

auth.createUserWithEmailAndPassword(
          {this.state.email},
          {this.state.password}

        )

Code:代码:

'use strict';
    import React, { Component } from 'react';
    import {
      Text,
      TextInput,
      View
    } from 'react-native';

import Button from '../components/Button';
import StatusBar from '../components/StatusBar';
import Login from './Login';
import styles from '../styles.js';

var firebaseConfig = {
    apiKey: "##",
    authDomain: "##",
    databaseURL: "##",
    storageBucket: "##",
};
const firebaseApp = firebase.initializeApp(firebaseConfig, 'AppB');
firebase.database.enableLogging(true);
const auth = firebaseApp.auth();

class Signup extends Component {

  constructor(props){
    super(props);
    this.state = {
      loaded: true,
      email: '',
      password: ''
    };
  }

  signup(email, password){
    this.setState({
      loaded: false,
    });

    auth.createUserWithEmailAndPassword(
      '{this.state.email}',
      '{this.state.password}'

    ).then((data) => {
            if (this.props.onSignupSuccess) {
                this.props.onSignupSuccess(data)
            }
        })
        .catch((error) => {
            var errorCode = error.code;
            var errorMessage = error.message;

            if (this.props.onLoginError) {
                this.props.onLoginError(error.code, error.message)
            }
        });
      this.setState({
        email: '',
        password: '',
        loaded: true
      });
  }

  goToLogin(){
    this.props.navigator.push({
      component: Login
    });
  }

  render() {
    return (
      <View style={styles.container}>
        <StatusBar title="Signup" loaded={this.state.loaded} />
        <View style={styles.body}>

            <TextInput
                style={styles.textinput}
                onChangeText={(text) => this.setState({email: text})}
                value={this.state.email}
            placeholder={"Email Address"}
            />
          <TextInput
            style={styles.textinput}
            onChangeText={(text) => this.setState({password: text})}
            value={this.state.password}
            secureTextEntry={true}
            placeholder={"Password"}
          />
          <Button
            text="Signup"
            onpress={this.signup.bind(this)}
            button_styles={styles.primary_button}
            button_text_styles={styles.primary_button_text} />

          <Button
            text="Got an Account?"
            onpress={this.goToLogin.bind(this)}
            button_styles={styles.transparent_button}
            button_text_styles={styles.transparent_button_text} />
        </View>
      </View>
    );
  }
}

module.exports = Signup;

Here you go...干得好...

 import React, { Component } from 'react'; import { View, Text, TextInput, TouchableOpacity } from 'react-native'; import { Button } from '../common/button'; import styles from '../../styles'; import { firebaseApp } from './authentication'; export class signIn extends Component { constructor(props) { super(props); this.state = { email: '', //props.email password: '', //props.password toast: '', authUser: {} }; } componentDidMount() { firebaseApp.auth().onAuthStateChanged(firebaseUser => { if (firebaseUser) { this.setState({ authUser: firebaseUser }); this.setState({ toast: `Logged in as ${this.state.authUser.email}` }); console.log(this.state.authUser); this.props.navigator.push({ screen: 'home' }); } else { this.setState({ toast: 'Logged Out' }); } }); } render() { return ( < View style = { styles.container } > < Text > Sign In < /Text> <Text style={styles.label}>Username:</Text > < TextInput style = { styles.input } value = { this.state.email } onChangeText = { (text) => this.setState({ email: text }) } placeholder = "Email" / > < Text style = { styles.label } > Password: < /Text> <TextInput style={styles.input} secureTextEntry={true} value={this.state.password} onChangeText={(text) => this.setState({password: text})} placeholder="Password" / > < Button text = { 'Sign In' } onPress = { () => this.signIn() } /> <View style={styles.links}> <TouchableOpacity> <Text style={[styles.link,styles.linkPadding]}> Forgot Password? </Text > < /TouchableOpacity> <TouchableOpacity onPress={() => this.props.navigator.push({ screen: 'signUp' })}> <Text style={styles.link}> Sign Up </Text > < /TouchableOpacity> </View > < Text style = { styles.error } > { this.state.toast } < /Text> </View > ) } signIn() { let { email, password } = this.state; firebaseApp.auth().signInWithEmailAndPassword(email, password) .catch(error => { this.setState({ toast: error.message }); }); } }

And your authentication.js file/component should look like this:您的 authentication.js 文件/组件应如下所示:

 import * as firebase from 'firebase'; const config = { apiKey: "", authDomain: "", databaseURL: "", storageBucket: "", }; export const firebaseApp = firebase.initializeApp(config);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM