Quantcast
Channel: Active questions tagged react-native+ios - Stack Overflow
Viewing all articles
Browse latest Browse all 16750

how to stay logged in forever after logging in using Firebase with React Native

$
0
0

I have been stuck on this problem for SEVERAL days.Basically, I'm trying to make Firebase remember the user so that it stays logged in once logged in.First block of code it the App component (I cut off some of the necessary code irrelevant to my issue for the first part.) The second block of code if the Form which maps to the Login and Signup.

TypeError: reactNativeRouterFlux.Actions.home is not a function

Please let me know how I'm able to accomplish how to stay logged in forever after logging in using Firebase with React Native

  export default class App extends Component {  constructor() {    super();    this.state = {      loading: true,    };  }   componentDidMount() {    this.authSubscription = firebase.auth().onAuthStateChanged((user) => {      this.setState({        loading: false,        user,      });    });  }   /**   * Don't forget to stop listening for authentication state changes   * when the component unmounts.   */  componentWillUnmount() {    this.authSubscription();  }  render() {      var user = firebase.auth().currentUser;     if (this.state.loading) return <Loading />;      // The user is an Object, so they're logged in     if (user) return  <Home />; // load home screen and make it route to different places without using actions     if(!user) return <Login />;      return (    // The user is null, so they're logged out<View style={{"flex": 1}}><StatusBar                backgroundColor="#002f6c"                barStyle="light-content"            />       {tests}</View>    );  }}firebase.initializeApp(firebaseConfig);export const db = firebase.firestore()  import React, {Component} from 'react';import {StylesSheet, Text, View, TextInput,         TouchableOpacity, AsyncStorage, Keyboard, SafeAreaView} from 'react-native';import {Actions} from 'react-native-router-flux';import styles from './formstyles';import VerseStuff from './Verse'import * as firebase from "firebase";import { createAppContainer } from 'react-navigation'; import { createStackNavigator } from 'react-navigation-stack';import Routes from './Routes';export default class Form extends Component {    constructor(props) {        super(props);        //this.show = this.show.bind(this);        this.state = {            email: '',            password: ''        }    }    btnToChat() {                      //here use arrow  function        console.log("test");        this.props.navigation.navigate('Home');        }    saveData = async() => {        const {email, password} = this.state;        // save data with asyncstorage        let loginDetails = {            email: email,            password        }        if(this.props.type !== 'Login') {            try {                firebase                    .auth()                    .createUserWithEmailAndPassword(email, password)                    .then(user => {                         Actions.newUserOne();                    });            } catch(error) {                errorCode = error.code;                errorMessage = error.message;                if(errorCode == 'auth/invalid-password') {                    alert('Passowrd must be at least 6 characters long!');                } else {                    Actions.newUserOne();                }            }        } else if(this.props.type == 'Login') {            try {                firebase                    .auth()                    .setPersistence(firebase.auth.Auth.Persistence.SESSION)                    .then(function() {                        return firebase.auth().signInWithEmailAndPassword(email, password)                        Actions.home();                        // this.btnToChat();                    });            } catch (error) {                errorCode = error.code;                errorMessage = error.message;                if(errorCode == 'auth/wrong-password') {                    alert('You entered the wrong password!');                } else {                    Actions.home();                    // this.btnToChat();                }            }        }    }    render() {        return (<SafeAreaView style={styles.container}><TextInput style={styles.inputBox}                                onChangeText={(email) => this.setState({email})}                                placeholder="Email"                                placeholderTextColor = "#002f6c"                                selectionColor="fff"                                onSubmitEdiitng={() => this.password.focus()} /><TextInput style={styles.inputBox}                                onChangeText={(password) => this.setState({password})}                                placeholder="Password"                                autoCapitalize="none"                                secureTextEntry={true}                                placeholderTextColor = "#002f6c"                                ref={(input) => this.password = input}/><TouchableOpacity style={styles.button}><Text style={styles.buttonText} onPress={this.saveData}>                         {this.props.type}</Text></TouchableOpacity></SafeAreaView>        )    }}

Viewing all articles
Browse latest Browse all 16750

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>