node.js和Firebase会话控制

时间:2016-09-26 12:12:45

标签: node.js firebase firebase-authentication express-session

我正在使用firebase作为存储在node.js上构建应用程序。

使用此功能登录时

firebase.auth().signInWithEmailAndPassword(N1,N2).catch(function(error)

用户成功登录但是当我从另一个浏览器打开我的localhost时,先前用户登录状态显示。

我认为会话正在被覆盖或类似

请帮我这里是我的server.js文件: -

const express = require('express');
var assert = require('assert');
var bodyParser = require("body-parser");
var path = require('path');
var app = express();
const session = require('express-session');
var OpenTok = require('opentok'),
    opentok = new OpenTok('xxxxxxxxx', 'xxxxxxxxxxxxxxxx');
const FirebaseStore = require('connect-session-firebase')(session);
const firebase = require('firebase');
const ref = firebase.initializeApp({
  apiKey: "xxxxxxxxxxxxx",
    authDomain: "xxxxxxxxxxxx",
    databaseURL: "https://xxxxxxxxx",
    storageBucket: "xxxxxxxxxxxx",
    messagingSenderId: "xxxxxxxxxxx"
});

app.set('port', (process.env.PORT || 5000));
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'views')));
app.set('views', __dirname + '/views');
app.use(express.static(__dirname + '/public'));
app.use(session({resave: false, saveUninitialized: true, secret:'xxxxxx'}));
app.use(bodyParser.json());

express()
  .use(session({
    store: new FirebaseStore({
      database: ref.database()
    }),
    secret: 'xxxxxxxxx',
    resave: true,
    saveUninitialized: true
  }));
app.get('/', function(req, res){
    firebase.auth().onAuthStateChanged(function(user){
        if(user){
            res.redirect("/admin");
            console.log(user.email);
        } 
        else {
            res.render('index2.ejs');   
        }
        }); 
});

我使用此程序包 Connect Session Firebase 进行调试,做一些事情,但如果我做错了请告诉我们。

登录代码

app.post('/signin',function (req, res){
    var N1 = req.body.N1_in;
    var N2 = req.body.N2_in;

        firebase.auth().signInWithEmailAndPassword(N1,N2).catch(function(error) {
        }); 
        function function1() {
            // stuff you want to happen right away

        }

        function function2() {
            // all the stuff you want to happen after that pause
            res.redirect('/admin');
        }

        // call the first chunk of code right away
        function1();

        // call the rest of the code and have it execute after 3 seconds
        setTimeout(function2, 2500);
}); 

0 个答案:

没有答案