简体   繁体   English

尝试登录时反应本机 firebase 注销方法会删除我的 email 和密码数据

[英]react native firebase sign out method deletes my email and password data when trying to sign in

I have a signout method in my react native app that each time i sign out and try to sign back in my email and password is not remembered in firebase.我的反应本机应用程序中有一个注销方法,每次我注销并尝试重新登录我的 email 并且 firebase 中不记住密码时。 The database shows the email and password but for some reason when i enter my email and password it doesn't log in. I can't find what i'm doing wrong.数据库显示 email 和密码,但由于某种原因,当我输入 email 和密码时,它没有登录。我找不到我做错了什么。

here is my sign in screen这是我的登录屏幕

import React, { useState } from 'react'
import { Image, Text, TextInput, TouchableOpacity, View, ImageBackground } from 'react-native'
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
import styles from './styles';
import { firebase } from '../../firebase/config';


export default function LoginScreen({navigation}) {
    const [email, setEmail] = useState('')
    const [password, setPassword] = useState('')


    const onFooterLinkPress1 = () => {
        navigation.navigate("Registration")
    }

    const onFooterLinkPress2 = () => {
        navigation.navigate("ResetPassword")
    }

    const onLoginPress = () => {
        firebase
            .auth()
            .signInWithEmailAndPassword(email, password)
            .then((response) => {
                const uid = response.user.uid
                const usersRef = firebase.firestore().collection('users')
                usersRef
                    .doc(uid)
                    .get()
                    .then(firestoreDocument => {
                        if (!firestoreDocument.exists) {
                            alert("User does not exist anymore.")
                            return;
                        }
                        const user = firestoreDocument.data()
                        
                    })
                    .catch(error => {
                        alert(error)
                    });
            })
            .catch(error => {
                alert(error)
            })
        }

    return (
        <ImageBackground source={require('../../../assets/backgroundCopySilk.jpg')} style={styles.backgroundImage}>
        <View style={styles.container}>
            <KeyboardAwareScrollView
                style={{ flex: 1, width: '100%' }}
                keyboardShouldPersistTaps="always">
                <Image
                    style={styles.logo}
                    source={require('../../../assets/logoCopy.png')}
                />
                <TextInput
                    style={styles.input}
                    placeholder='E-mail'
                    placeholderTextColor="#aaaaaa"
                    onChangeText={(text) => setEmail(text)}
                    value={email}
                    underlineColorAndroid="transparent"
                    autoCapitalize="none"
                />
                <TextInput
                    style={styles.input}
                    placeholderTextColor="#aaaaaa"
                    secureTextEntry
                    placeholder='Password'
                    onChangeText={(text) => setPassword(text)}
                    value={password}
                    underlineColorAndroid="transparent"
                    autoCapitalize="none"
                />
                <TouchableOpacity
                    style={styles.button}
                    onPress={() => onLoginPress()}>
                    <Text style={styles.buttonTitle}>Log in</Text>
                </TouchableOpacity>
                <View style={styles.footerView}>
                    <Text style={styles.footerText}>Don't have an account? <Text onPress={onFooterLinkPress1} style={styles.footerLink}>Sign up</Text></Text>
                </View>
                <View style={styles.footerView}>
                    <Text style={styles.footerText}>Forgot Password? <Text onPress={onFooterLinkPress2} style={styles.footerLink}>Reset Here</Text></Text>
                </View>
            </KeyboardAwareScrollView>
        </View>
        </ImageBackground>
    )
}

Here is my sign out screen这是我的退出屏幕

import React from 'react';
import { Image, Text, Linking, TouchableOpacity, View,ImageBackground, Alert } from 'react-native'
import styles from './styles';
import { firebase } from '../../firebase/config';
import { auth } from 'firebase';
import {Spacer} from '../spacer';



