2

Al guardar la contraseña realizo el bcryp.hash y me guarda en la base de datos algo como esto, para "1234" => (d1c8912e41fe7ae8f44717c0a2157ab9377b14a7) , pero al momento de realizar el login que uso bcrypt.compare me retorna false

add user

user: async (_, { input:{firstName, lastName, email, fullName, age, role, login, passwordHash} }, { models: { users } }) => {
      let user

      try {
        user = await users.create({
          firstName,
          lastName,
          email,
          fullName,
          age,
          role,
          login,
          passwordHash: await bcrypt.hash(passwordHash, 10)
        })
      } catch (error) {
        console.error(error)
      }
      return user
    }

login user

login: async (_, { input: { email, passwordHash } }, { models: { users } }) => {
      const user = await users.findOne({ email })

      if (!user) {
        throw new Error('No user with that email')
      }
      const valid = await bcrypt.compare(passwordHash, user.passwordHash)
       if (!valid) {
         throw new Error('Incorrect password')
       }

       //return json web token
      return jsonwebtoken.sign(
        { id: user._id, email: user.email, role: user.role },
        process.env.JWT_SECRET,
        { expiresIn: '1d' }
      )
    }
1
  • passwordHash debe ser un string normal que se compara con un hash almacenado en tu bd, osea que user.passwordHash es el que esta encriptado. Así lo tienes?
    – Legna
    el 21 ene. 2020 a las 17:43

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.