2017-02-11 4 views
1

私はユーザーにサインアップしようとしています。私はHTMLフォームが動作しています。サインアップ自体を処理する必要があります。ユーザーをサインアップしてサインインして、現在のユーザーのParse Serverにアクセスするにはどうすればいいですか?

ユーザーは作成されましたが、ユーザーのログインを維持する方法や現在のユーザーにParse.Userオブジェクトとしてログインする方法はわかりません。

app.post("/sign-up", function (req, res) { 

var userObject = new Parse.User(); 
userObject.set("username", username); 
userObject.set("password", password); 
userObject.set("email", email); 

userObject.set("supportEmail", email); 

userObject.signUp(null, {success: function(user) { 
    //success 

res.redirect('/admin'); 

    }, 
    error: function(user, error) { 
    //show error 
    console.log(error.message); 
    res.render('sign-up', {success:false, errorMessage:error.message}); 

    } 
}); 

}); ログインしたままにして、現在のユーザーのParse.Userオブジェクトにアクセスするために何をすべきかわかりません。

答えて

0

アプリケーション内のグローバル変数に保存することができます。他のファイルで使用するためにユーザーオブジェクトをエクスポートすることもできます。サインアップの約束は、セッショントークンと一緒に認証オブジェクトに解決

var userObject = new Parse.User(); 

app.post("/sign-up", function (req, res) { 

    userObject.set("username", username); 
    userObject.set("password", password); 
    userObject.set("email", email); 

    userObject.set("supportEmail", email); 

    app.locals.user = userObject; 

    userObject.signUp(null, {success: function(user) { 
     //success 

    res.redirect('/admin'); 

     }, 
     error: function(user, error) { 
     //show error 
     console.log(error.message); 
     res.render('sign-up', {success:false, errorMessage:error.message}); 

     } 
    }); 


module.exports.userObject = userObjetct; 
+0

おかげで、どのように私は、ユーザーがログアウトに行きますか? –

0

app.locals.user = userObjectにデータベースまたは1つの他の方法で保存するための別の方法があります。

これを使用して、Parse.User.becomeを呼び出してユーザークラスを取得できます。

Parse.User.become("session-token-here").then(function (user) { 
    // The current user is now set to user. 
}, function (error) { 
    // The token could not be validated. 
}); 

出典:http://parseplatform.github.io/docs/js/guide/#setting-the-current-user

関連する問題