Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const userRoute = express.Router();
- const cors = require('cors');
- const bodyParser = require('body-parser');
- const jwt = require('jsonwebtoken');
- const saltRounds = 15;
- const bcrypt = require('bcrypt');
- const users = require('../models').users;
- userRoute.use(function timeLog (req, res, next) {
- console.log('Time: ', Date.now());
- next();
- });
- userRoute.use(cors());
- userRoute.use(bodyParser.json());
- userRoute.use(bodyParser.urlencoded({ extended: true}));
- userRoute.post('/register', (req, res) => {
- users.findAll({
- attributes: ['username'],
- where: {
- username: req.body.username
- }
- }).then((data) => {
- if(data == '') {
- bcrypt.hash(req.body.password, saltRounds, (err, hash) => {
- if(err != undefined) {
- console.log(err);
- res.status(400).send("There was an issue while creating the user. Please see try again or consult with a system administrator");
- } else {
- users.create({
- username: req.body.username,
- password: hash
- }).then((err, data) => {
- if(data != '') {
- res.send("User successfully created");
- } else {
- console.log(err);
- res.status(400).send("There was an issue while creating your username. Please try again.");
- }
- })
- }
- });
- } else {
- console.log("Username already exists");
- res.status(400).send("Username already exists. Please try again.");
- }
- });
- });
- module.exports = userRoute;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement