【问题标题】:Sending verification email with Firebase and React Native使用 Firebase 和 React Native 发送验证电子邮件
【发布时间】:2020-09-08 00:00:28
【问题描述】:

我正在尝试使用 firebase 在帐户注册时发送验证电子邮件。注册已成功完成,但每当我尝试对电子邮件验证进行编码时,它都会给我一个错误。可能是因为我不知道放在哪里。我所有的firebase方法都在Fire.js上,分别是:

import firebaseKeys from './Config';
import firebase from 'firebase';
require("firebase/firestore");

class Fire {
constructor() {
    if (!firebase.apps.length) {
        firebase.initializeApp(firebaseKeys);
      }
}


addPost = async ({ text, localUri }) => {
    const remoteUri = await this.uploadPhotoAsync(localUri, 'photos/${this.uid}/${Date.now()}');

    return new Promise((res, rej) => {
        this.firestore.collection('posts').add({
            text,
            uid: this.uid,
            timestamp: this.timestamp,
            image: remoteUri
        })
            .then(ref => {
                res(ref);
        })
        .catch(error => {
            rej(error);
        });
    });
}

uploadPhotoAsync = async (uri, filename) => {
    return new Promise(async (res, rej) => {
        const response = await fetch(uri);
        const file = await response.blob();

        let upload = firebase
            .storage()
            .ref(filename)
            .put(file);

        upload.on(
            "state_changed",
            snapshot => {},
            err => {
                rej(err);
            },
            async () => {
                const url = await upload.snapshot.ref.getDownloadURL();
                res(url);
            }
        );
    });
}

createUser = async user => {
    let remoteUri = null

    try {
        await firebase.auth().createUserWithEmailAndPassword(user.email, user.password)
//I tried to code it here with user.sendEmailVerification();
        let db = this.firestore.collection("users").doc(this.uid)

        db.set({
            name: user.name,
            email: user.email,
            avatar: null
        })

        if (user.avatar) {
            remoteUri = await this.uploadPhotoAsync(user.avatar, 'avatars/${this.uid}')

            db.set({avatar: remoteUri}, {merge: true});
        }
    } catch (error) {
        alert("Error: ", error);
    }
};

get firestore() {
    return firebase.firestore();
}

get uid() {
    return (firebase.auth().currentUser || {}).uid;
}

get timestamp() {
    return Date.now();
}
}

Fire.shared = new Fire();
export default Fire;

【问题讨论】:

    标签: reactjs firebase react-native firebase-authentication


    【解决方案1】:

    createUserWithEmailAndPassword() 方法返回一个用 UserCredential AND 解析的 Promise(如文档所示)“在成功创建用户帐户后,此用户也将登录到您的应用程序。”

    这样就可以通过UserCredentialuser属性轻松获取登录用户,并调用sendEmailVerification()方法,如下:

    try {
        const userCredential = await firebase.auth().createUserWithEmailAndPassword(user.email, user.password);
    
        await userCredential.user.sendEmailVerification();
    
        //In the next line, you should most probably use userCredential.user.uid as the ID of the Firestore document (instead of this.uid)
        cont db = this.firestore.collection("users").doc(this.uid);
    
        //...
    
    } catch (...)
    

    请注意,您可以将ActionCodeSettings 对象传递给sendEmailVerification() 方法,请参阅doc

    【讨论】:

    猜你喜欢
    • 2021-08-17
    • 2019-06-24
    • 2018-11-21
    • 1970-01-01
    • 2017-03-17
    • 2019-03-13
    • 2021-10-10
    • 1970-01-01
    • 2020-08-06
    相关资源
    最近更新 更多