I am the beginner of nodejs and firebase. I am using post method to sign in with email and password, after the signInWithEmailAndPassword process completed will go to listener onAuthStateChanged. I would like to response the status code in onAuthStateChanged, can i response outside the post?
var express = require('express');
var app = express();
var router = express.Router();
var firebase = require('../db');
var bodyParser = require('body-parser');
// for forms
router.use(bodyParser.urlencoded({extended: true}));
router.use(bodyParser.json());
router.post('/', function (req, res) {
var email = req.body.email;
var password = req.body.password;
firebase.auth().signInWithEmailAndPassword(email,
password).catch(function(error) {
if(error) {
res.status(404).json();
}
});
});
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
user.getToken().then(function(token) {
// I want to response 200 status code and token here
res.status(200).json({token:token})
});
}
});
module.exports = router;
via Ong Kah Kit
No comments:
Post a Comment