Skip to content

Commit 323d0e3

Browse files
Merge pull request #6 from TeamProjectsReact/v4.0.0-Updating-ver1
V4.0.0 updating ver1
2 parents 65010bd + cb97eb2 commit 323d0e3

File tree

10 files changed

+1614
-7
lines changed

10 files changed

+1614
-7
lines changed

BackEndMysqlMVC/server/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const bcrypt = require('bcryptjs');
2+
const jwt = require('jsonwebtoken');
3+
const User = require('../Models/User')
4+
5+
const authController = {
6+
SignUp: async (req, res) => {
7+
// console.log(req.body)
8+
const {
9+
username,
10+
email,
11+
password,
12+
} = req.body;
13+
14+
User.findByUsernameOrEmail(username, email, async (err, result) => {
15+
if(err) throw err
16+
17+
if(result.length > 0){
18+
return res.json({ Error: "User is Already Exists" })
19+
}
20+
else{
21+
const hashPass = await bcrypt.hash(password, 10)
22+
// console.log(hashPass)
23+
if(hashPass){
24+
const newUser = User.create({
25+
username: username,
26+
email: email,
27+
password: hashPass,
28+
role: "User",
29+
create_at: new Date(),
30+
is_active: 1,
31+
is_lock: 0
32+
})
33+
34+
return res.json({ Status: "Success" })
35+
}
36+
else{
37+
return res.json({ Error: "Error White Hashing Password" })
38+
}
39+
}
40+
})
41+
},
42+
SignIn: (req, res) => {
43+
// console.log(req.body)
44+
45+
const {
46+
email,
47+
password
48+
} = req.body;
49+
50+
User.findByEmail(email, (err, result) => {
51+
if(err) throw err
52+
53+
if(result.length === 0){
54+
return res.json({ Error: "User not Found,..." })
55+
}
56+
57+
const person = result[0]
58+
59+
bcrypt.compare(password, person.password, (err, passMatch) => {
60+
if(err) throw err
61+
62+
if(!passMatch) {
63+
return res.json({ Error: "Password not Match" })
64+
}
65+
else{
66+
const token = jwt.sign({ email: person.email }, 'your_jwt_secret', { expiresIn: '1h' })
67+
return res.json({ Msg: "Success", Token:token, LoginUser:result })
68+
}
69+
})
70+
})
71+
72+
}
73+
}
74+
75+
module.exports = authController;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const connection = require('../config/connection')
2+
3+
const User = {
4+
create: (userData, callback) => {
5+
connection.query('INSERT INTO users SET ?', userData, callback);
6+
},
7+
findByUsernameOrEmail: (username, email, callback) => {
8+
const query = 'SELECT * FROM users WHERE username = ? OR email = ?';
9+
connection.execute(query, [username, email], callback);
10+
},
11+
findByEmail: (email, callback) => {
12+
const query = 'SELECT * FROM users WHERE email = ?';
13+
connection.execute(query, [email], callback);
14+
}
15+
}
16+
17+
module.exports = User
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const express = require('express')
2+
const authController = require('../Controller/authController')
3+
4+
const router = express.Router()
5+
6+
router.post('/SignUp', authController.SignUp);
7+
router.post('/SignIn', authController.SignIn);
8+
9+
module.exports = router
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const JkMysql = require('jkmysql-easy');
2+
3+
// make connection between server and mysql database
4+
// change `yourDB` to your Database you use in Mysql
5+
const connection = JkMysql.ConnectToDatabase('localhost', 'root', '1234', 'yourDB')
6+
7+
connection.connect(err => {
8+
if (err) {
9+
console.error('Error connecting to the database:', err);
10+
return;
11+
}
12+
console.log('Connected to the database.');
13+
});
14+
15+
module.exports = connection

0 commit comments

Comments
 (0)