我一直试图让本机和firebase与其他人一起工作,但无论我在哪里看起来我似乎都找到了过时的教程。 当我最终能够初始化我的firebase对象时,我们在其他教程中使用的方法名称没有用我定义的对象定义。 我对此有点不知所措。 任何帮助都将受到大力赞赏。 谢谢。
import React, { Component } from 'react';
import {View} from 'react-native';
import * as Firebase from 'firebase';
var firebaseConfig = {
apiKey: '....',
authDomain: '....',
databaseURL: 'https://....',
storageBucket: '...',
};
Firebase.initializeApp(firebaseConfig, 'unique_id');
class LoginProcess extends Component {
constructor(props) {
super(props);
var fbRef = Firebase;
}
login(email, password) {
if (this.props.onLoginRequested) {
this.props.onLoginRequested();
}
// Firebase.authWithPassword({
// 'email': email,
// 'password': password
// },
// (error, user_data) => {
// if (error) {
// console.log('Login Failed. Please try again');
// }
// else {
// AsyncStorage.setItem('user_data', JSON.stringify(user_data));
// console.log(JSON.stringify(user_data))
// }
// });
}
render () {
return (<View/>)
}
}
module.exports = LoginProcess;
答案 0 :(得分:1)
import React, { Component } from 'react';
import {View} from 'react-native';
import * as Firebase from 'firebase';
var firebaseConfig = {
apiKey: '.....',
authDomain: '......',
databaseURL: 'https://......',
storageBucket: '......',
};
const firebaseApp = Firebase.initializeApp(firebaseConfig, 'unique_id');
const auth = firebaseApp.auth();
class LoginProcess extends Component {
login(email, password) {
if (this.props.onLoginRequested) {
this.props.onLoginRequested();
}
// auth.createUserWithEmailAndPassword // for signup
auth.signInWithEmailAndPassword(email, password)
.then((data) => {
if (this.props.onLoginSuccess) {
this.props.onLoginSuccess(data)
}
})
.catch((error) => {
var errorCode = error.code;
var errorMessage = error.message;
if (this.props.onLoginError) {
this.props.onLoginError(error.code, error.message)
}
});
}
render () {return <View/>;}
}
module.exports = LoginProcess;
答案 1 :(得分:0)
你已经找到了解决方案,但是我已经把它与Redux联系起来了(如果还不存在则创建用户)
export const loginUser = ({ email, password }) => {
return (dispatch) => {
dispatch({ type: LOGIN_USER });
firebase.auth().signInWithEmailAndPassword(email, password)
.then(user => loginUserSuccess(dispatch, user))
.catch(() => {
firebase.auth().createUserWithEmailAndPassword(email, password)
.then(user => loginUserSuccess(dispatch, user))
.catch(() => loginUserFail(dispatch));
});
};
};
const loginUserSuccess = (dispatch, user) => {
dispatch({
type: LOGIN_USER_SUCCESS,
payload: user
});
Actions.main();
};
我还想知道你是否已经找到了一种方法,可以在重新打开应用程序时坚持登录?