BETTERZON-84: Adding service method to check if a session is valid (#37)

This commit is contained in:
Patrick
2021-05-03 16:29:25 +02:00
committed by GitHub
parent a42c7da9a5
commit 816036dbdf
3 changed files with 111 additions and 4 deletions
+29
View File
@@ -81,3 +81,32 @@ usersRouter.post('/login', async (req: Request, res: Response) => {
res.status(404).send(e.message);
}
});
// POST users/checkSessionValid
usersRouter.post('/checkSessionValid', async (req: Request, res: Response) => {
try {
const sessionId: string = req.body.sessionId;
const sessionKey: string = req.body.sessionKey;
const ip: string = req.connection.remoteAddress ?? '';
if (!sessionId || !sessionKey) {
// Missing
res.status(400).send(JSON.stringify({message: 'Missing parameters'}));
return;
}
// Update the user entry and create a session
const user: User = await UserService.checkSession(sessionId, sessionKey, ip);
if(!user.user_id) {
// Error logging in, probably wrong username / password
res.status(401).send(JSON.stringify({messages: ["Invalid session"], codes: [5]}));
return;
}
// Send the session details back to the user
res.status(201).send(user);
} catch (e) {
res.status(404).send(e.message);
}
});