Sunday 19 August 2018

Code not saving hashed password

Hi anyone able to tell me why this is not storing the hashed value for my passwords?it does generate a hashed value in the method, but its saving the plain text password.My Schema:const mongoose = require('mongoose');const bcrypt = require('bcrypt');const jwt = require('jsonwebtoken');const { Schema } = mongoose;const saltRounds = 10;const BrandsSchema = new Schema({email: String,password: String});BrandsSchema.methods.setPassword = (password) => {return bcrypt.hash(password, saltRounds).then(hash => {this.password = hash;});};mongoose.model('Brands', BrandsSchema);My route:const mongoose = require('mongoose');const passport = require('passport');const router = require('express').Router();const auth = require('../auth');const Brands = mongoose.model('Brands');// POST new user route (optional, everyone has access)router.post('/', auth.optional, (req, res, next) => {const { body: { user } } = req;if (!user.email) {return res.status(422).json({errors: {email: 'is required',},});}if (!user.password) {return res.status(422).json({errors: {password: 'is required',},});}const finalUser = new Brands(user);finalUser.setPassword(user.password);console.log(finalUser)return finalUser.save().then(() => res.json({user: finalUser.toAuthJSON() }));});}module.exports = router;and my JSON post values:{"user": {"email": ["test@test.com](mailto:"test@test.com)","password": "test"}}any help is much appreciated

Submitted August 19, 2018 at 07:15PM by louisjacksonrees

No comments:

Post a Comment