私はドキュメントのいたるところにありますが、資格情報を更新する方法が見つからないようです。
これは、コードを分析して拾うことができたものです。
passport.deserializeUser(function(id, done) {
AppUser.findById(id, function(err, user) {
done(err, user);
});
});
DeserializeUser は便利なようですが、それを使用してフィールドを更新または追加する方法がわかりません。
ログインからロジックをハッキングしてコピーし、それを理解しようとしていました。
passport.use('local-update', new LocalStrategy({
usernameField : 'username',
passReqToCallback : true
},
function(req, username, done) {
console.log(req)
// asynchronous
// AppUser.findOne wont fire unless data is sent back
// process.nextTick(function() {
// // find a user whose email is the same as the forms email
// // we are checking to see if the user trying to login already exists
// AppUser.findOne({ 'local.email' : email }, function(err, user) {
// // if there are any errors, return the error
// if (err)
// return done(err);
// // check to see if theres already a user with that email
// if (!user) {
// //return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
// return done(null, false);
// } else {
// // if there is a user with that email
// // create the username
// var updateUser = new AppUser();
// // set the user's local credentials
// newUser.local.email = email;
// newUser.local.password = newUser.generateHash(password);
// // save the user
// newUser.update(function(err) {
// if (err)
// throw err;
// return done(null, newUser);
// });
// }
// });
// });
}));
次に、フォーム送信でこれを行いました。
app.post('/profile', passport.authenticate('local-update', {
successRedirect : '/', // redirect to the secure profile section
failureRedirect : '/signup' // redirect back to the signup page if there is an error
//failureFlash : true // allow flash messages
}));
これにより、リダイレクトが失敗します。
応答がないので動作しませんが、mongoDB でモデルを見つける必要があります。最初にコンソールで req を確認しようとしているので、モデルを見つける方法を確認できるかもしれませんが、何も表示されません。
上記の明らかにハックなコードですが、これが私ができる最善のことです。具体的な答えが必要です。それは簡単だと確信しており、ドキュメントに記載されていません!
編集:ここでのアイデアは、ユーザーがサインアップ/ログインして電子メールを提供するときです。ユーザーがログインしてアカウントが作成されると、ユーザー名を作成できます。
編集:パスポートで更新リクエストを行う方法がわかりませんが、ルーターには次のようなものがあります。
app.post('/', function(req, res) {
if (req.user) {
AppUser.findOne({ _id: req.user.id }, function (err, user) {
user.local.username = req.body.username;
user.save(function(err) {
if (err){
console.log('Error')
} else {
console.log('Sucess')
}
});
});
}
});
唯一の問題は、ブラウザーのデフォルトのアクションです。フォームを送信し、ページを無限にリロードします。しかし、それは私のmongodbモデルを更新します。スキーマにアドオンする必要があり、パスポートのサインアップ ロジックにそのプロパティを追加する必要がありました。
しかし、このロジックをクライアント側のコードに追加して、POST メソッドをバックボーンにスローすれば、うまくいくはずです!