1

私はドキュメントのいたるところにありますが、資格情報を更新する方法が見つからないようです。

これは、コードを分析して拾うことができたものです。

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 メソッドをバックボーンにスローすれば、うまくいくはずです!

4

1 に答える 1

1

このような場合callback array、Express ルートの引数として a を追加できます。

検証ハンドラーを次のように変更できると思います。

function(req, username, done) {
    //perform here only the validations you need to let the login pass
    if (validationSuccess) {
        done();
    } else {
    done('an error occured');
    }
}

したがって、この関数がユーザー資格情報の検証に成功すると仮定すると、別の関数を作成できます。

function doSomethingAfter(req, res, next) {
    //do anything else you need here
    //e.g.
    SomeModel.create(req.body.username);
    //the response is available here
    res.send('Everything ok');
}

最後に、ルーティング関数を編集できます

app.post('/profile', [passport.authenticate('local-update', {
    failureRedirect : '/signup' // redirect back to the signup page if there is an error
}), doSomethingAfter]);

そうすれば、認証を実行して、リクエストが適切に認証された後に必要なものを作成できます。さらに関数を追加する必要がある場合は、それらを配列に追加して、next()それぞれを呼び出す必要があります。

それが役に立てば幸い。

于 2014-03-26T21:46:46.483 に答える