-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.js
83 lines (77 loc) · 2.2 KB
/
connect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const mongoose = require('mongoose');
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const app = require('./app');
const config = require ('./config.js');
const session = require('express-session');
const passport = require('passport');
const MongoStore = require('connect-mongodb-session')(session);
//connect to MongoDb Atlas
const connect = async (uri) => {
const options = {
dbName: "grandma",
useNewUrlParser: true,
useUnifiedTopology: true
};
try {
await mongoose.connect(uri, options);
console.log('Successfully connected to MongoDB Atlas');
} catch (err) {
console.log('Error connecting to MongoDB Atlas', err);
process.exit(1);
}
}
//initialize app
admin.initializeApp();
connect(config.mongoUrl);
const firebaseApp = express();
firebaseApp.set("trust proxy", true);
//session storage in mongodb
const store = new MongoStore({
uri: config.mongoUrl,
databaseName: 'grandma',
collection: 'sessions',
expires: 1000 * 60 * 60 * 24 * 90, //3 months
connectionOptions: {
useNewUrlParser: true,
useUnifiedTopology: true,
}
});
exports.extendSession = async (sessionId, expirationDate, done) => {
try {
const db = store.client.db('grandma');
await db.collection('sessions').updateOne(
{ _id: sessionId },
{ $set: { expires: expirationDate } }
)
return done();
} catch (err) {
done(err);
}
}
exports.destroySession = (sessionId, done) => {
store.destroy(sessionId, (err) => {
if (err) { return done(err) }
return done();
})
}
//configure sessions
firebaseApp.use(session({
secret: config.sessionSecret,
resave: false,
saveUninitialized: false,
cookie: {
secure: true,
httpOnly: true,
sameSite: 'none',
expires: 1000 * 60 * 60 * 24 * 90 //3 months
},
store: store,
proxy: true
}))
firebaseApp.use(passport.initialize());
firebaseApp.use(passport.session());
//define and export entire app as cloud function
firebaseApp.use(app);
exports.api = functions.https.onRequest(firebaseApp);