export default function ProductScreen({navigation}){

    const logOutPress = () => {
        try {
        auth()
        .signOut()
        .then(() => { navigation.navigate("Login"),
        alert('You have signed out')})
    } catch(error){
    console.log('Unable to logout')}
    }

   const yalaPress = () =>  Alert.alert( "You're about to leave the app",[
    { text: "Cancel",
        onPress: ()=> console.log('Cancel Pressed')},
    {text: "Ok", 
        onPress: () => Linking.openURL('https://yalajets.com/')
    }],{ cancelable: false }  );


    return(
<ImageBackground source={require('../../../assets/backgroundCopySilk.jpg')} style={styles.backgroundImage}>
<View style={styles.container}>

<TouchableOpacity 
onPress={()=> navigation.navigate('LawnCare')}
style={styles.button}>
<Text style={styles.buttonText}>Luxury Commercial Lawn Care</Text>
</TouchableOpacity>

<TouchableOpacity style={styles.button}>
<Text style={styles.buttonText}>Luxury Vehicle Car Detail</Text>
</TouchableOpacity>

<TouchableOpacity style={styles.button}>
<Text style={styles.buttonText}>Luxury Pharmaceuticals</Text>
</TouchableOpacity>

<TouchableOpacity style={styles.button}>
<Text style={styles.buttonText}>Luxury Personal Fitness</Text>
</TouchableOpacity>

<TouchableOpacity style={styles.button}>
<Text style={styles.buttonText}>Luxury Massage with Catch These Hands</Text>
</TouchableOpacity>

<TouchableOpacity 
onPress={yalaPress}
style={styles.button}>
<Text style={styles.buttonText}>Luxury Private Flights with Yala Jets</Text>
</TouchableOpacity>

<Spacer/>
<TouchableOpacity
onPress={()=> logOutPress()}>
<Text style={styles.buttonText}>Log Out</Text>
</TouchableOpacity>
        </View>
</ImageBackground>
    )
};

also here is my sign up screen这也是我的注册屏幕

import React, { useState } from 'react'
import { Image, Text, TextInput, TouchableOpacity, View,ImageBackground } from 'react-native'
import { KeyboardAwareScrollView } from 'react-native-keyboard-aware-scroll-view';
import styles from './styles';
import { firebase } from '../../firebase/config'


export default function RegistrationScreen({navigation}) {
    const [firstName, setFirstName] = useState('')
    const [lastName, setLastName] = useState('')
    const [email, setEmail] = useState('')
    const [password, setPassword] = useState('')
    const [confirmPassword, setConfirmPassword] = useState('')
    const onFooterLinkPress = () => {
        navigation.navigate('Login')
    }

   
    const onRegisterPress = () => {
        if (password !== confirmPassword) {
            alert("Passwords don't match.")
            return
        }
        firebase
            .auth()
            .createUserWithEmailAndPassword(email, password)
            .then((response) => {
                const uid = response.user.uid
                const data = {
                    id: uid,
                    email,
                    firstName,
                    lastName
                };
                const usersRef = firebase.firestore().collection('users')
                usersRef
                    .doc(uid)
                    .set(data)
                    .then(() => {
                        navigation.navigate("Products", {user: data})
                    })
                    .catch((error) => {
                        alert(error)
                    });
            })
            .catch((error) => {
                alert(error)
        });
    }

    return (
        <ImageBackground source={require('../../../assets/backgroundCopySilk.jpg')} style={styles.backgroundImage}>
        <View style={styles.container}>
            <KeyboardAwareScrollView
                style={{ flex: 1, width: '100%' }}
                keyboardShouldPersistTaps="always">
                <Image
                    style={styles.logo}
                    source={require('../../../assets/logoCopy.png')}
                />
                <TextInput
                    style={styles.input}
                    placeholder='First Name'
                    placeholderTextColor="#aaaaaa"
                    onChangeText={(text) => setFirstName(text)}
                    value={firstName}
                    underlineColorAndroid="transparent"
                    autoCapitalize="none"
                />
                <TextInput
                    style={styles.input}
                    placeholder='Last Name'
                    placeholderTextColor="#aaaaaa"
                    onChangeText={(text) => setLastName(text)}
                    value={lastName}
                    underlineColorAndroid="transparent"
                    autoCapitalize="none"
                />
                <TextInput
                    style={styles.input}
                    placeholder='E-mail'
                    placeholderTextColor="#aaaaaa"
                    onChangeText={(text) => setEmail(text)}
                    value={email}
                    underlineColorAndroid="transparent"
                    autoCapitalize="none"
                />
                <TextInput
                    style={styles.input}
                    placeholderTextColor="#aaaaaa"
                    secureTextEntry
                    placeholder='Password'
                    onChangeText={(text) => setPassword(text)}
                    value={password}
                    underlineColorAndroid="transparent"
                    autoCapitalize="none"
                />
                <TextInput
                    style={styles.input}
                    placeholderTextColor="#aaaaaa"
                    secureTextEntry
                    placeholder='Confirm Password'
                    onChangeText={(text) => setConfirmPassword(text)}
                    value={confirmPassword}
                    underlineColorAndroid="transparent"
                    autoCapitalize="none"
                />
                <TouchableOpacity
                    style={styles.button}
                    onPress={() => onRegisterPress()}>
                    <Text style={styles.buttonTitle}>Create account</Text>
                </TouchableOpacity>
                <View style={styles.footerView}>
                    <Text style={styles.footerText}>Already got an account? <Text onPress={onFooterLinkPress} style={styles.footerLink}>Log in</Text></Text>
                </View>
            </KeyboardAwareScrollView>
        </View>
        </ImageBackground>
    )
}

