2022-04-01 10:35:36 +00:00
|
|
|
import MongoStore from "connect-mongo";
|
|
|
|
import session from "express-session";
|
2022-04-01 15:27:58 +00:00
|
|
|
import { mongooseDbName, mongoURI } from "../database/mongoose.js";
|
2022-04-01 10:35:36 +00:00
|
|
|
const sessionConf = {
|
|
|
|
secret: process.env.SESSION_SECRET || "super duper secret string.",
|
|
|
|
cookie: {
|
|
|
|
expires: process.env.SESSION_TIMEOUT || 300000,
|
|
|
|
httpOnly: true,
|
|
|
|
},
|
|
|
|
saveUninitialized: false,
|
|
|
|
resave: false,
|
|
|
|
};
|
|
|
|
if (process.env.NODE_ENV === "production") {
|
|
|
|
sessionConf.cookie.secure = true;
|
2022-04-01 15:27:58 +00:00
|
|
|
sessionConf.store = MongoStore.create({ mongoUrl: mongoURI, dbName: mongooseDbName });
|
2022-04-01 10:35:36 +00:00
|
|
|
}
|
|
|
|
export const userSession = session(sessionConf);
|
|
|
|
|
|
|
|
export function authenticationGuard(req, res, next) {
|
|
|
|
if (req.session.userId) {
|
|
|
|
next();
|
|
|
|
} else {
|
|
|
|
res.sendStatus(401);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Authentication
|
|
|
|
// TODO: Identity
|
|
|
|
// TODO: Authority
|