update jamespong14205,
This commit is contained in:
47
task1/project/003_src/client/db_seed/auth.js
Normal file
47
task1/project/003_src/client/db_seed/auth.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const { DataTypes } = require('sequelize');
|
||||
const bcrypt = require('bcrypt');
|
||||
const { sequelize } = require('./model');
|
||||
|
||||
const Auth = sequelize.define(
|
||||
'Auths',
|
||||
{
|
||||
uid: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true, unique: true },
|
||||
username: { type: DataTypes.STRING, allowNull: false },
|
||||
password: { type: DataTypes.STRING, allowNull: false },
|
||||
session: { type: DataTypes.STRING, allowNull: false, defaultValue: '' },
|
||||
role: { type: DataTypes.STRING, allowNull: false, defaultValue: 'customer' },
|
||||
},
|
||||
{ timestamps: false },
|
||||
);
|
||||
|
||||
async function hashPassword(plainTextPassword) {
|
||||
const saltRounds = 10;
|
||||
return await bcrypt.hash(plainTextPassword, saltRounds);
|
||||
}
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
console.log('Connection has been established successfully.');
|
||||
|
||||
// create table
|
||||
await Auth.drop();
|
||||
await sequelize.sync();
|
||||
|
||||
let user_row;
|
||||
let password;
|
||||
user_row = await Auth.create({
|
||||
username: 'admin',
|
||||
password: 'nimda',
|
||||
role: 'admin',
|
||||
});
|
||||
|
||||
user_row = await Auth.create({ username: 'pat1', password: await hashPassword('1tap') });
|
||||
user_row = await Auth.create({ username: 'pat2', password: await hashPassword('2tap') });
|
||||
user_row = await Auth.create({ username: 'pat3', password: await hashPassword('3tap') });
|
||||
|
||||
await sequelize.close();
|
||||
} catch (error) {
|
||||
console.error('Unable to connect to the database:', error);
|
||||
}
|
||||
})();
|
Reference in New Issue
Block a user