Endpoints are separated into distinct packages and interfaces are extracted into files Co-authored-by: Patrick Müller <patrick@mueller-patrick.tech> Reviewed-on: #12 Co-authored-by: Patrick Müller <patrick@plutodev.de> Co-committed-by: Patrick Müller <patrick@plutodev.de>
This commit was merged in pull request #12.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* Required External Modules and Interfaces
|
||||
*/
|
||||
import express, {Request, Response} from 'express';
|
||||
import * as SessionService from './session.service';
|
||||
import * as UserService from '../user/user.service';
|
||||
import logger from '../../../middleware/logger';
|
||||
|
||||
/**
|
||||
* Router Definition
|
||||
*/
|
||||
export const sessionRouter = express.Router();
|
||||
|
||||
sessionRouter.get('/:isDevCall', async (req: Request, res: Response) => {
|
||||
try {
|
||||
let userId = (req.query.userId ?? '').toString();
|
||||
let sessionId = (req.query.sessionId ?? '').toString();
|
||||
let sessionKey = (req.query.sessionKey ?? '').toString();
|
||||
let useDev: boolean = (req.params.isDevCall ?? '') === 'dev'; // TBD
|
||||
|
||||
if (userId === '' || sessionId === '' || sessionKey === '') {
|
||||
res.status(400).send({
|
||||
'status': 'WRONG_PARAMS',
|
||||
'message': 'Missing or wrong parameters'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (!await UserService.checkSession(useDev, userId, sessionId, sessionKey)) {
|
||||
res.status(403).send({
|
||||
'status': 'INVALID_SESSION',
|
||||
'message': 'The user or session could not be found or the session is invalid'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
let data = await SessionService.getSessionData(useDev, userId);
|
||||
|
||||
res.status(200).send(data);
|
||||
} catch (e) {
|
||||
logger.error('Error handling a request: ' + e.message);
|
||||
res.status(500).send({'message': 'Internal Server Error. Try again later.'});
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Used in the getSessionData method as return value
|
||||
*/
|
||||
export interface SessionData {
|
||||
sessionId: string;
|
||||
type: string;
|
||||
lastLogin: string;
|
||||
lastIp: string;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import * as dotenv from 'dotenv';
|
||||
import {SessionData} from './SessionData.interface';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const mariadb = require('mariadb');
|
||||
const prod_pool = mariadb.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.PARTYPLANER_PROD_DATABASE,
|
||||
connectionLimit: 5
|
||||
});
|
||||
const dev_pool = mariadb.createPool({
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.PARTYPLANER_DEV_DATABASE,
|
||||
connectionLimit: 5
|
||||
});
|
||||
|
||||
/**
|
||||
* 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;
|
||||
try {
|
||||
if (useDev) {
|
||||
conn = await dev_pool.getConnection();
|
||||
} else {
|
||||
conn = await prod_pool.getConnection();
|
||||
}
|
||||
|
||||
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 {
|
||||
if (conn) {
|
||||
conn.end();
|
||||
}
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user