Commit 4d181631 by Tonk

update register and login

parent e9ada991
...@@ -45,20 +45,24 @@ class AuthLoadingScreen extends Component { ...@@ -45,20 +45,24 @@ class AuthLoadingScreen extends Component {
// Preload data from an external API && Preload data using AsyncStorage // Preload data from an external API && Preload data using AsyncStorage
const alreadyLaunched = await AsyncStorage.getItem('alreadyLaunched'); const alreadyLaunched = await AsyncStorage.getItem('alreadyLaunched');
const RememberedLogin = await AsyncStorage.getItem('RememberedLogin'); const RememberedLogin = await AsyncStorage.getItem('RememberedLogin');
// console.log('alreadyLaunched', alreadyLaunched);
// console.log('RememberedLogin', RememberedLogin); if (alreadyLaunched === 'true') {
if (RememberedLogin === 'true') {
app.auth().onAuthStateChanged(async user => { app.auth().onAuthStateChanged(async user => {
if (user) { if (user) {
console.log(user); console.log(user);
await this.props.getCurrentUser(user); await this.props.getCurrentUser(user);
await this.getToken(); await this.getToken();
this.props.navigation.navigate(RememberedLogin === 'true' ? 'Main' : 'Login'); this.props.navigation.navigate('Main');
// createMockData(); }
});
} else { } else {
this.props.navigation.navigate(alreadyLaunched === 'true' ? 'Login' : 'Onboarding'); app.auth().signOut();
this.props.navigation.navigate('Login');
} }
}); } else {
this.props.navigation.navigate('Onboarding');
}
} catch (e) { } catch (e) {
alert(e); alert(e);
} }
......
...@@ -5,8 +5,10 @@ import AsyncStorage from '@react-native-community/async-storage'; ...@@ -5,8 +5,10 @@ import AsyncStorage from '@react-native-community/async-storage';
import LoginForm from '../../components/Form/LoginForm'; import LoginForm from '../../components/Form/LoginForm';
import { KeyboardAvoidingView, View } from 'react-native'; import { KeyboardAvoidingView, View } from 'react-native';
import app from '../../firebase'; import app from '../../firebase';
import { getCurrentUser } from '../../reduxStore/actions/cerrentUserAction';
import { connect } from 'react-redux';
export default class LoginScreen extends Component { class LoginScreen extends Component {
static navigationOptions = { static navigationOptions = {
header: null, header: null,
}; };
...@@ -22,6 +24,8 @@ export default class LoginScreen extends Component { ...@@ -22,6 +24,8 @@ export default class LoginScreen extends Component {
try { try {
const userCredential = await app.auth().signInWithEmailAndPassword(email, password); const userCredential = await app.auth().signInWithEmailAndPassword(email, password);
if (userCredential) { if (userCredential) {
const user = app.auth().currentUser;
await this.props.getCurrentUser(user);
await AsyncStorage.setItem('alreadyLaunched', 'true'); await AsyncStorage.setItem('alreadyLaunched', 'true');
if (this.state.isCheck === true) { if (this.state.isCheck === true) {
await AsyncStorage.setItem('RememberedLogin', 'true'); await AsyncStorage.setItem('RememberedLogin', 'true');
...@@ -74,3 +78,12 @@ export default class LoginScreen extends Component { ...@@ -74,3 +78,12 @@ export default class LoginScreen extends Component {
); );
} }
} }
const mapDispatchToProps = {
getCurrentUser,
};
export default connect(
null,
mapDispatchToProps
)(LoginScreen);
import React, { Component } from 'react'; import React, { Component } from 'react';
import { Container, Text, CheckBox, Icon } from 'native-base'; import { Text, CheckBox } from 'native-base';
import { color, theme } from '../../constants/Styles'; import { color, theme } from '../../constants/Styles';
import { Platform, View, ScrollView } from 'react-native'; import { View, ScrollView } from 'react-native';
import RegisterForm from '../../components/Form/RegisterForm'; import RegisterForm from '../../components/Form/RegisterForm';
import { HeaderButtons, Item } from 'react-navigation-header-buttons'; import { HeaderButtons, Item } from 'react-navigation-header-buttons';
import IoniconsHeaderButton from '../../components/IoniconsHeaderButton'; import IoniconsHeaderButton from '../../components/IoniconsHeaderButton';
import app, { fireStore, baseURL } from '../../firebase'; import app from '../../firebase';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { updateUser } from '../../reduxStore/actions/cerrentUserAction'; import { getCurrentUser, updateUser } from '../../reduxStore/actions/cerrentUserAction';
class RegisterScreen extends Component { class RegisterScreen extends Component {
static navigationOptions = ({ navigation }) => ({ static navigationOptions = ({ navigation }) => ({
...@@ -43,15 +43,20 @@ class RegisterScreen extends Component { ...@@ -43,15 +43,20 @@ class RegisterScreen extends Component {
createAccount = async values => { createAccount = async values => {
const userData = { firstName: values.firstName, lastName: values.lastName, phoneNumber: values.phoneno }; const userData = { firstName: values.firstName, lastName: values.lastName, phoneNumber: values.phoneno };
try { try {
const reg = app.auth().createUserWithEmailAndPassword(values.email, values.password); const reg = app
.auth()
.createUserWithEmailAndPassword(values.email, values.password)
.then(async value => {
await this.props.getCurrentUser(value);
await this.props.updateUser(userData);
});
if (reg) { if (reg) {
this.props.updateUser(userData); // can't update data when create user
app.auth().signOut(); app.auth().signOut();
this.props.navigation.navigate('Success'); this.props.navigation.navigate('Success');
} }
} catch (error) { } catch (error) {
console.log(error); alert(error);
} }
}; };
...@@ -84,13 +89,12 @@ class RegisterScreen extends Component { ...@@ -84,13 +89,12 @@ class RegisterScreen extends Component {
} }
} }
const mapStateToProps = state => ({});
const mapDispatchToProps = { const mapDispatchToProps = {
updateUser, updateUser,
getCurrentUser,
}; };
export default connect( export default connect(
mapStateToProps, null,
mapDispatchToProps mapDispatchToProps
)(RegisterScreen); )(RegisterScreen);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment