get sessions to work, better middleware design pattern
This commit is contained in:
parent
80c0a8be3e
commit
0843b798a4
17
index.js
17
index.js
|
@ -11,27 +11,12 @@ if (process.env.APP_DEBUG == 'true') {
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
const app = express();
|
const app = express();
|
||||||
const session = require('express-session');
|
const session = require('express-session');
|
||||||
const cookie_parse = require('cookie-parser');
|
|
||||||
const cookie_encrypt = require('cookie-encrypter');
|
|
||||||
const Redis = require("ioredis");
|
|
||||||
const bodyparser = require('body-parser');
|
const bodyparser = require('body-parser');
|
||||||
|
|
||||||
let redisClient = new Redis(process.env.REDIS_PORT, process.env.REDIS_HOST);
|
const { APP_PORT } = process.env;
|
||||||
let RedisStore = require("connect-redis")(session)
|
|
||||||
|
|
||||||
const { APP_PORT, APP_KEY } = process.env;
|
|
||||||
|
|
||||||
app.use(bodyparser.json());
|
app.use(bodyparser.json());
|
||||||
app.use(bodyparser.urlencoded({ extended: true }));
|
app.use(bodyparser.urlencoded({ extended: true }));
|
||||||
app.use(cookie_parse(APP_KEY))
|
|
||||||
app.use(cookie_encrypt(APP_KEY));
|
|
||||||
app.use(session({
|
|
||||||
secret: APP_KEY,
|
|
||||||
resave: false,
|
|
||||||
saveUninitialized: true,
|
|
||||||
cookie: { secure: true },
|
|
||||||
store: new RedisStore({ client: redisClient })
|
|
||||||
}));
|
|
||||||
app.use(require('./middleware'));
|
app.use(require('./middleware'));
|
||||||
|
|
||||||
app.use(require('./routes'));
|
app.use(require('./routes'));
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
const express = require('express');
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
const cookie_parse = require('cookie-parser');
|
||||||
|
const cookie_encrypt = require('cookie-encrypter');
|
||||||
|
|
||||||
|
const { APP_KEY } = process.env;
|
||||||
|
|
||||||
|
router.use(cookie_parse(APP_KEY))
|
||||||
|
router.use(cookie_encrypt(APP_KEY));
|
||||||
|
|
||||||
|
module.exports = router
|
|
@ -2,6 +2,7 @@ const express = require('express');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const glob = require('glob');
|
const glob = require('glob');
|
||||||
|
|
||||||
|
router.use(require('./cookie'));
|
||||||
router.use(require('./session'));
|
router.use(require('./session'));
|
||||||
router.use(require('./session_back'));
|
router.use(require('./session_back'));
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,28 @@
|
||||||
|
|
||||||
|
const express = require('express');
|
||||||
|
const session = require('express-session');
|
||||||
|
const crypto = require('crypto');
|
||||||
|
const router = new express.Router();
|
||||||
|
const parser = require('cookie-parser');
|
||||||
|
|
||||||
const Redis = require("ioredis").Redis;
|
const Redis = require("ioredis").Redis;
|
||||||
/** @type {import('ioredis').Redis} */
|
/** @type {import('ioredis').Redis} */
|
||||||
const redis = new Redis(process.env.REDIS_PORT, process.env.REDIS_HOST);
|
const redis = new Redis(process.env.REDIS_PORT, process.env.REDIS_HOST);
|
||||||
|
const ConnectRedis = require('connect-redis')(session);
|
||||||
|
const store = new ConnectRedis({client: redis});
|
||||||
|
|
||||||
const express = require('express');
|
router.use(parser())
|
||||||
const router = express.Router();
|
router.use(session({
|
||||||
|
secret: process.env.APP_KEY,
|
||||||
async function session(req, res, next) {
|
store: store,
|
||||||
res.send(req.cookies);
|
cookie: {
|
||||||
return;
|
// secure cookies didnt work for some reason
|
||||||
|
secure: false
|
||||||
|
},
|
||||||
|
resave: true,
|
||||||
|
genid: (req) => {
|
||||||
|
return crypto.randomBytes(256).toString('base64');
|
||||||
}
|
}
|
||||||
|
}));
|
||||||
router.use(session);
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
Loading…
Reference in New Issue