37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
import * as dotenv from 'dotenv';
|
|
import {SessionData} from './SessionData.interface';
|
|
import {PartyPlanerDB} from '../PartyPlaner.db';
|
|
|
|
dotenv.config();
|
|
|
|
/**
|
|
* Returns all active sessions of the given user
|
|
* @param useDev If the dev or prod database should be used
|
|
* @param userId The userId of the user to return the sessions for
|
|
* @return SessionData[] A list containing objects with the session data
|
|
*/
|
|
export const getSessionData = async (useDev: boolean, userId: string): Promise<SessionData[]> => {
|
|
let conn = await PartyPlanerDB.getConnection(useDev);
|
|
try {
|
|
let rows = await conn.query('SELECT session_id, type, last_login, last_ip FROM sessions WHERE user_id = ? AND valid_until > NOW()', userId);
|
|
|
|
let sessions: SessionData[] = [];
|
|
|
|
for (let row of rows) {
|
|
sessions.push({
|
|
sessionId: row.session_id,
|
|
type: row.type,
|
|
lastLogin: row.last_login,
|
|
lastIp: row.last_ip
|
|
});
|
|
}
|
|
|
|
return sessions;
|
|
} catch (err) {
|
|
throw err;
|
|
} finally {
|
|
// Return connection
|
|
await conn.end();
|
|
}
|
|
};
|