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 router = express.Router();
|
||||
const bcrypt = require("bcrypt");
|
||||
const { authDb } = require("../../config/authDb");
|
||||
const authDb = require("../../config/authDb");
|
||||
|
||||
// User registration endpoint
|
||||
router.post("/", async (req, res) => {
|
||||
router.post("/", (req, res) => {
|
||||
const { username, password } = req.body;
|
||||
|
||||
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
|
||||
const sqlCheck = "SELECT * FROM tbl_user WHERE username = ?";
|
||||
authDb.query(sqlCheck, [username], async (err, results) => {
|
||||
const sqlCheck = "SELECT * FROM popcenauth.tbl_user WHERE username = ?";
|
||||
authDb.query(sqlCheck, [username], (err, results) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.status(500).send("Server error");
|
||||
console.error("Database error:", err);
|
||||
return res.status(500).json({ message: "Server error" });
|
||||
}
|
||||
|
||||
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
|
||||
const saltRounds = 10;
|
||||
const hashedPassword = await bcrypt.hash(password, saltRounds);
|
||||
// Hash the password using bcrypt
|
||||
const saltRounds = 10;
|
||||
bcrypt.hash(password, saltRounds, (hashErr, hashedPassword) => {
|
||||
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 tbl_user (username, password) VALUES (?, ?)";
|
||||
authDb.query(sqlInsert, [username, hashedPassword], (err, result) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.status(500).send("Server error");
|
||||
const sqlInsert = "INSERT INTO popcenauth.tbl_user (username, password) VALUES (?, ?)";
|
||||
authDb.query(sqlInsert, [username, hashedPassword], (insertErr) => {
|
||||
if (insertErr) {
|
||||
console.error("Error inserting user:", insertErr);
|
||||
return res.status(500).json({ message: "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 jwt = require('jsonwebtoken');
|
||||
const router = express.Router();
|
||||
const { authDb } = require("../../config/authDb");
|
||||
const authDb = require("../../config/authDb");
|
||||
|
||||
// User login endpoint
|
||||
router.post("/login", (req, res) => {
|
||||
const { username, password } = req.body;
|
||||
|
||||
if (!username || !password) {
|
||||
return res.status(400).send("Username and password are required");
|
||||
}
|
||||
|
||||
// Check the current database
|
||||
authDb.query("SELECT DATABASE();", (err, results) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.status(500).send("Error retrieving database name");
|
||||
}
|
||||
|
||||
console.log("Current database:", results[0]['DATABASE()']); // Log the current database
|
||||
|
||||
const sql = "SELECT * FROM tbl_user WHERE username = ?";
|
||||
const sql = "SELECT * FROM popcenauth.tbl_user WHERE username = ?";
|
||||
authDb.query(sql, [username], async (err, results) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.status(500).send("Server error");
|
||||
}
|
||||
|
||||
if (results.length === 0) {
|
||||
return res.status(401).send("Invalid credentials");
|
||||
}
|
||||
|
||||
const user = results[0];
|
||||
|
||||
// Compare provided password with the hashed password in the database
|
||||
const isMatch = await bcrypt.compare(password, user.password);
|
||||
|
||||
if (isMatch) {
|
||||
let jwtSecretKey = process.env.JWT_SECRET_KEY || "defaultSecretKey";
|
||||
let data = { userId: user.id, username: user.username };
|
||||
|
||||
// Create JWT token
|
||||
const token = jwt.sign(data, jwtSecretKey, { expiresIn: "1d" });
|
||||
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:
|
||||
#- D:/Projects/AuthenticatedAPIDocker/app/mysql/etc/mysql/mysqlauth/etc:/etc
|
||||
- D:/Projects/AuthenticatedAPIDocker/app/mysql/mysqldata_auth:/var/lib/mysql
|
||||
extra_hosts:
|
||||
- "host.docker.internal:host-gateway"
|
||||
# extra_hosts:
|
||||
# - "host.docker.internal:host-gateway"
|
||||
|
||||
# Data Database
|
||||
mysql-data:
|
||||
@@ -45,12 +45,14 @@ services:
|
||||
environment:
|
||||
# Auth DB connection
|
||||
AUTH_DB_HOST: mysql-auth
|
||||
AUTH_DB_USER: admin
|
||||
AUTH_DB_USER: root
|
||||
#AUTH_DB_PORT: 3306
|
||||
AUTH_DB_NAME: popcenauth
|
||||
|
||||
# Data DB connection
|
||||
DATA_DB_HOST: mysql-data
|
||||
DATA_DB_USER: admin
|
||||
#DATA_DB_PORT: 3306
|
||||
DATA_DB_USER: root
|
||||
DATA_DB_NAME: popcen
|
||||
|
||||
depends_on:
|
||||
|
||||
Reference in New Issue
Block a user