Expo中Firebase邮箱验证功能实现问题求助
Expo + Firebase 注册后无法发送邮箱验证邮件问题
我参考Stack Overflow相关教程,尝试在Expo项目中通过Firebase创建用户账号后发送邮箱验证邮件,但始终无法实现该功能。目前账号可以成功创建,但验证邮件从未发送至注册邮箱。
初始代码
import { StyleSheet, Text, View, KeyboardAvoidingView, TextInput, TouchableOpacity } from 'react-native' import React from 'react' import { useNavigation } from '@react-navigation/native'; import { createUserWithEmailAndPassword } from "firebase/auth" import { useState } from 'react' import { auth } from '../firebase' import { signOut } from 'firebase/auth' import {sendEmailVerification } from "firebase/auth"; const RegisterScreen = () => { const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [name, setName] = useState('') const [showError, setShowError] = useState(false); const navigation = useNavigation() const handleSignUp = async () => { try { if (email && password) { setShowError(false); const { user } = await createUserWithEmailAndPassword(auth, email, password) console.log('Registered as :' , user.email); try{ const {user} = await sendEmailVerification(auth, email) } catch{ } try{ await signOut(auth) console.log("Signed out successfully") navigation.replace("Login") }catch (error) { console.log({error}); } } } catch (error) { console.log({error}); setShowError(true); } } return (
两次修改尝试及问题
修改1代码
try { if (email && password) { setShowError(false); createUserWithEmailAndPassword(email, password).then(async ({user}) => { // 2. Send verification email await user.sendEmailVerification() console.log("Verification email sent!") }) catch{ } try{ await signOut(auth) console.log("Signed out successfully") navigation.replace("Login") }catch (error) { console.log({error}); } } } catch (error) { console.log({error}); setShowError(true); }
- 出现代码错误,且用户自动登录后无法登出。
修改2代码
try { if (email && password) { setShowError(false); const { user } = await createUserWithEmailAndPassword(auth, email, password)..then(async ({user}) => { // 2. Send verification email await user.sendEmailVerification() console.log("Verification email sent!") console.log('Registered as :' , user.email); try{ await signOut(auth) console.log("Signed out successfully") navigation.replace("Login") }catch (error) { console.log({error}); } } } catch (error) { console.log({error}); setShowError(true); }
- 仍报错,登出功能失效,重复邮箱可被提交但未存入Firebase。
额外问题
导入import firebase from 'firebase'时出现错误。
当前代码
import { StyleSheet, Text, View, KeyboardAvoidingView, TextInput, TouchableOpacity } from 'react-native' import React from 'react' import { useNavigation } from '@react-navigation/native'; import { createUserWithEmailAndPassword } from "firebase/auth" import { useState } from 'react' import { auth } from '../firebase' import { signOut } from 'firebase/auth' import {sendEmailVerification } from "firebase/auth"; const RegisterScreen = () => { const [email, setEmail] = useState('') const [password, setPassword] = useState('') const [name, setName] = useState('') const [showError, setShowError] = useState(false); const navigation = useNavigation() const handleSignUp = async () => { try { if (email && password) { setShowError(false); const { user } = await createUserWithEmailAndPassword(auth, email, password).then(async ({user}) => { auth().currentUser.sendEmailVerification() console.log("Verification email sent!") }) console.log('Registered as :' , user.email); try{ await signOut(auth) console.log("Signed out successfully") navigation.replace("Login") }catch (error) { console.log({error}); } } } catch (error) { console.log({error}); setShowError(true); } } return ( <KeyboardAvoidingView //To prevent keyboard from blocking the writing area style={styles.container} behavior = "padding" > <View style = {styles.inputContainer}> <Text>Email:</Text> <TextInput placeholder = "Email" value={email} onChangeText ={text => setEmail(text)} style = {styles.input} /> <Text></Text> <Text>Password:</Text> <TextInput placeholder = "Password (Min: 6 chars)" value={password} onChangeText ={text => setPassword(text)} style = {styles.input} secureTextEntry //Hide password /> </View> {showError && <View style={styles.error}> <Text>Email taken or password not valid - Min: 6 char</Text> </View>} <View style = {styles.buttonContainer}> <TouchableOpacity onPress = {handleSignUp} style = {styles.button} > <Text style={styles.buttonText}>Register</Text> </TouchableOpacity> </View> </KeyboardAvoidingView> ) }
恳请各位帮忙解决该问题,非常感谢!
内容的提问来源于stack exchange,提问作者L.Calvin
相关产品推荐
相关产品推荐

