diff --git a/Sean/models/User.js b/Sean/models/User.js new file mode 100644 index 0000000..ab12ee9 --- /dev/null +++ b/Sean/models/User.js @@ -0,0 +1,35 @@ +// models/User.js + +const { Sequelize, DataTypes } = require('sequelize'); +const sequelize = new Sequelize(process.env.database, process.env.user, process.env.password, { + host: process.env.host, + dialect: 'mysql', + timezone: 'Z', // Set the timezone to UTC +}); + +const User = sequelize.define('User', { + name: { + type: DataTypes.STRING, + allowNull: false, + }, + username: { + type: DataTypes.STRING, + allowNull: false, + unique: true, + }, + email: { + type: DataTypes.STRING, + allowNull: false, + unique: true, + }, + password: { + type: DataTypes.STRING, + allowNull: false, + }, + jobTitle: { + type: DataTypes.STRING, + allowNull: false, + }, +}); + +module.exports = User; diff --git a/Sean/server.js b/Sean/server.js index c1c05e5..ee110ca 100644 --- a/Sean/server.js +++ b/Sean/server.js @@ -2,6 +2,8 @@ const express = require('express'); const session = require('express-session'); const mysql = require('mysql'); const bodyParser = require('body-parser'); +const bcrypt = require('bcrypt'); + const app = express(); const PORT = process.env.PORT || 3000; @@ -16,6 +18,13 @@ const mysqlConfig = { }; const mysqlConnection = mysql.createConnection(mysqlConfig); +mysqlConnection.connect((err) => { + if (err) { + console.error('Error connecting to MySQL:', err); + return; + } + console.log('Connected to MySQL'); +}); app.use(bodyParser.urlencoded({ extended: true })); app.use(session({ secret: 'your_session_secret', resave: false, saveUninitialized: true })); @@ -119,6 +128,64 @@ app.get('/inusers', isAuthenticated, (req, res) => { }); }); +app.post('/createUser', async (req, res) => { + try { + const { name, username, email, password, jobTitle } = req.body; + + // Hash the password using bcrypt + const hashedPassword = await bcrypt.hash(password, 10); + + // Start a transaction + mysqlConnection.beginTransaction((transactionErr) => { + if (transactionErr) { + console.error('Error starting transaction:', transactionErr); + res.status(500).json({ error: 'Internal Server Error' }); + return; + } + + // Define the insert query + const insertUserQuery = 'INSERT INTO users (name, username, email, password, lastLogin, jobTitle) VALUES (?, ?, ?, ?, NULL, ?)'; + + // Log the query and its parameters + console.log('Insert Query:', insertUserQuery); + console.log('Query Parameters:', [name, username, email, hashedPassword, jobTitle]); + + // Execute the query with user data + mysqlConnection.query(insertUserQuery, [name, username, email, hashedPassword, jobTitle], (queryErr, results) => { + if (queryErr) { + console.error('Error executing query:', queryErr); + + // Rollback the transaction in case of an error + mysqlConnection.rollback((rollbackErr) => { + if (rollbackErr) { + console.error('Error rolling back transaction:', rollbackErr); + } + res.status(500).json({ error: 'Internal Server Error' }); + }); + return; + } + + // Commit the transaction + mysqlConnection.commit((commitErr) => { + if (commitErr) { + console.error('Error committing transaction:', commitErr); + res.status(500).json({ error: 'Internal Server Error' }); + return; + } + + // Log the results of the query + console.log('Query Results:', results); + + // Respond with a success message + res.status(201).json({ message: 'User created successfully' }); + }); + }); + }); + } catch (error) { + console.error('Error creating user:', error); + res.status(500).json({ error: 'Internal Server Error' }); + } +}); diff --git a/Sean/views/inusers.ejs b/Sean/views/inusers.ejs index 1a8b321..2c016e4 100644 --- a/Sean/views/inusers.ejs +++ b/Sean/views/inusers.ejs @@ -9,6 +9,9 @@