my nodejs bcrypt compare not working properly

my nodejs bcrypt compare not working properly

I am building an app with nodes qraphQl using apollo, am trying to do a login page, but ater signing up and and i try to sign in, my bcrypt would always return false,

I am building an app with nodes qraphQl using apollo, am trying to do a login page, but ater signing up and and i try to sign in, my bcrypt would always return false,

in my user model

import bcrypt from 'bcryptjs';

const user = (sequelize, DataTypes) => { const User = sequelize.define('user', {
id: { type: DataTypes.INTEGER, autoIncrement: true, unique: true, primaryKey: true, field: 'id' }, fullname: DataTypes.STRING, username: { type: DataTypes.STRING, allowNull: false, validate: { notEmpty: true, }, }, email: { type: DataTypes.STRING, allowedNull: false, validate: { notEmpty: true, isEmail: true, } }, password: { type: DataTypes.STRING, allowedNull: false, validate: { notEmpty: true, len: [7, 42], }, }, role: { type: DataTypes.ENUM, values: ['ADMIN', 'INSTRUCTOR', 'STUDENT'], defaultValue: 'STUDENT' } });

User.beforeCreate(async user => { user.password = await user.generatePasswordHash() }); User.beforeSave(async user => { user.password = await user.generatePasswordHash() });

User.prototype.generatePasswordHash = async function() { const saltRounds = 10; return await bcrypt.hash(this.password, saltRounds) };

User.prototype.validatePassword = async function(password) { return await bcrypt.compare(password, this.password); };

User.associate = models => { User.hasMany(models.Message, { onDelete: 'CASCADE' }); };

User.findByLogin = async login => { let user = await User.findOne({ where: { username: login }, });

if (!user) {
  user = await User.findOne({
    where: { email: login },
  });
}

return user;

};

return User; };

export default user;

And in my users resolver, here is the code

import { combineResolvers } from 'graphql-resolvers';
import Joi from 'joi'
import { isAuthenticated, isAdmin } from './authorization';
import {SignUp, SignIn} from '../functions/joi'
import {createToken} from '../functions/jwt'

export default {

Mutation: { signUp: async (parent, { username, fullname, email, password, Rpassword}, { models, secret }) => { if(password !== Rpassword){ return new Error('Password did not match') } var thejoi = { username, fullname, email, password } const checkUserEm = await models.User.find({ where: { email: email }}) if (checkUserEm) { return new Error('Email address already Exist') } const checkUserUs = await models.User.find({ where: { username: username }}) if (checkUserUs) { return new Error('Username already Exist') }

  await Joi.validate(thejoi, SignUp, {abortEarly:false})
  const user = await models.User.create({
    username, 
    fullname, 
    email,
    password,
    role:'STUDENT'
  });
  return { token: createToken(user) };
},
signIn: async (parent, { login, password }, { models, secret }, ) => {
  var varrh = { password }
  await Joi.validate(varrh, SignIn, {abortEarly:false})
  const user = await models.User.findByLogin(login);

  if (!user) {
    return new Error('No user found with this login credentials.');
  }

  const isValid = await user.validatePassword(password);
  if (!isValid) { 
    return new Error('Invalid password .');
  }

  return { token: createToken(user) };
}

}, User: { messages: async (user, args, { models }) => { return await models.Message.findAll({ where: { userId: user.id } }); }, }, }

pls am really confused because its it suppose to work, i have searched google but it didnt help me, pls how can i solve this issue thanks

node-js graphql

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Graphql Node JS ( Graphql Server & Client using Apollo ) # 12

Github : https://github.com/tkssharma/Graphql-fullstack-tutorials Graphql Client-Server (Complete Playlist) https://www.youtube.com/playlist?list=PLIGDNOJWiL

Graphql Node JS ( Graphql Server & Client using Apollo ) # 03

Github : https://github.com/tkssharma/Graphql-fullstack-tutorials Graphql Client-Server (Complete Playlist) https://www.youtube.com/playlist?list=PLIGDNOJWiL

GraphQL - How To Setup GraphQL Yoga Server with Node.js

Learn How To Setup GraphQL Yoga Server with Node.jS. Learn Set Up Graphql Server With Node and Graphql-Yoga.

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

Hands on with Node.Js Streams | Examples & Approach

The practical implications of having Streams in Node.js are vast. Nodejs Streams are a great way to handle data chunks and uncomplicate development.