working mysql services
This commit is contained in:
@@ -0,0 +1,2 @@
|
|||||||
|
need to create tables in mysql-auth using cli
|
||||||
|
id, username, password
|
||||||
+22
-23
@@ -1,47 +1,46 @@
|
|||||||
const express = require("express");
|
const express = require("express");
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const bcrypt = require("bcrypt");
|
const bcrypt = require("bcrypt");
|
||||||
const { authDb } = require("../../config/authDb");
|
const authDb = require("../../config/authDb");
|
||||||
|
|
||||||
// User registration endpoint
|
// User registration endpoint
|
||||||
router.post("/", async (req, res) => {
|
router.post("/", (req, res) => {
|
||||||
const { username, password } = req.body;
|
const { username, password } = req.body;
|
||||||
|
|
||||||
if (!username || !password) {
|
if (!username || !password) {
|
||||||
return res.status(400).send("Username and password are required");
|
return res.status(400).json({ message: "Username and password are required" });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the username already exists
|
// Check if the username already exists
|
||||||
const sqlCheck = "SELECT * FROM tbl_user WHERE username = ?";
|
const sqlCheck = "SELECT * FROM popcenauth.tbl_user WHERE username = ?";
|
||||||
authDb.query(sqlCheck, [username], async (err, results) => {
|
authDb.query(sqlCheck, [username], (err, results) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err);
|
console.error("Database error:", err);
|
||||||
return res.status(500).send("Server error");
|
return res.status(500).json({ message: "Server error" });
|
||||||
}
|
}
|
||||||
|
|
||||||
if (results.length > 0) {
|
if (results.length > 0) {
|
||||||
return res.status(400).send("Username already exists");
|
return res.status(400).json({ message: "Username already exists" });
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
// Hash the password using bcrypt
|
||||||
// Hash the password using bcrypt
|
const saltRounds = 10;
|
||||||
const saltRounds = 10;
|
bcrypt.hash(password, saltRounds, (hashErr, hashedPassword) => {
|
||||||
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
if (hashErr) {
|
||||||
|
console.error("Hashing error:", hashErr);
|
||||||
|
return res.status(500).json({ message: "Error registering user" });
|
||||||
|
}
|
||||||
|
|
||||||
// Insert the new user into the database with the hashed password
|
const sqlInsert = "INSERT INTO popcenauth.tbl_user (username, password) VALUES (?, ?)";
|
||||||
const sqlInsert = "INSERT INTO tbl_user (username, password) VALUES (?, ?)";
|
authDb.query(sqlInsert, [username, hashedPassword], (insertErr) => {
|
||||||
authDb.query(sqlInsert, [username, hashedPassword], (err, result) => {
|
if (insertErr) {
|
||||||
if (err) {
|
console.error("Error inserting user:", insertErr);
|
||||||
console.error(err);
|
return res.status(500).json({ message: "Server error" });
|
||||||
return res.status(500).send("Server error");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(201).send("User registered successfully");
|
res.status(201).json({ message: "User registered successfully" });
|
||||||
});
|
});
|
||||||
} catch (error) {
|
});
|
||||||
console.error(error);
|
|
||||||
res.status(500).send("Error registering user");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+3
-10
@@ -2,44 +2,37 @@ const express = require('express');
|
|||||||
const bcrypt = require('bcrypt');
|
const bcrypt = require('bcrypt');
|
||||||
const jwt = require('jsonwebtoken');
|
const jwt = require('jsonwebtoken');
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
const { authDb } = require("../../config/authDb");
|
const authDb = require("../../config/authDb");
|
||||||
|
|
||||||
// User login endpoint
|
// User login endpoint
|
||||||
router.post("/login", (req, res) => {
|
router.post("/login", (req, res) => {
|
||||||
const { username, password } = req.body;
|
const { username, password } = req.body;
|
||||||
|
|
||||||
if (!username || !password) {
|
if (!username || !password) {
|
||||||
return res.status(400).send("Username and password are required");
|
return res.status(400).send("Username and password are required");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check the current database
|
// Check the current database
|
||||||
authDb.query("SELECT DATABASE();", (err, results) => {
|
authDb.query("SELECT DATABASE();", (err, results) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
return res.status(500).send("Error retrieving database name");
|
return res.status(500).send("Error retrieving database name");
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Current database:", results[0]['DATABASE()']); // Log the current database
|
console.log("Current database:", results[0]['DATABASE()']); // Log the current database
|
||||||
|
const sql = "SELECT * FROM popcenauth.tbl_user WHERE username = ?";
|
||||||
const sql = "SELECT * FROM tbl_user WHERE username = ?";
|
|
||||||
authDb.query(sql, [username], async (err, results) => {
|
authDb.query(sql, [username], async (err, results) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
return res.status(500).send("Server error");
|
return res.status(500).send("Server error");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (results.length === 0) {
|
if (results.length === 0) {
|
||||||
return res.status(401).send("Invalid credentials");
|
return res.status(401).send("Invalid credentials");
|
||||||
}
|
}
|
||||||
|
|
||||||
const user = results[0];
|
const user = results[0];
|
||||||
|
|
||||||
// Compare provided password with the hashed password in the database
|
// Compare provided password with the hashed password in the database
|
||||||
const isMatch = await bcrypt.compare(password, user.password);
|
const isMatch = await bcrypt.compare(password, user.password);
|
||||||
|
|
||||||
if (isMatch) {
|
if (isMatch) {
|
||||||
let jwtSecretKey = process.env.JWT_SECRET_KEY || "defaultSecretKey";
|
let jwtSecretKey = process.env.JWT_SECRET_KEY || "defaultSecretKey";
|
||||||
let data = { userId: user.id, username: user.username };
|
let data = { userId: user.id, username: user.username };
|
||||||
|
|
||||||
// Create JWT token
|
// Create JWT token
|
||||||
const token = jwt.sign(data, jwtSecretKey, { expiresIn: "1d" });
|
const token = jwt.sign(data, jwtSecretKey, { expiresIn: "1d" });
|
||||||
return res.json({ token });
|
return res.json({ token });
|
||||||
|
|||||||
-32
@@ -1,32 +0,0 @@
|
|||||||
# For advice on how to change settings please see
|
|
||||||
# http://dev.mysql.com/doc/refman/9.0/en/server-configuration-defaults.html
|
|
||||||
|
|
||||||
[mysqld]
|
|
||||||
#
|
|
||||||
# Remove leading # and set to the amount of RAM for the most important data
|
|
||||||
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
|
|
||||||
# innodb_buffer_pool_size = 128M
|
|
||||||
#
|
|
||||||
# Remove leading # to turn on a very important data integrity option: logging
|
|
||||||
# changes to the binary log between backups.
|
|
||||||
# log_bin
|
|
||||||
#
|
|
||||||
# Remove leading # to set options mainly useful for reporting servers.
|
|
||||||
# The server defaults are faster for transactions and fast SELECTs.
|
|
||||||
# Adjust sizes as needed, experiment to find the optimal values.
|
|
||||||
# join_buffer_size = 128M
|
|
||||||
# sort_buffer_size = 2M
|
|
||||||
# read_rnd_buffer_size = 2M
|
|
||||||
|
|
||||||
host-cache-size=0
|
|
||||||
skip-name-resolve
|
|
||||||
datadir=/var/lib/mysql
|
|
||||||
socket=/var/run/mysqld/mysqld.sock
|
|
||||||
secure-file-priv=/var/lib/mysql-files
|
|
||||||
user=root
|
|
||||||
|
|
||||||
pid-file=/var/run/mysqld/mysqld.pid
|
|
||||||
[client]
|
|
||||||
socket=/var/run/mysqld/mysqld.sock
|
|
||||||
|
|
||||||
!includedir /etc/mysql/conf.d/
|
|
||||||
+6
-4
@@ -14,8 +14,8 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
#- D:/Projects/AuthenticatedAPIDocker/app/mysql/etc/mysql/mysqlauth/etc:/etc
|
#- D:/Projects/AuthenticatedAPIDocker/app/mysql/etc/mysql/mysqlauth/etc:/etc
|
||||||
- D:/Projects/AuthenticatedAPIDocker/app/mysql/mysqldata_auth:/var/lib/mysql
|
- D:/Projects/AuthenticatedAPIDocker/app/mysql/mysqldata_auth:/var/lib/mysql
|
||||||
extra_hosts:
|
# extra_hosts:
|
||||||
- "host.docker.internal:host-gateway"
|
# - "host.docker.internal:host-gateway"
|
||||||
|
|
||||||
# Data Database
|
# Data Database
|
||||||
mysql-data:
|
mysql-data:
|
||||||
@@ -45,12 +45,14 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
# Auth DB connection
|
# Auth DB connection
|
||||||
AUTH_DB_HOST: mysql-auth
|
AUTH_DB_HOST: mysql-auth
|
||||||
AUTH_DB_USER: admin
|
AUTH_DB_USER: root
|
||||||
|
#AUTH_DB_PORT: 3306
|
||||||
AUTH_DB_NAME: popcenauth
|
AUTH_DB_NAME: popcenauth
|
||||||
|
|
||||||
# Data DB connection
|
# Data DB connection
|
||||||
DATA_DB_HOST: mysql-data
|
DATA_DB_HOST: mysql-data
|
||||||
DATA_DB_USER: admin
|
#DATA_DB_PORT: 3306
|
||||||
|
DATA_DB_USER: root
|
||||||
DATA_DB_NAME: popcen
|
DATA_DB_NAME: popcen
|
||||||
|
|
||||||
depends_on:
|
depends_on:
|
||||||
|
|||||||
Reference in New Issue
Block a user