Skip to content

Commit

Permalink
Allow MongoDB credentials to be set
Browse files Browse the repository at this point in the history
This commit together with openstad/openstad-frontend#354 allows MongoDB to have authentication.

This commit introduces 3 new environment variables:
- MONGO_DB_USER
- MONGO_DB_PASSWORD
- MONGO_DB_AUTHSOURCE

If both `MONGO_DB_USER` and `MONGO_DB_PASSWORD` are set, they are added to the MongoDB connection string.
The `MONGO_DB_AUTHSOURCE` allows us to specify which database we are authenticating against. (see https://www.mongodb.com/docs/manual/reference/connection-string/#std-label-connections-connection-options)
  • Loading branch information
rudivanhierden committed Aug 23, 2022
1 parent c1e2b1f commit c21398a
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions app-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,19 @@ const sessionStore = new MySQLStore({
*/


const mongoCredentials = {
host: process.env.MONGO_DB_HOST || 'localhost',
port: process.env.MONGO_DB_PORT || 27017,
function getMongoDbConnectionString () {
const host = process.env.MONGO_DB_HOST || 'localhost';
const port = process.env.MONGO_DB_PORT || 27017;
const user = process.env.MONGO_DB_USER || '';
const password = process.env.MONGO_DB_PASSWORD || '';
const authSource = process.env.MONGO_DB_AUTHSOURCE || '';

const useAuth = user && password;

return `mongodb://${useAuth ? `${user}:${password}@` : ''}${host}:${port}/sessions${authSource ? `?authSource=${authSource}` : ''}`;
}

const url = 'mongodb://'+ mongoCredentials.host +':'+mongoCredentials.port+'/sessions';
const url = getMongoDbConnectionString();

const sessionStore = new MongoStore({
url: url,
Expand Down

0 comments on commit c21398a

Please sign in to comment.