admin管理员组

文章数量:1424916

I am using passport js for login registration process. Once a new person logs in a cookie is created (custom by me) on browser that includes DB unique key.

So how the program works is:

If a newly person logins a cookie is created by this function

function setCookie(cname,cvalue,exdays) {
    //alert("Cookie set on tedJavascript");
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

then the details entered on login page is send to post function on server (passport js). Local strategy authentication code is given below(same follows for guest,fb etc)

passport.use('guest',new LocalStrategy(
function(username,password,done){
       User.getUserBycookie(password, function(err, user){
       if(err) throw err;

        //if cookie present (checked in terms of password)
       if(user){
                    //Some code
                    user.save()
                        }
                        else{
                            console.log("some problem with updating guest   user by db id!");
                        }
                    });

                    return done(null, user);
        }

//if cookie not present
else {              
                    // some part of code for new user

                    var newUser = new User();
                    newUser.guest.cookie = newUser._id;
                    trophytable_id = newUser._id;
                    newUser.guest.name = username;
                    newUser.save(function(err){
                        if(err) throw err;
                        return done(null, newUser);
                    });

     }});}));

The passport js automatically authenticates the information using strategies local,guest ,facebook,twitter etc, if correct it creates session.

But if a already logged in person enters site it first checks if a cookie is present on browser if its present it picks out the DB key.

After the key is known it finds the details of user from database(mongodb) and directly skips the login page and redirects to main menu.

But the problem is it redirects to main menu but unable to create session that could create problem further So want to create session again which passport created automatically for a new user.

This is ensure authenticated function

function ensureAuthenticated(req, res, next){
console.log("Inside ensureAuthenticated...............");
if(req.isAuthenticated()){
    console.log("Inside if part of ensureAuthenticated-------");
    console.log(req.cookies);
    return next();
} else {
            console.log("Inside else part of ensureAuthenticated----------");
            console.log(req.cookies.user_id);
            if(isEmpty(req.cookies.user_id)){
                console.log("user_id == undefined");
                res.redirect('/users/login');
            }
            else{
                 console.log("user_id defined");
                 return next();
            }
        }

}

next() leads to:

router.get('/',ensureAuthenticated, function(req,res){
console.log("Inside router get ensure authenticated--------------------");
if(isEmpty(req.session.passport)){
    //**have to create new session**

    //**getting user details from database**
    User.getUserBydbid(req.cookies.user_id, function(err, user){
        if(err) throw err;
        if(user){
            if(!isEmpty(user.local)){

               //**here i want to create session**

           }else if(!isEmpty(user.guest)){

                    //**here i want to create session**

                }
                 //skipped fb,twitter code
        }                   
        else console.log("*********some problem in finding     getUserBydbid");
    });

     //after creating session want to redirect to main menu 
     //it works but session not created
    //res.render('index',{userid:req.cookies.user_id});
}
else{
    //for newly logged in user
    res.render('index',{userid:req.session.passport.user});
}});

How can i manually create passport session for already logged in user?

I am using passport js for login registration process. Once a new person logs in a cookie is created (custom by me) on browser that includes DB unique key.

So how the program works is:

If a newly person logins a cookie is created by this function

function setCookie(cname,cvalue,exdays) {
    //alert("Cookie set on tedJavascript");
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

then the details entered on login page is send to post function on server (passport js). Local strategy authentication code is given below(same follows for guest,fb etc)

passport.use('guest',new LocalStrategy(
function(username,password,done){
       User.getUserBycookie(password, function(err, user){
       if(err) throw err;

        //if cookie present (checked in terms of password)
       if(user){
                    //Some code
                    user.save()
                        }
                        else{
                            console.log("some problem with updating guest   user by db id!");
                        }
                    });

                    return done(null, user);
        }

//if cookie not present
else {              
                    // some part of code for new user

                    var newUser = new User();
                    newUser.guest.cookie = newUser._id;
                    trophytable_id = newUser._id;
                    newUser.guest.name = username;
                    newUser.save(function(err){
                        if(err) throw err;
                        return done(null, newUser);
                    });

     }});}));

The passport js automatically authenticates the information using strategies local,guest ,facebook,twitter etc, if correct it creates session.

But if a already logged in person enters site it first checks if a cookie is present on browser if its present it picks out the DB key.

After the key is known it finds the details of user from database(mongodb) and directly skips the login page and redirects to main menu.

But the problem is it redirects to main menu but unable to create session that could create problem further So want to create session again which passport created automatically for a new user.

This is ensure authenticated function

function ensureAuthenticated(req, res, next){
console.log("Inside ensureAuthenticated...............");
if(req.isAuthenticated()){
    console.log("Inside if part of ensureAuthenticated-------");
    console.log(req.cookies);
    return next();
} else {
            console.log("Inside else part of ensureAuthenticated----------");
            console.log(req.cookies.user_id);
            if(isEmpty(req.cookies.user_id)){
                console.log("user_id == undefined");
                res.redirect('/users/login');
            }
            else{
                 console.log("user_id defined");
                 return next();
            }
        }

}

next() leads to:

router.get('/',ensureAuthenticated, function(req,res){
console.log("Inside router get ensure authenticated--------------------");
if(isEmpty(req.session.passport)){
    //**have to create new session**

    //**getting user details from database**
    User.getUserBydbid(req.cookies.user_id, function(err, user){
        if(err) throw err;
        if(user){
            if(!isEmpty(user.local)){

               //**here i want to create session**

           }else if(!isEmpty(user.guest)){

                    //**here i want to create session**

                }
                 //skipped fb,twitter code
        }                   
        else console.log("*********some problem in finding     getUserBydbid");
    });

     //after creating session want to redirect to main menu 
     //it works but session not created
    //res.render('index',{userid:req.cookies.user_id});
}
else{
    //for newly logged in user
    res.render('index',{userid:req.session.passport.user});
}});

How can i manually create passport session for already logged in user?

Share Improve this question edited Nov 21, 2016 at 7:42 wrangler asked Nov 21, 2016 at 7:36 wranglerwrangler 3,5761 gold badge21 silver badges28 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I think you have to implement the serializeUser and deserializeUser functions for it to work, as stated in the Sessions subsection of the Configure section of the documentation. Link.

After a bit of research I think you are looking for passport openid I found in the documentation here.

You can read about OpenID from the Docs but I would think you can place your DB key as the identifier token and in the identification function do the DB check and if the DB key checks out it send back the user and this should create a session for the user based on your already logged in user.

I would give code examples and how to install OpenID but they are already in the docs

本文标签: javascriptManually create passport session for already logged in UserStack Overflow