Commit 8c201fce by Shaganaz

Hashed password and inserted admin role using seeders

parent 0a10ca0f
......@@ -9,6 +9,7 @@
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"bcrypt": "^6.0.0",
"body-parser": "^2.2.0",
"dotenv": "^16.5.0",
"express": "^5.1.0",
......@@ -165,6 +166,20 @@
"integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==",
"license": "MIT"
},
"node_modules/bcrypt": {
"version": "6.0.0",
"resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-6.0.0.tgz",
"integrity": "sha512-cU8v/EGSrnH+HnxV2z0J7/blxH8gq7Xh2JFT6Aroax7UohdmiJJlxApMxtKfuI7z68NvvVcmR78k2LbT6efhRg==",
"hasInstallScript": true,
"license": "MIT",
"dependencies": {
"node-addon-api": "^8.3.0",
"node-gyp-build": "^4.8.4"
},
"engines": {
"node": ">= 18"
}
},
"node_modules/binary-extensions": {
"version": "2.3.0",
"resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz",
......@@ -1443,6 +1458,26 @@
"node": ">= 0.6"
}
},
"node_modules/node-addon-api": {
"version": "8.3.1",
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-8.3.1.tgz",
"integrity": "sha512-lytcDEdxKjGJPTLEfW4mYMigRezMlyJY8W4wxJK8zE533Jlb8L8dRuObJFWg2P+AuOIxoCgKF+2Oq4d4Zd0OUA==",
"license": "MIT",
"engines": {
"node": "^18 || ^20 || >= 21"
}
},
"node_modules/node-gyp-build": {
"version": "4.8.4",
"resolved": "https://registry.npmjs.org/node-gyp-build/-/node-gyp-build-4.8.4.tgz",
"integrity": "sha512-LA4ZjwlnUblHVgq0oBF3Jl/6h/Nvs5fzBLwdEF4nuxnFdsfajde4WfxtJr3CaiH+F6ewcIB/q4jQ4UzPyid+CQ==",
"license": "MIT",
"bin": {
"node-gyp-build": "bin.js",
"node-gyp-build-optional": "optional.js",
"node-gyp-build-test": "build-test.js"
}
},
"node_modules/nodemon": {
"version": "3.1.10",
"resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.1.10.tgz",
......
......@@ -12,6 +12,7 @@
"license": "ISC",
"description": "",
"dependencies": {
"bcrypt": "^6.0.0",
"body-parser": "^2.2.0",
"dotenv": "^16.5.0",
"express": "^5.1.0",
......
......@@ -22,18 +22,21 @@ module.exports = {
const mathsQuizId=1
await queryInterface.bulkInsert('questions', [
{
id:1,
quiz_id: mathsQuizId,
question_text: 'What is 2 + 2?',
createdAt: new Date(),
updatedAt: new Date()
},
{
id:2,
quiz_id: mathsQuizId,
question_text: 'What is 10 - 4?',
createdAt: new Date(),
updatedAt: new Date()
}
]);
await queryInterface.bulkInsert('options', [
{
question_id: 1,
......@@ -83,8 +86,8 @@ module.exports = {
},
async down (queryInterface, Sequelize) {
await queryInterface.bulkDelete('quizzes', null, {});
await queryInterface.bulkDelete('questions', null, {});
await queryInterface.bulkDelete('options', null, {});
await queryInterface.bulkDelete('questions', null, {});
await queryInterface.bulkDelete('quizzes', null, {});
}
};
'use strict';
const bcrypt=require('bcrypt');
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up (queryInterface, Sequelize) {
const hashedPassword=await bcrypt.hash('admin@123',10);
await queryInterface.bulkInsert('users',[{
email:'admin@gmail.com',
password:hashedPassword,
role:'admin',
createdAt: new Date(),
updatedAt: new Date()
}], {});
},
async down (queryInterface, Sequelize) {
return queryInterface.bulkDelete('users', { email: 'admin@gmail.com' }, {});
}
};
const bcrypt=require('bcrypt');
const jwt = require('jsonwebtoken');
const db = require('../../models');
const { json } = require('body-parser');
exports.register = async (req, res) => {
const { email, password } = req.body;
......@@ -10,7 +12,9 @@ exports.register = async (req, res) => {
if (existing) {
return res.json({ message: 'User already registered' });
}
await db.user.create({ email, password, role });
const salt=await bcrypt.genSalt(10);
const hashedPass=await bcrypt.hash(password,salt);
await db.user.create({ email, password:hashedPass, role });
return res.json({ message: 'User registered successfully' });
} catch (err) {
return res.json({ message: 'Registration failed', error: err.message });
......@@ -21,12 +25,15 @@ exports.login = async (req, res) => {
const { email, password } = req.body;
try {
const user = await db.user.findOne({ where: { email, password } });
const user = await db.user.findOne({ where: { email} });
if (!user) {
return res.json({ message: 'Invalid credentials' });
}
const isMatch=await bcrypt.compare(password,user.password);
if(!isMatch){
return res.json({message:'Invalid credentials'});
}
const token = jwt.sign(
{ userId: user.id, email: user.email, role: user.role },
process.env.JWT_SECRET,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment