126 lines
3.5 KiB
TypeScript
126 lines
3.5 KiB
TypeScript
/**
|
|
* Required External Modules and Interfaces
|
|
*/
|
|
|
|
import express, {Request, Response} from 'express';
|
|
import * as UserService from './users.service';
|
|
import {Session} from './session.interface';
|
|
import {User} from './user.interface';
|
|
import {Guid} from 'guid-typescript';
|
|
import logger from '../../../middleware/logger';
|
|
|
|
/**
|
|
* Router Definition
|
|
*/
|
|
|
|
export const usersRouter = express.Router();
|
|
|
|
|
|
/**
|
|
* Controller Definitions
|
|
*/
|
|
|
|
// POST users/register
|
|
usersRouter.post('/register', async (req: Request, res: Response) => {
|
|
try {
|
|
const password: string = req.body.password;
|
|
const email: string = req.body.email;
|
|
const fullName: string = req.body.fullName;
|
|
const ip: string = req.socket.remoteAddress ?? '';
|
|
|
|
if (!password || !email) {
|
|
// Missing
|
|
res.status(400).send(JSON.stringify({message: 'Missing parameters'}));
|
|
return;
|
|
}
|
|
|
|
// Create the user and a session
|
|
const session: Session = await UserService.createUser(email, password, fullName, ip);
|
|
|
|
// Send the session details back to the user
|
|
res.status(201).send({
|
|
sessionId: session.sessionId,
|
|
sessionKey: session.sessionKey
|
|
});
|
|
} catch (e: any) {
|
|
let errorGuid = Guid.create().toString();
|
|
logger.error('Error handling a request: ' + e.message, {reference: errorGuid});
|
|
res.status(500).send({
|
|
'status': 'PROCESSING_ERROR',
|
|
'message': 'Internal Server Error. Try again later.',
|
|
'reference': errorGuid
|
|
});
|
|
}
|
|
});
|
|
|
|
// POST users/login
|
|
usersRouter.post('/login', async (req: Request, res: Response) => {
|
|
try {
|
|
const password: string = req.body.password;
|
|
const email: string = req.body.email;
|
|
const ip: string = req.socket.remoteAddress ?? '';
|
|
|
|
if (!password || !email) {
|
|
// Missing
|
|
res.status(400).send(JSON.stringify({message: 'Missing parameters'}));
|
|
return;
|
|
}
|
|
|
|
// Create a session
|
|
const session: Session = await UserService.login(email, password, ip);
|
|
|
|
if (!session.sessionId) {
|
|
// Error logging in, probably wrong username / password
|
|
res.status(401).send(JSON.stringify({message: 'Wrong username and / or password', sessionId: -1, sessionKey: ''}));
|
|
return;
|
|
}
|
|
|
|
// Send the session details back to the user
|
|
res.status(200).send({
|
|
sessionId: session.sessionId,
|
|
sessionKey: session.sessionKey
|
|
});
|
|
} catch (e: any) {
|
|
let errorGuid = Guid.create().toString();
|
|
logger.error('Error handling a request: ' + e.message, {reference: errorGuid});
|
|
res.status(500).send({
|
|
'status': 'PROCESSING_ERROR',
|
|
'message': 'Internal Server Error. Try again later.',
|
|
'reference': errorGuid
|
|
});
|
|
}
|
|
});
|
|
|
|
// POST users/checkSessionValid
|
|
usersRouter.post('/checkSessionValid', async (req: Request, res: Response) => {
|
|
try {
|
|
const ip: string = req.socket.remoteAddress ?? '';
|
|
const session_id = req.body.sessionId;
|
|
const session_key = req.body.sessionKey;
|
|
|
|
if (!session_id || !session_key) {
|
|
// Error logging in, probably wrong username / password
|
|
res.status(401).send(JSON.stringify({messages: ['No session detected']}));
|
|
return;
|
|
}
|
|
|
|
const user: User = await UserService.checkSession(session_id, session_key, ip);
|
|
|
|
if (!user.userId) {
|
|
// Error logging in, probably wrong username / password
|
|
res.status(401).send(JSON.stringify({messages: ['Invalid session']}));
|
|
return;
|
|
}
|
|
|
|
res.status(200).send(user);
|
|
} catch (e: any) {
|
|
let errorGuid = Guid.create().toString();
|
|
logger.error('Error handling a request: ' + e.message, {reference: errorGuid});
|
|
res.status(500).send({
|
|
'status': 'PROCESSING_ERROR',
|
|
'message': 'Internal Server Error. Try again later.',
|
|
'reference': errorGuid
|
|
});
|
|
}
|
|
});
|