node.js - How do I access cookies set by passportjs in angularjs -
i hope isn't stupid question suspect is. i've googled can understand what's going on still don't understand how read cookies have been set using nodejs/express/passport.
i'm using angular's ngcookies , $cookies service when try print out cookies set on server side, see (using console.log($cookies["connect.sess"]))
s:j:{"passport":{"user":{"id":1,"email":"xxxxxx@yyyy.com","role":"admin"}}}.0lkpqzpgzcajgkiaydkuiolhktiysohr61u/4xswwfa my cookie in there. it's stored string other stuff surrounding it. should looking @ different object? won't let me access -->
$cookies["connect.sess"].passport because that's undefined makes sense since $cookies["connect.sess"] returns string. hints i'm going wrong here? need access these cookies set in server. don't need change them. i'm hoping set of eyes on trick. i'm stumped!
i tried setting own cookies using res.cookie. on server side creating cookie looks fine. somewhere between there , angular, looks "j:{my json cookie}" time no s: in front , no gobble-de-gook on end. sure can parse stuff out , find json, stuff getting added , why?
i did poking around , express response.cookie adds j: in front of json if pass in object. , s: blurb after json object way of signing cookie. found didn't need session cookies passports sending. use userid in session cookies authentication on server side , create own email , role cookies created using:
res.cookie("email", user.email, {my cookie options}) res.cookie("role", user.role, {my cookie options}) instead of trying populate session cookies json object. answer i've chosen below best option given know today. although find hard believe have strip characters off object client cookies properly. seems odd. i'm cookie newbie, if has explanation or link reading i'd love hear it.
cookies stored string, reason getting string when console.log($cookies["connect.sess"]) , string json representation of object.
as cookie has non-json stuff, first need extract between brackets "{}", deserialize string javascript object.
to deserialize json string javascript object must use angular.fromjson(json).
example:
//'s:j:{"passport":{"user":{"id":1,"email":"xxxxxx@yyyy.com","role":"admin"}}}.0lkpqzpgzcajgkiaydkuiolhktiysohr61u/4xswwfa'; var s = $cookies["connect.sess"]; // first extract json object crazy cookie string //there option to regular expressions var jsonstring = s.substring(s.indexof("{"), s.lastindexof("}") + 1); // convert javascript object var obj = angular.fromjson(jsonstring); var passport = obj.passport;
Comments
Post a Comment