mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 08:12:00 +02:00

The `express-session` library comes with a session storage meant for testing by default. That is why you get a message like this when you start up LibreChat with OIDC enabled: Warning: connect.session() MemoryStore is not designed for a production environment, as it will leak memory, and will not scale past a single process. LibreChat can already use Redis as a session storage, although Redis support is still marked as experimental. It also makes the set-up more complex, since you will need to configure and run yet another service. This pull request provides a simple alternative by using a in-memory session store marked as a production-ready alternative by the guys from `express-session`¹. You can still configure Redis, but this provides a simple, good default for everyone else. See also https://github.com/danny-avila/LibreChat/discussions/1014 ¹⁾ https://github.com/expressjs/session?tab=readme-ov-file#compatible-session-stores
63 lines
1.9 KiB
JavaScript
63 lines
1.9 KiB
JavaScript
const Redis = require('ioredis');
|
|
const passport = require('passport');
|
|
const session = require('express-session');
|
|
const MemoryStore = require('memorystore')(session)
|
|
const RedisStore = require('connect-redis').default;
|
|
const {
|
|
setupOpenId,
|
|
googleLogin,
|
|
githubLogin,
|
|
discordLogin,
|
|
facebookLogin,
|
|
} = require('~/strategies');
|
|
const { isEnabled } = require('~/server/utils');
|
|
const { logger } = require('~/config');
|
|
|
|
/**
|
|
*
|
|
* @param {Express.Application} app
|
|
*/
|
|
const configureSocialLogins = (app) => {
|
|
if (process.env.GOOGLE_CLIENT_ID && process.env.GOOGLE_CLIENT_SECRET) {
|
|
passport.use(googleLogin());
|
|
}
|
|
if (process.env.FACEBOOK_CLIENT_ID && process.env.FACEBOOK_CLIENT_SECRET) {
|
|
passport.use(facebookLogin());
|
|
}
|
|
if (process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) {
|
|
passport.use(githubLogin());
|
|
}
|
|
if (process.env.DISCORD_CLIENT_ID && process.env.DISCORD_CLIENT_SECRET) {
|
|
passport.use(discordLogin());
|
|
}
|
|
if (
|
|
process.env.OPENID_CLIENT_ID &&
|
|
process.env.OPENID_CLIENT_SECRET &&
|
|
process.env.OPENID_ISSUER &&
|
|
process.env.OPENID_SCOPE &&
|
|
process.env.OPENID_SESSION_SECRET
|
|
) {
|
|
const sessionOptions = {
|
|
secret: process.env.OPENID_SESSION_SECRET,
|
|
resave: false,
|
|
saveUninitialized: false,
|
|
};
|
|
if (isEnabled(process.env.USE_REDIS)) {
|
|
const client = new Redis(process.env.REDIS_URI);
|
|
client
|
|
.on('error', (err) => logger.error('ioredis error:', err))
|
|
.on('ready', () => logger.info('ioredis successfully initialized.'))
|
|
.on('reconnecting', () => logger.info('ioredis reconnecting...'));
|
|
sessionOptions.store = new RedisStore({ client, prefix: 'librechat' });
|
|
} else {
|
|
sessionOptions.store = new MemoryStore({
|
|
checkPeriod: 86400000 // prune expired entries every 24h
|
|
})
|
|
}
|
|
app.use(session(sessionOptions));
|
|
app.use(passport.session());
|
|
setupOpenId();
|
|
}
|
|
};
|
|
|
|
module.exports = configureSocialLogins;
|