Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BackEndMysqlMVC/server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
75 changes: 75 additions & 0 deletions BackEndMysqlMVC/server/Controller/authController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
const User = require('../Models/User')

const authController = {
SignUp: async (req, res) => {
// console.log(req.body)
const {
username,
email,
password,
} = req.body;

User.findByUsernameOrEmail(username, email, async (err, result) => {
if(err) throw err

if(result.length > 0){
return res.json({ Error: "User is Already Exists" })
}
else{
const hashPass = await bcrypt.hash(password, 10)
// console.log(hashPass)
if(hashPass){
const newUser = User.create({
username: username,
email: email,
password: hashPass,
role: "User",
create_at: new Date(),
is_active: 1,
is_lock: 0
})

return res.json({ Status: "Success" })
}
else{
return res.json({ Error: "Error White Hashing Password" })
}
}
})
},
SignIn: (req, res) => {
// console.log(req.body)

const {
email,
password
} = req.body;

User.findByEmail(email, (err, result) => {
if(err) throw err

if(result.length === 0){
return res.json({ Error: "User not Found,..." })
}

const person = result[0]

bcrypt.compare(password, person.password, (err, passMatch) => {
if(err) throw err

if(!passMatch) {
return res.json({ Error: "Password not Match" })
}
else{
const token = jwt.sign({ email: person.email }, 'your_jwt_secret', { expiresIn: '1h' })
return res.json({ Msg: "Success", Token:token, LoginUser:result })
}
})
})

}
}

module.exports = authController;
17 changes: 17 additions & 0 deletions BackEndMysqlMVC/server/Models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const connection = require('../config/connection')

const User = {
create: (userData, callback) => {
connection.query('INSERT INTO users SET ?', userData, callback);
},
findByUsernameOrEmail: (username, email, callback) => {
const query = 'SELECT * FROM users WHERE username = ? OR email = ?';
connection.execute(query, [username, email], callback);
},
findByEmail: (email, callback) => {
const query = 'SELECT * FROM users WHERE email = ?';
connection.execute(query, [email], callback);
}
}

module.exports = User
9 changes: 9 additions & 0 deletions BackEndMysqlMVC/server/Routes/authRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express')
const authController = require('../Controller/authController')

const router = express.Router()

router.post('/SignUp', authController.SignUp);
router.post('/SignIn', authController.SignIn);

module.exports = router
15 changes: 15 additions & 0 deletions BackEndMysqlMVC/server/config/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const JkMysql = require('jkmysql-easy');

// make connection between server and mysql database
// change `yourDB` to your Database you use in Mysql
const connection = JkMysql.ConnectToDatabase('localhost', 'root', '1234', 'yourDB')

connection.connect(err => {
if (err) {
console.error('Error connecting to the database:', err);
return;
}
console.log('Connected to the database.');
});

module.exports = connection
Loading