import * as api from '../../client/src/api'; import { EndpointError, EndpointHandler, catchUnhandledErrors } from './types'; import Knex from 'knex'; import { sha512 } from 'js-sha512'; export const RegisterUserEndpointHandler: EndpointHandler = async (req: any, res: any, knex: Knex) => { if (!api.checkRegisterUserRequest(req)) { const e: EndpointError = { internalMessage: 'Invalid RegisterUser request: ' + JSON.stringify(req.body), httpStatus: 400 }; throw e; } const reqObject: api.RegisterUserRequest = req.body; console.log("Register User: ", reqObject); await knex.transaction(async (trx) => { try { // FIXME check if the user already exists // Create the new user. const passwordHash = sha512(reqObject.password); const userId = (await trx('users') .insert({ email: reqObject.email, passwordHash: passwordHash, }) .returning('id') // Needed for Postgres )[0]; // Respond to the request. res.status(200).send(); } catch (e) { catchUnhandledErrors(e); trx.rollback(); } }) }