Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cryptoJS = require ('crypto-js');
- import {MysqlError} from "mysql";
- export default function(app, database) {
- /* @TODO: build this route, analog to app.post('/user in server.ts */
- app.post('/register', function(req, res){
- const firstName: string = req.body.firstName;
- const lastName: string = req.body.lastName;
- const email: string = req.body.email;
- const password: string = cryptoJS.SHA512(req.body.password).toString();
- const companyRegistrationNo: string = req.body.companyRegistrationNo;
- if (firstName && lastName && email && password) {
- // Create database query and data
- const data: any = [
- email, firstName, lastName, password, companyRegistrationNo
- ];
- const selectQuery: string = 'SELECT * FROM nutzer WHERE email = ? OR company_registration_no = ?';
- database.query(selectQuery, data, (err: MysqlError, result: any) => {
- if (err) {
- // Query could not been executed
- res.status(200).send({
- message: 'user does not exist: ' + err,
- });
- } else {
- // The user already exists
- res.status(500).send({
- userId: result.insertId,
- message: 'user already exists',
- });
- }
- return;
- });
- const query: string = 'INSERT INTO nutzer (email, first_name, last_name, password, companyRegistrationNo) ' +
- 'VALUES (?, ?, ?, ?, ?);';
- // Execute database query
- database.query(query, data, (err: MysqlError, result: any) => {
- if (err) {
- // Query could not been executed
- res.status(500).send({
- message: 'Database request failed: ' + err,
- });
- } else {
- // The user was created
- res.status(200).send({
- userId: result.insertId,
- message: 'Successfully created new user',
- });
- }
- });
- } else {
- res.status(400).send({
- message: 'Not all mandatory fields are filled in',
- });
- }
- }
- );
- //other routes..
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement