输入密码中的空值react-native redux firebase

时间:2018-01-12 04:43:08

标签: firebase react-native redux react-redux redux-thunk

我只是用redux学习RN。我有与RN,redux,redux-thunk,firebase一起使用的身份验证。当我只使用RN和firebase的auth并尝试登录时,它可以工作。但是当我使用RN和redux时,我得到了这个错误:

  

signInWithEmailAndPassword失败:预期2个参数但得到1

我的日志:enter image description here

App.js:

import React, { Component } from 'react';
import { Provider } from 'react-redux';
import { createStore, applyMiddleware } from 'redux';
import reducers from './src/reducers/';
import firebase from 'firebase';
import ReduxThunk from 'redux-thunk';

import LoginForm from './src/components/LoginForm';

export default class App extends Component {
  // Setup Firebase
  componentWillMount() {
    const config = {
      apiKey: 'AIzaSyBIyc6nSBQwec3b84C6hKS5IQlyUS1JkAk',
      authDomain: 'manager-82e0d.firebaseapp.com',
      databaseURL: 'https://manager-82e0d.firebaseio.com',
      projectId: 'manager-82e0d',
      storageBucket: 'manager-82e0d.appspot.com',
      messagingSenderId: '707007441911'
    };
    firebase.initializeApp(config);
  }

  render() {
    const store = createStore(reducers, {}, applyMiddleware(ReduxThunk));
    return (
      <Provider store={store}>
        <LoginForm />
      </Provider>
    );
  }
}

AuthReducers.js:

import { EMAIL_CHANGED, PASSWORD_CHANGED } from './../actions/types';

const INITIAL_STATE = { email: '', password: '' };

export default (state = INITIAL_STATE, action) => {
  console.log(action);
  switch (action.type) {
    case EMAIL_CHANGED:
      return { ...state, email: action.payload };
    case PASSWORD_CHANGED:
      return { ...state, password: action.payload };
    default:
      return state;
  }
};

动作创作者:

import firebase from 'firebase';
import { EMAIL_CHANGED, PASSWORD_CHANGED } from './types';

export const emailChanged = text => ({
  type: EMAIL_CHANGED,
  paylod: text
});

export const passwordChanged = text => ({
  type: PASSWORD_CHANGED,
  payload: text
});

export const loginUser = ({ email, password }) => dispatch => {
  firebase
    .auth()
    .signInWithEmailAndPassword({ email, password })
    .then(user => {
      dispatch({ type: 'LOGIN_USER_SUCCESS', payload: user });
    });
};

组件:LoginForm.js

import React, { Component } from 'react';
import { Card, CardSection, Input, Button } from './common';
import { connect } from 'react-redux';

// import action creator yang mau dipakai
import { emailChanged, passwordChanged, loginUser } from './../actions/';

class LoginForm extends Component {
  onEmailChange(text) {
    this.props.emailChanged(text);
  }

  onPasswordChange(text) {
    this.props.passwordChanged(text);
  }

  onButtonPress() {
    const { email, password } = this.props;
    this.props.loginUser({ email, password });
  }

  render() {
    return (
      <Card>
        <CardSection>
          <Input
            label="Email"
            placeholder="email@domain.com"
            onChangeText={this.onEmailChange.bind(this)}
            value={this.props.email}
          />
        </CardSection>

        <CardSection>
          <Input
            label="Password"
            placeholder="enter your password"
            secureTextEntry
            onChangeText={this.onPasswordChange.bind(this)}
            value={this.props.password}
          />
        </CardSection>

        <CardSection>
          <Button whenPressed={this.onButtonPress.bind(this)}>Login</Button>
        </CardSection>
      </Card>
    );
  }
}

const mapStateToProps = state => ({
  email: state.auth.email, // .auth. -> dapet dari reducers
  password: state.auth.password
});

export default connect(mapStateToProps, {
  emailChanged,
  passwordChanged,
  loginUser
})(LoginForm);

组件:Input.js

import React from "react";
import { View, Text, TextInput } from "react-native";

const Input = ({ label, value, onChangeText, placeholder, secureTextEntry }) => {
    const { containerStyle, labelStyle, inputStyle } = styles;

    return (
        <View style={containerStyle}>
            <Text style={labelStyle}>{ label }</Text>
            <TextInput
                secureTextEntry={secureTextEntry}
                autoCorrect={false}
                placeholder={placeholder}
                style={inputStyle}
                value={value}
                onChangeText={onChangeText}
                underlineColorAndroid='transparent'
            />
        </View>
    );
}

const styles = {
    containerStyle: {
        height: 40,
        flex: 1,
        flexDirection: 'row',
        alignItems: 'center'
    },
    labelStyle:{
        fontSize: 18,
        flex: 1,
        paddingLeft: 20
    },
    inputStyle:{
        fontSize: 18,
        paddingLeft: 5,
        paddingRight: 5,
        flex: 2,
        lineHeight: 23,
        color: '#000',
    }
};

export { Input };

任何人都可以帮助我吗?为什么缺少输入密码值?

2 个答案:

答案 0 :(得分:0)

如错误所示,&#34; firebase.auth()。signInWithEmailAndPassword&#34;需要两个参数。将您的loginUser函数更改为:

export const loginUser = ({ email, password }) => dispatch => {
  firebase
    .auth()
    .signInWithEmailAndPassword( email, password )
    .then(user => {
      dispatch({ type: 'LOGIN_USER_SUCCESS', payload: user });
    });
};

答案 1 :(得分:0)

只需添加 之所以会出现此错误,是因为您在单个参数(一个对象)中包含了电子邮件和密码。由vbandrade回答,这两个参数应分为两个参数。

PS:另外,在您的应用中公开敏感数据也不是一个好习惯,您可以阅读有关使用react-native-dotenv

的更多信息