firebase上的node.js从服务器端发送用户验证邮件

时间:2018-05-29 13:56:48

标签: node.js firebase firebase-authentication firebase-admin

我想在服务器端创建一个新用户,我有:

import * as express   from 'express'           ;
import * as admin     from 'firebase-admin'    ;
import * as functions from 'firebase-functions';
import * as path      from "path"              ;
import * as math      from 'mathjs'            ;

const accountKeyPath = path.join(__dirname, '../credentials/serviceAccountKey.json');
const accountKey     = require(accountKeyPath);

const adminSDKPath   = path.join(__dirname, '../credentials/spice-ai-firebase-adminsdk.json');
const adminSDK       = require(adminSDKPath);

const firebaseAdmin = admin.initializeApp({
      credential : admin.credential.cert(adminSDK)
    , databaseURL: accountKey['databaseURL']
});

const dbRef = new Proposal(firebaseAdmin, 'datasets');
const auth  = firebaseAdmin.auth()

const app = express();

app.get('/user', (req, res) => {
    res.send('CRUDing user ...');
    createUser(auth)
}

function createUser(auth){
    const email : string = 'person-' + math.round(math.random(1,100),0) + '@gmail.com'
    const phone : string = '+1' +  math.round(math.random()*10000000000,0);
    const passoword : string = email + '-password'



    auth.createUser({
          email         : email
        , emailVerified : false
        , phoneNumber  : phone
        , password     : passoword
        , displayName  : "some name"
        , photoURL     : "https://firebase.google.com/docs/firestore/solutions/arrays"
        , disabled     : false      
    }) 

    .then(userRecord => {

        const user = auth.currentUser;
        console.log("created new user: ", userRecord.uid, user);

    })
    .catch(err => {
        console.log('error: ', err)

    })
}

我想在创建帐户后发送验证电子邮件,但是典型的API auth.currentUser.sendEmailVerification不可用,可能是因为firebase无法跟踪快递会话中的应用状态?有没有办法从服务器端发送电子邮件验证,或检测当前用户,而无需推出自己的解决方案?

0 个答案:

没有答案