For create user You need add some Data in the method:对于创建用户,您需要在方法中添加一些数据:

firebase
            .auth()
            .createUserWithEmailAndPassword(email, password)
            .then((response) => {
                const uid = response.user.uid
                const data = {
                    id: uid,
                    email: useremail,
                    firstName: firstname,
                    lastName: lastname
                };
                const usersRef = firebase.firestore().collection('users')
                usersRef
                    .doc(uid)
                    .set(data, {merge: true})
                    .then(() => {
                        navigation.navigate("Products", {user: data})
                    })
                    .catch((error) => {
                        alert(error)
                    });
            })
            .catch((error) => {
                alert(error)
        });

You make create login method where You chceck is a new user for example like this.您创建登录方法,其中您检查是新用户,例如这样。

emailPassRegister(mail, password) {
    this.afAuth.createUserWithEmailAndPassword(mail, pasword).then(ref => {
      if (ref.additionalUserInfo.isNewUser) {
        // method for new user
        this.addNewUser(ref.user);
        this.router.navigate(['/']);
      } else {
        // method for reapeater user
        this.updataUser(ref.user);
        this.router.navigate(['/']);
      }
    });
  }

but every time if You set new data You need use {merge: true}, because if document or filed does not exist is created if exist is updated.但每次如果您设置新数据,您需要使用 {merge: true},因为如果文档或文件不存在,则如果存在更新则创建。 I hope that help You in You problem.我希望能帮助你解决你的问题。

method to register user:注册用户方法:

RegisterUser() {
  emailPassRegister(**username from input**, **password from input**);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 React-Native 中登录/退出 Firebase 的最佳方式? - Best way to sign in/out from Firebase in React-Native? 当另一个用户使用 Firebase 身份验证登录 React Native 时如何注销? - How to sign out when another user signs in in React Native with Firebase authentication? 如何使用 email 登录用户并使用 firebase + svelte 登录密码? - How to sign in a user with email and password with firebase + svelte? 使用电子邮件和密码 firebase 以角度硬编码登录 - Hardcode sign in with email and password firebase in angular 尝试让 Firebase 电子邮件/密码身份验证与 React Native 一起使用 - Trying to get the Firebase Email/Password Authentication to work with React Native firebase signOut() 方法不退出 - firebase signOut() method doesn't sign out React Native - React Native Firebase - Google 登录 Android - React Native - React Native Firebase - Google Sign In Android 如何确定用户是使用电子邮件和密码登录 Firebase 还是使用 google 登录? - How to determine if the user signed in to Firebase with email and password or with google sign in? 使用Firebase注册电子邮件/密码时出现“网络错误”错误 - “A network error has occurred” error on email/password sign up with Firebase 注销 Firebase 身份验证 - Sign out firebase auth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM