当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How to know the currentUser in parse server

问题描述:

I am doing the register with express and Parse but when the login redirect to other view I want get the currentUser but this send undefined this is my code to login

exports.login_post = function(req, res) {

Parse.User.logIn(req.body.username, req.body.password,{

success: function(user) {

if (user.get("tipoUsuario") == 1) {

console.log("Usuario Administrador Logeado con Exito "+user.id)

res.redirect('/admin_admin');

}

if (user.get("tipoUsuario")==2){

console.log("Usuario Restaurante Logeado con Exito " + user.id)

res.redirect('/edit_profile');

} else {

res.redirect('/');

}

},

error: function(user, error) {

console.log(req.body.username);

console.log(req.body.password);

console.log("error"+error)

res.redirect('/');

}

});

};

But I cannot use var currentUser = Parse.User.current();

This returns as undefined.

Some know the other solution to get the currentUser?

网友答案:

you can set a session variable to store your current user, later retrieve that user in your other views.

You can read how to set up session variable in this documenatation. This will give you better insights and understanding on how to use session variables.

Set the session variable before redirect.

req.session.currUser= currentUser;
res.redirect('/');

Retrieve the session variable in the other views:

app.get('/', function(req, res) {
  var currUser= req.session.currUser;
  req.session.currUser= null; // reset the session variable after retrieving the value.
  // Do something
});
网友答案:

The solution was

exports.login = function(req, res){
var pathname = req.body.pathname;
Parse.User.logIn(req.body.username, req.body.password, {
    success: function(user){
        if(user.get("tipoUsuario")==1){
            console.log("Usuario Administrador Logeado con Exito  "+user.id)
            req.session.user = user;
            console.log(req.session.user);
            res.redirect("/admin_admin");
        }
        if(user.get("tipoUsuario")==2){
            console.log("Usuario Restaurante Logeado con Exito  "+user.id)
            req.session.user = user;
            console.log(req.session.user);
            res.redirect("/edit_profile");
        }
    },
    error: function(user, error){
        console.log("error name: "+ error.name);
        console.log("error message:  "+ error.message);
        res.redirect("/");
    }   
}); 

};

分享给朋友:
您可能感兴趣的文章:
随机阅读: