server/src/resolvers/auth.js
// update the password of an existing user
update the password of an existing user
update the password of an existing user
async function updatePassword (parent, { oldPassword, newPassword }, ctx, info) {
  let userId = getUserId(ctx)
  const user = await ctx.db.query.user({ where: { id: userId } })
  const oldPasswordValid = await bcrypt.compare(oldPassword, user.password)
  if (!oldPasswordValid) {
    console.log('old Password not Valid')
    throw new Error('Old password is wrong, please try again.')
  }
  const newPasswordHash = await bcrypt.hash(newPassword, 10)
  try {
    await ctx.db.mutation.updateUser({
      where: { id: userId },
      data: { password: newPasswordHash }
    })
  } catch (e) {
    return e
  }
  return user
}
async function updatePassword ( parent , { oldPassword , newPassword } , ctx , info ) { let userId = getUserId ( ctx ) const user = await ctx . db . query . user ( { where : { id : userId } } ) const oldPasswordValid = await bcrypt . compare ( oldPassword , user . password ) if ( ! oldPasswordValid ) { console . log ( 'old Password not Valid' ) throw new Error ( 'Old password is wrong, please try again.' ) } const newPasswordHash = await bcrypt . hash ( newPassword , 10 ) try { await ctx . db . mutation . updateUser ( { where : { id : userId } , data : { password : newPasswordHash } } ) } catch ( e ) { return e } return user }
update the password of an existing user
update the password of an existing user