2017-06-19 5 views
0

ノードjsを使用してアプリケーションを作成しています。このアプリケーションでは、すでにユーザーのログインとパスポートjsによる登録を完了しました。だから今私はパスワードを変更するには、ログインしているユーザーへのアクセスを提供する必要があります。だから私は自分のやり方でこれをやろうとしているが、このプロセスを実行すると、変更されたパスワードは更新されず、ログされたユーザーのmongoose文書に保存される。私はそのプロセスに使用したコードを提供します。だから私はあなたに私のプログラムでこれをどうやってやり遂げることができるか教えてください。ノードjsパスポートjsユーザーのパスワードを変更する

これは変更パスワードのPOSTルートです。

app.post('/changePass/:hash', isLoggedIn, function(req, res){ 
    cph.findOne({hash: req.params.hash}).populate('userId', "local.password -_id").exec(function(err, hash){ 
     if(err) throw err; 
     if(validator.isEmpty(req.body.currentPassword) || validator.isEmpty(req.body.newPassword) || validator.isEmpty(req.body.confirmPassword)){ 
      res.render('admin/settings/pages/cup/cpf', { 
       user: req.user, 
       message: 'Fields must be required', 
       data: hash 
      }); 
     } 
     else { 
      if(!bcrypt.compareSync(req.body.currentPassword, hash.userId.local.password)){ 
       res.render('admin/settings/pages/cup/cpf', { 
        user: req.user, 
        message: 'Current password is incurrect', 
        data: hash 
       }); 
      } 
      else { 
       if(req.body.newPassword != req.body.confirmPassword){ 
        res.render('admin/settings/pages/cup/cpf', { 
         user: req.user, 
         message: 'New password and confirm password do not match', 
         data: hash 
        }); 
       } 
       else { 
        cph.update({$set:{'userId.local.password': bcrypt.hashSync(req.body.confirmPassword, bcrypt.genSaltSync(8), null)}}, function(){ 
         console.log('Success') 
        }); 
       } 
      } 
     } 
    }); 
}); 

これはハッシュの作成がログインしたユーザーの電子メールへの組み合わせのリンクとして送信し、パスワードを変更するには、マングースのコレクションです。

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var bcrypt = require('bcrypt-nodejs'); 

var cpHashSchema = Schema({ 
    userId: { 
     type: Schema.ObjectId, 
     ref: 'users' 
    }, 
    hash: { 
     type: String 
    } 
}); 

module.exports = mongoose.model('changepasswordHash', cpHashSchema); 

これは、ユーザのコレクション

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var bcrypt = require('bcrypt-nodejs'); 

var userSchema = Schema({ 
    active: { 
     type: Boolean, 
     default: false 
    }, 
    first: { 
     type: String 
    }, 
    last: { 
     type: String 
    }, 
    email: { 
     type: String 
    }, 
    local: { 
     username: { 
      type: String 
     }, 
     password: { 
      type: String 
     } 
    }, 
    joined: { 
     type: Date, 
     default: Date.now 
    }, 
    usertype: { 
     type: String, 
     default: 'user' 
    } 
}); 

userSchema.methods.generateHash = function(password) { 
    return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null); 
}; 

userSchema.methods.validPassword = function(password) { 
    return bcrypt.compareSync(password, this.local.password); 
}; 

module.exports = mongoose.model('users', userSchema); 

これらは、私は、このアプリケーションを構築するために使用しているソースコードですです。だから皆さん、このアプリケーションを完了するために私を助けてください。

はあなたにすべての

答えて

1

まず感謝 - あなたは別のテーブルのフィールドでchangepasswordHashコレクションを更新しようとしています。 MongoDBは関連レコードを更新できませんでした。

あなたが好きなのuserIdのものを使用してusersコレクションを更新する必要があります。このusers.updateで

users.update({_id: hash.userId._id}, {$set: {'local.password': newPass}}, callbackHere) 
+0

({_ ID:hash.userId._id}それについて説明してください、それが何をすべきかのオブジェクトは、」 –

+0

より多くのビットそれは –

+1

Mongooseユーザーを2番目のクエリとしてユーザーコレクションに読み込み、ハッシュオブジェクトに追加することで、ユーザーコレクションのすべてのフィールドを 'hash.userId'プロパティに追加できます。 –

関連する問題