diff --git a/.env.example b/.env.example index db12b357..37c697fa 100644 --- a/.env.example +++ b/.env.example @@ -37,4 +37,8 @@ GOOGLE_CALLBACK_URL = "" # CLOUDINARY CONFIGURATION CLOUDINARY_NAME="" CLOUDINARY_KEY="" -CLOUDINARY_SECRET="" \ No newline at end of file +CLOUDINARY_SECRET="" + +#USER ADMIN CREDENTIALS +ADMIN_PASSWORD="" +ADMIN_PHONE="" \ No newline at end of file diff --git a/src/controllers/authController.ts b/src/controllers/authController.ts index e601588c..fc466301 100644 --- a/src/controllers/authController.ts +++ b/src/controllers/authController.ts @@ -1,10 +1,10 @@ import { Request, Response, NextFunction } from 'express'; import passport from 'passport'; import jwt from 'jsonwebtoken'; -import bcrypt from 'bcrypt'; import User, { UserAttributes } from '../database/models/user'; import { sendInternalErrorResponse, validateFields } from '../validations'; import logger from '../logs/config'; +import { passwordCompare } from '../helpers/encrypt'; const authenticateViaGoogle = (req: Request, res: Response, next: NextFunction) => { passport.authenticate('google', (err: unknown, user: UserAttributes | null) => { @@ -71,7 +71,7 @@ const login = async (req: Request, res: Response): Promise => { } // Verify password - const passwordValid = await bcrypt.compare(password, user.password); + const passwordValid = await passwordCompare(password, user.password); if (!passwordValid) { logger.error('Invalid credentials'); res.status(404).json({ ok: false, message: 'Invalid credentials' }); diff --git a/src/controllers/userController.ts b/src/controllers/userController.ts index c6db3c33..aec6f8ee 100644 --- a/src/controllers/userController.ts +++ b/src/controllers/userController.ts @@ -1,5 +1,4 @@ import { Request, Response } from 'express'; -import bcrypt from 'bcrypt'; import User from '../database/models/user'; import logger from '../logs/config'; import { userToken } from '../helpers/token.generator'; @@ -8,6 +7,7 @@ import * as jwt from 'jsonwebtoken'; import Role from '../database/models/role'; import { sendEmail } from '../helpers/send-email'; import { sendInternalErrorResponse, validateEmail, validateFields, validatePassword } from '../validations'; +import { passwordEncrypt } from '../helpers/encrypt'; // Function for user signup export const signupUser = async (req: Request, res: Response) => { @@ -41,8 +41,7 @@ export const signupUser = async (req: Request, res: Response) => { return res.status(400).json({ ok: false, error: 'Email is already used, Login to continuue' }); } - const saltRound = await bcrypt.genSalt(10); - const hashPassword = await bcrypt.hash(password, saltRound); + const hashPassword = await passwordEncrypt(password); const newUser = await User.create({ firstName, diff --git a/src/database/seeders/20240427082911-create-default-role.js b/src/database/seeders/20240427082911-create-default-role.js index 3f0bb578..5cb9369d 100644 --- a/src/database/seeders/20240427082911-create-default-role.js +++ b/src/database/seeders/20240427082911-create-default-role.js @@ -13,6 +13,13 @@ module.exports = { createdAt: new Date(), updatedAt: new Date(), }, + { + id: '6ef1e121-304a-4f08-ad4e-cd07f9578b52', + name: 'admin', + displayName: 'Admin Role', + createdAt: new Date(), + updatedAt: new Date(), + }, ]); }, diff --git a/src/database/seeders/20240501163745-User.js b/src/database/seeders/20240501163745-User.js new file mode 100644 index 00000000..3f1ebd8d --- /dev/null +++ b/src/database/seeders/20240501163745-User.js @@ -0,0 +1,34 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +'use strict'; +const { v4: uuidv4 } = require('uuid'); + +/** @type {import('sequelize-cli').Seed} */ +module.exports = { + async up(queryInterface, Sequelize) { + return queryInterface.bulkInsert( + 'Users', + [ + { + id: uuidv4(), + firstName: 'admin', + lastName: '', + email: process.env.EMAIL, + password: + '$2b$10$ZCgzouXesg4Zqgj22u7ale5aAOJzmjfOchCpMlSgBMV8o2f.zdYUq', + gender: 'not specified', + phoneNumber: process.env.ADMIN_PHONE, + verified: true, + createdAt: new Date(), + updatedAt: new Date(), + status: 'active', + RoleId: '6ef1e121-304a-4f08-ad4e-cd07f9578b52', // Replace with the actual RoleId + }, + ], + {} + ); + }, + + async down(queryInterface, Sequelize) { + return queryInterface.bulkDelete('Users', null, {}); + }, +}; diff --git a/src/helpers/encrypt.ts b/src/helpers/encrypt.ts new file mode 100644 index 00000000..4d7a91ec --- /dev/null +++ b/src/helpers/encrypt.ts @@ -0,0 +1,10 @@ +import bcrypt from 'bcrypt'; + +export const passwordEncrypt = async (password: string) => { + const saltRound = await bcrypt.genSalt(12); + const hashedPwd = await bcrypt.hash(password, saltRound); + return hashedPwd; +}; +export const passwordCompare = async (password: string, inputPwd: string) => { + return await bcrypt.compare(password, inputPwd); +};