-
Notifications
You must be signed in to change notification settings - Fork 599
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #497 from timeoff-management/tom-xxx-redis-as-sess…
…ion-store Introduce Redis as alternative session storage.
- Loading branch information
Showing
7 changed files
with
130 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# How to use Redis as storage for Sessions | ||
|
||
By default application uses its database as a storage for session data (`Sessions` table). | ||
|
||
It is possible to use different storage mechanism for Sessions data: [Redis](https://redis.io/). | ||
|
||
## Steps | ||
|
||
* Ensure the application's source is at least `1.4.0` | ||
* Stop the application | ||
* Open `config/app.json` for editing | ||
* Update `sessionStore.useRedis` section to be `true` | ||
* Update `sessionStore.redisConnectionConfiguration`'s `host` and `port` pointing to corresponding instance of Redis | ||
* Save the configuration file | ||
* Restart the application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
const session = require('express-session'); | ||
const SequelizeStore = require('connect-session-sequelize')(session.Store); | ||
|
||
const redis = require('redis'); | ||
const connectRedis = require('connect-redis'); | ||
|
||
const {sessionStore: sessionStoreConfig} = require(__dirname + '/../../config/app.json') || {}; | ||
|
||
const createSessionMiddleware = ({ | ||
sequelizeDb, | ||
}) => { | ||
let store; | ||
|
||
if (sessionStoreConfig && sessionStoreConfig.useRedis) { | ||
const RedisStore = connectRedis(session); | ||
const {redisConnectionConfiguration = {}} = sessionStoreConfig; | ||
const {host, port} = redisConnectionConfiguration; | ||
if (!(host && port)) { | ||
throw new Error('Missing configuration for Redis to use with Sessions'); | ||
} | ||
const redisClient = redis.createClient({ host, port }); | ||
|
||
redisClient.on('error', function (err) { | ||
throw new Error(`Failed to connect to Redis: ${err}`); | ||
}); | ||
redisClient.on('connect', function (err) { | ||
console.log('Connected to redis successfully'); | ||
}); | ||
|
||
store = new RedisStore({ client: redisClient }); | ||
} else { | ||
if (!sequelizeDb) { | ||
throw new Error('Database connection was not provided into Session store manager!'); | ||
} | ||
store = new SequelizeStore({ db: sequelizeDb }); | ||
} | ||
|
||
return session({ | ||
store, | ||
secret: 'my dirty secret ;khjsdkjahsdajhasdam,nnsnad,', | ||
resave: false, | ||
saveUninitialized: false, | ||
}); | ||
}; | ||
|
||
module.exports = createSessionMiddleware; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters