diff --git a/src/configs/default.json b/src/configs/default.json index 872d40b9..b930c953 100644 --- a/src/configs/default.json +++ b/src/configs/default.json @@ -106,6 +106,7 @@ "password": "pass123!", "database": "manualdb", "charset": "utf8mb4", + "sessionTable": "sessions", "useFor": [] } }, diff --git a/src/data/map.js b/src/data/map.js index 8db54573..f23677a6 100644 --- a/src/data/map.js +++ b/src/data/map.js @@ -1236,7 +1236,7 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM pokestop WHERE ${conditions.join(' OR ') || 'FALSE'} `; - useDb = dbSelection('pokestop'); + useDb = 'pokestop'; break; case 'search-nest': let ids = getPokemonIdsByName(sanitizedValue); @@ -1259,7 +1259,7 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM nests WHERE LOWER(name) LIKE '%${sanitizedValue}%' ${pokemonSQL} `; - useDb = dbSelection('nest'); + useDb = 'nest'; break; case 'search-portal': sql = ` @@ -1268,7 +1268,7 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM ingress_portals WHERE LOWER(name) LIKE '%${sanitizedValue}%' `; - useDb = dbSelection('portal'); + useDb = 'portal'; break; case 'search-gym': sql = ` @@ -1277,7 +1277,7 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM gym WHERE LOWER(name) LIKE '%${sanitizedValue}%' `; - useDb = dbSelection('gym'); + useDb = 'gym'; break; case 'search-pokestop': sql = ` @@ -1286,11 +1286,11 @@ const getSearchData = async (lat, lon, id, value, iconStyle) => { FROM pokestop WHERE LOWER(name) LIKE '%${sanitizedValue}%' `; - useDb = dbSelection('pokestop'); + useDb = 'pokestop'; break; } sql += ` ORDER BY distance LIMIT ${config.searchMaxResults || 20}`; - let results = useDb.query(sql, args); + let results = await dbSelection(useDb).query(sql, args); if (results && results.length > 0) { switch (id) { case 'search-reward': diff --git a/src/services/session-store.js b/src/services/session-store.js index 069cad46..33ff35b2 100644 --- a/src/services/session-store.js +++ b/src/services/session-store.js @@ -5,20 +5,23 @@ const MySQLStore = require('express-mysql-session')(session); const config = require('../services/config.js'); const MySQLConnector = require('../services/mysql.js'); -const db = new MySQLConnector(config.db.scanner); + +const { scanner, manualdb } = config.db; +const dbSelection = scanner.useFor.includes('session') ? scanner : manualdb; +const db = new MySQLConnector(dbSelection); // MySQL session store const sessionStore = new MySQLStore({ // Database server IP address/hostname - host: config.db.scanner.host, + host: dbSelection.host, // Database server listening port - port: config.db.scanner.port, + port: dbSelection.port, // Database username - user: config.db.scanner.username, + user: dbSelection.username, // Password for the above database user - password: config.db.scanner.password, + password: dbSelection.password, // Database name to save sessions table to - database: config.db.scanner.database, + database: dbSelection.database, // Whether or not to automatically check for and clear expired sessions: clearExpired: true, // How frequently expired sessions will be cleared; milliseconds: @@ -27,14 +30,14 @@ const sessionStore = new MySQLStore({ createDatabaseTable: true, // Set Sessions table name schema: { - tableName: config.db.scanner.sessionTable + tableName: dbSelection.sessionTable } }); const isValidSession = async (userId) => { let sql = ` SELECT session_id - FROM ${config.db.scanner.sessionTable} + FROM ${dbSelection.sessionTable} WHERE json_extract(data, '$.user_id') = ? AND expires >= UNIX_TIMESTAMP() @@ -46,7 +49,7 @@ const isValidSession = async (userId) => { const clearOtherSessions = async (userId, currentSessionId) => { let sql = ` - DELETE FROM ${config.db.scanner.sessionTable} + DELETE FROM ${dbSelection.sessionTable} WHERE json_extract(data, '$.user_id') = ? AND session_id != ?