使用 Firebase 和 React Native 发送验证电子邮件 [英] Sending verification email with Firebase and React Native

查看:66
本文介绍了使用 Firebase 和 React Native 发送验证电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to send the validation email upon the account registration, using firebase. The registration is being done successfully but whenever I try to code email verification it gives me an error. Probably because I don't know where to place it. All my firebase methods are on Fire.js, which are the following:

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;

推荐答案

createUserWithEmailAndPassword() 方法返回一个使用 UserCredential AND(如文档所示)成功创建用户帐户,此用户也将登录到您的应用程序."

The createUserWithEmailAndPassword() method returns a Promise which resolves with a UserCredential AND (as the the doc indicates) "on successful creation of the user account, this user will also be signed in to your application."

因此,您可以使用 UserCredentialuser 属性轻松获取已登录用户,并调用 sendEmailVerification() 方法,如下:

So you can easily get the signed in user by using the user property of the UserCredential, and call the sendEmailVerification() method, as follows:

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.


Note that you may pass an ActionCodeSettings object to the sendEmailVerification() method, see the doc.

这篇关于使用 Firebase 和 React Native 发送验证电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