inProgress-team / react-native-meteor

Meteor Reactivity for your React Native application :)
MIT License
693 stars 210 forks source link

User logged out after loginWithPassword and changing network WIFI/4G #335

Open marwaSZ opened 5 years ago

marwaSZ commented 5 years ago

Hi, I have created a simple App that connects to a remote server and just do login a user and logout. When i reload app when user is logged In and change network, everything works well as expected, the server reconnect and the user still exists

After LoginWithPassword and charging the user connected view, when changing Network, the server disconnects and reconnect in a normal way, except that after a few seconds it goes to Login page with user null and every time i click on login, the user is still null and i still get the login page. When i reload in this case, i have the screen userConnected.

My code is Here :

import React, {Component} from 'react';
import { StyleSheet, Text, View, TouchableOpacity} from 'react-native';

import Meteor, { withTracker } from 'react-native-meteor';

type Props = {};

Meteor.connect('wss:XXXX'); //do this only once

class App extends Component {
  constructor(){
    super();
    this.state={
      subscribed: false,
    }
  }
  loginUser(){
    Meteor.loginWithPassword('hh@hh.mmm', 'qwerty', (err) => {
      if (err) {
       console.log('Error Login')
      }
    });
  }
  logoutUser(){
    Meteor.logout();
  }
  render() {
    const {
    user, status, loggingIn,
  } = this.props;

    if (status.connected === false || loggingIn) {
      console.log('WE are LOADING')
      return(
        <View style={styles.container}>
          <Text style={styles.welcome}>LOADING!</Text>
        </View>
      );
    }
    if (user === null) {
      console.log('WE are not Logged')
      return (
        <View style={styles.container}>
          <Text style={styles.welcome}>LOGIN!</Text>
          <TouchableOpacity onPress={()=>this.loginUser()}>
            <Text>CONNECT</Text>
          </TouchableOpacity>
        </View>
      );
    }else {
      console.log('WE are logged in')
      return(
        <View style={styles.container}>
          <Text style={styles.welcome}>UserConnected</Text>
          <TouchableOpacity onPress={()=>this.logoutUser()}>
            <Text>DISCONNECT</Text>
          </TouchableOpacity>
        </View>
      );

    }

    }
  }

export default withTracker(params => {

  return {
    status: Meteor.status(),
    user: Meteor.user(),
    loggingIn: Meteor.loggingIn(),
  };
})(App);

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});

Demo_BUG

marwaSZ commented 5 years ago

Hi again, I just wonder why are we connecting again when logging out the user, in react-native-meteor/src/user/User.js :

logout(callback) {
    call('logout', err => {
      this.handleLogout();
      this.connect();

      typeof callback == 'function' && callback(err);
    });
  },

I had to comment this.connect(); and it works now. every time I reconnect the user, it works very well.

Thanks