Skip to content

Commit

Permalink
Cleanup & Session Support
Browse files Browse the repository at this point in the history
- Bug fixes/cleanup
- Now supporting sessions table
  • Loading branch information
TurtIeSocks committed Dec 9, 2020
1 parent 7dc7bf5 commit 92a87ae
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/configs/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"password": "pass123!",
"database": "manualdb",
"charset": "utf8mb4",
"sessionTable": "sessions",
"useFor": []
}
},
Expand Down
12 changes: 6 additions & 6 deletions src/data/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 = `
Expand All @@ -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 = `
Expand All @@ -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 = `
Expand All @@ -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':
Expand Down
21 changes: 12 additions & 9 deletions src/services/session-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
Expand All @@ -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 != ?
Expand Down

0 comments on commit 92a87ae

Please sign in to comment.