Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug-fx create seeders for admin and role for admin #50

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ GOOGLE_CALLBACK_URL = ""
# CLOUDINARY CONFIGURATION
CLOUDINARY_NAME=""
CLOUDINARY_KEY=""
CLOUDINARY_SECRET=""
CLOUDINARY_SECRET=""

#USER ADMIN CREDENTIALS
ADMIN_PASSWORD=""
ADMIN_PHONE=""
4 changes: 2 additions & 2 deletions src/controllers/authController.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down Expand Up @@ -71,7 +71,7 @@ const login = async (req: Request, res: Response): Promise<void> => {
}

// 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' });
Expand Down
5 changes: 2 additions & 3 deletions src/controllers/userController.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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) => {
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions src/database/seeders/20240427082911-create-default-role.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
]);
},

Expand Down
34 changes: 34 additions & 0 deletions src/database/seeders/20240501163745-User.js
Original file line number Diff line number Diff line change
@@ -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, {});
},
};
10 changes: 10 additions & 0 deletions src/helpers/encrypt.ts
Original file line number Diff line number Diff line change
@@ -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);
};
Loading