Update frontend login page.jsx และ backend
This commit is contained in:
		| @@ -1,13 +1,26 @@ | ||||
| // ESM | ||||
| import mysql from 'mysql2/promise'; | ||||
| // FILE: src/db/index.js (ESM) | ||||
| // Database connection and query utility | ||||
| // - Uses mysql2/promise for connection pooling and async/await | ||||
| // - Exports a query function for executing SQL with parameters | ||||
| // - Connection settings are read from environment variables with defaults | ||||
| // - Uses named placeholders for query parameters | ||||
| // - Dates are handled as strings in UTC timezone to avoid timezone issues | ||||
| // - Connection pool is configured to handle multiple concurrent requests | ||||
| // ============================================================= | ||||
| // Load environment variables from .env file if present | ||||
| // (uncomment the next line if using dotenv) | ||||
| // import dotenv from 'dotenv'; dotenv.config(); | ||||
| // (Make sure to install dotenv package if using this line) | ||||
|  | ||||
| import mysql from "mysql2/promise"; | ||||
|  | ||||
| const { | ||||
|   DB_HOST = 'mariadb', | ||||
|   DB_PORT = '3306', | ||||
|   DB_USER = 'center', | ||||
|   DB_PASSWORD = 'Center#2025', | ||||
|   DB_NAME = 'dms', | ||||
|   DB_CONN_LIMIT = '10', | ||||
|   DB_HOST = "mariadb", | ||||
|   DB_PORT = "3306", | ||||
|   DB_USER = "center", | ||||
|   DB_PASSWORD = "Center#2025", | ||||
|   DB_NAME = "dms", | ||||
|   DB_CONN_LIMIT = "10", | ||||
| } = process.env; | ||||
|  | ||||
| const pool = mysql.createPool({ | ||||
| @@ -20,7 +33,7 @@ const pool = mysql.createPool({ | ||||
|   waitForConnections: true, // Recommended for handling connection spikes | ||||
|   namedPlaceholders: true, | ||||
|   dateStrings: true, // Keep dates as strings | ||||
|   timezone: 'Z', // Store and retrieve dates in UTC | ||||
|   timezone: "Z", // Store and retrieve dates in UTC | ||||
| }); | ||||
|  | ||||
| /** | ||||
|   | ||||
| @@ -1,5 +1,12 @@ | ||||
| import { Sequelize } from 'sequelize'; | ||||
| import { config } from '../config.js'; | ||||
| // FILE: src/db/sequelize.js | ||||
| // Sequelize initialization and model definitions | ||||
| // - Configured via config.js | ||||
| // - Defines User, Role, Permission, UserRole, RolePermission models | ||||
| // - Sets up associations between models | ||||
| // - Exports sequelize instance and models for use in other modules | ||||
|  | ||||
| import { Sequelize } from "sequelize"; | ||||
| import { config } from "../config.js"; | ||||
|  | ||||
| export const sequelize = new Sequelize( | ||||
|   config.DB.NAME, | ||||
| @@ -8,9 +15,9 @@ export const sequelize = new Sequelize( | ||||
|   { | ||||
|     host: config.DB.HOST, | ||||
|     port: config.DB.PORT, | ||||
|     dialect: 'mariadb', | ||||
|     dialect: "mariadb", | ||||
|     logging: false, | ||||
|     dialectOptions: { timezone: 'Z' }, | ||||
|     dialectOptions: { timezone: "Z" }, | ||||
|     define: { | ||||
|       freezeTableName: true, | ||||
|       underscored: false, | ||||
| @@ -20,11 +27,11 @@ export const sequelize = new Sequelize( | ||||
|   } | ||||
| ); | ||||
|  | ||||
| import UserModel from './models/User.js'; | ||||
| import RoleModel from './models/Role.js'; | ||||
| import PermissionModel from './models/Permission.js'; | ||||
| import UserRoleModel from './models/UserRole.js'; | ||||
| import RolePermissionModel from './models/RolePermission.js'; | ||||
| import UserModel from "./models/User.js"; | ||||
| import RoleModel from "./models/Role.js"; | ||||
| import PermissionModel from "./models/Permission.js"; | ||||
| import UserRoleModel from "./models/UserRole.js"; | ||||
| import RolePermissionModel from "./models/RolePermission.js"; | ||||
|  | ||||
| export const User = UserModel(sequelize); | ||||
| export const Role = RoleModel(sequelize); | ||||
| @@ -32,11 +39,27 @@ export const Permission = PermissionModel(sequelize); | ||||
| export const UserRole = UserRoleModel(sequelize); | ||||
| export const RolePermission = RolePermissionModel(sequelize); | ||||
|  | ||||
| User.belongsToMany(Role, { through: UserRole, foreignKey: 'user_id', otherKey: 'role_id' }); | ||||
| Role.belongsToMany(User, { through: UserRole, foreignKey: 'role_id', otherKey: 'user_id' }); | ||||
| User.belongsToMany(Role, { | ||||
|   through: UserRole, | ||||
|   foreignKey: "user_id", | ||||
|   otherKey: "role_id", | ||||
| }); | ||||
| Role.belongsToMany(User, { | ||||
|   through: UserRole, | ||||
|   foreignKey: "role_id", | ||||
|   otherKey: "user_id", | ||||
| }); | ||||
|  | ||||
| Role.belongsToMany(Permission, { through: RolePermission, foreignKey: 'role_id', otherKey: 'permission_id' }); | ||||
| Permission.belongsToMany(Role, { through: RolePermission, foreignKey: 'permission_id', otherKey: 'role_id' }); | ||||
| Role.belongsToMany(Permission, { | ||||
|   through: RolePermission, | ||||
|   foreignKey: "role_id", | ||||
|   otherKey: "permission_id", | ||||
| }); | ||||
| Permission.belongsToMany(Role, { | ||||
|   through: RolePermission, | ||||
|   foreignKey: "permission_id", | ||||
|   otherKey: "role_id", | ||||
| }); | ||||
|  | ||||
| export async function dbReady() { | ||||
|   await sequelize.authenticate(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 admin
					admin