app ready for testing
This commit is contained in:
parent
3efa2503ea
commit
0218745bd3
@ -4,5 +4,5 @@ JWT_SECRET=yourSuperSecretKey # Replace with your actual secret key
|
||||
# Database configuration
|
||||
DB_HOST=localhost
|
||||
DB_USER=root
|
||||
DB_PASSWORD=yourpassword
|
||||
DB_NAME=jwt_auth
|
||||
DB_PASSWORD=
|
||||
DB_NAME=api_isakafo
|
||||
@ -8,4 +8,50 @@ const pool = mysql.createPool({
|
||||
database: process.env.DB_NAME,
|
||||
});
|
||||
|
||||
module.exports = pool;
|
||||
/**
|
||||
* Initialize the database and create necessary tables
|
||||
*
|
||||
*/
|
||||
async function initDB() {
|
||||
try {
|
||||
const connection = await pool.getConnection();
|
||||
|
||||
// Create users table if it doesn't exist
|
||||
await connection.query(`
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY NOT NULL,
|
||||
username VARCHAR(50) NOT NULL UNIQUE,
|
||||
password VARCHAR(255) NOT NULL,
|
||||
role VARCHAR(20) DEFAULT 'user',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
`);
|
||||
|
||||
// ajoute une autre table si necessaire
|
||||
|
||||
// add a default admin user if none exists
|
||||
const [rows] = await connection.query(`SELECT COUNT(*) as count FROM users`);
|
||||
if (rows[0].count === 0) {
|
||||
const bcrypt = require('bcryptjs');
|
||||
const hashedPassword = await bcrypt.hash('admin123', 10);
|
||||
|
||||
await connection.query(
|
||||
'INSERT INTO users (username, password, role) VALUES (?, ?, ?)',
|
||||
['admin', hashedPassword, 'admin']
|
||||
);
|
||||
|
||||
console.log('✅ Default admin user created: admin / admin123');
|
||||
}
|
||||
|
||||
connection.release();
|
||||
console.log('✅ Database initialized');
|
||||
} catch (err) {
|
||||
console.error('❌ Failed to initialize database:', err.message);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
pool,
|
||||
initDB,
|
||||
};
|
||||
8
index.js
8
index.js
@ -2,6 +2,7 @@ const express = require('express');
|
||||
const authRoutes = require('./routes/authRoute');
|
||||
const protectedRoutes = require('./routes/protectedRoute');
|
||||
require('dotenv').config();
|
||||
const { initDB } = require('./config/databases');
|
||||
|
||||
const app = express();
|
||||
|
||||
@ -10,6 +11,11 @@ app.use(express.json());
|
||||
app.use('/api/auth', authRoutes);
|
||||
app.use('/api/protected', protectedRoutes);
|
||||
|
||||
app.listen(process.env.PORT, () => {
|
||||
initDB().then(() => {
|
||||
app.listen(process.env.PORT, () => {
|
||||
console.log(`Server running on port ${process.env.PORT}`);
|
||||
});
|
||||
}).catch(err => {
|
||||
console.error('❌ Failed to initialize database:', err.message);
|
||||
});
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
const express = require('express');
|
||||
const bcrypt = require('bcryptjs');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const pool = require('../config/databases');
|
||||
const { pool } = require('../config/databases');
|
||||
require('dotenv').config();
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user