每次刷新时都会创建会话。关于云功能(firebase)

时间:2020-05-26 15:32:11

标签: node.js firebase express session google-cloud-functions

我正在使用Firebase云功能进行登录和注册页面,但该会话将不构成。每次刷新或重新访问时,它都会创建一个新会话。因此,我放弃登录和注册页面,直到解决此会话问题为止。我试图制作一个简单的会话方法来检查其是否工作,但仍然存在相同的问题。每次创建会话。

const functions = require('firebase-functions')
const admin = require('firebase-admin')
const {Firestore} = require('@google-cloud/firestore')
const {FirestoreStore} = require('@google-cloud/connect-firestore');
const serviceAccount = require('./firebase-key/**.json')

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "**.com"
})

const db = admin.firestore();
const express = require('express')
const app = express()
const session = require('express-session');
const cors = require('cors')({origin: true});
const fs = require('fs');


app.use((req, res, next) =>{
    res.header('Access-Control-Allow-Origin', '**.com');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, X-AUTHENTICATION, X-IP, Content-Type, Accept');
    res.header('Access-Control-Allow-Credentials', true);
    res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
    next();
})

app.use(session({
    secret: '**',
    resave: false,
    saveUninitialized: false,
    store: new FirestoreStore({
        dataset: new Firestore({
          kind: 'express-sessions',
        }),
    }),
    cookie : { 
        maxAge : 1000 * 60 * 30
    }
}));
app.use(cors);


app.get('/testing-session', function(req, res, next){
    console.log('req.session: ', req.session)
    if(req.session.num === undefined){
        req.session.num = 1
    }else{
        req.session.num = req.session.num + 1
    }
    res.json({
        num: `${req.session.num}`
    })
})


exports.api = functions.https.onRequest(app)

(我已经检查**),这是我的代码。通常,我复制了一个可以正常工作的代码,但我的却不行。

0 个答案:

没有答案