如何訪問angularjs中passportj設置的cookie

[英]How do I access cookies set by passportjs in angularjs


I really hope this isn't a stupid question but I suspect it is. I've googled everything I can to understand what's going on but I still don't understand how to read cookies that have been set using nodejs/express/passport.

我真的希望這不是一個愚蠢的問題,但我懷疑它是。我用Google搜索了所有可以理解發生了什么的內容,但我仍然不明白如何閱讀使用nodejs / express / passport設置的cookie。

I'm using Angular's ngCookies and $cookies service but when I try to print out the cookies I set on the server side, I see this (using console.log($cookies["connect.sess"]))

我正在使用Angular的ngCookies和$ cookies服務但是當我嘗試打印出我在服務器端設置的cookie時,我看到了這一點(使用console.log($ cookies [“connect.sess”]))

s:j:{"passport":{"user":{"id":1,"email":"xxxxxx@yyyy.com","role":"Admin"}}}.0lKpQzpgzCAjgkIayDkUiOLhktIYsohr61U/4xSwWFA

My cookie is in there. But it's all stored as a string with that other stuff surrounding it. Should I be looking at a different object? It won't let me access -->

我的餅干在那里。但它全部存儲為一個字符串,其他東西圍繞它。我應該看一個不同的對象嗎?它不會讓我訪問 - >

$cookies["connect.sess"].passport

because that's undefined which makes sense since $cookies["connect.sess"] returns a string. Any hints as to where I'm going wrong here? All I need to do is access these cookies that were set in the server. I don't need to change them. I'm hoping an extra set of eyes on this will do the trick. I'm stumped!

因為$ cookies [“connect.sess”]返回一個字符串,所以這是有意義的。關於我在哪里出錯的任何暗示?我需要做的就是訪問服務器中設置的這些cookie。我不需要改變它們。我希望額外的一雙眼睛能夠解決這個問題。我很難過!

I tried setting my own cookies by using res.cookie. On the server side where I am creating the cookie it looks fine. But somewhere between there and angular, it looks like "j:{my json cookie}" This time no s: in front and no gobble-de-gook on the end. Sure I can parse that stuff out and find my json, but where is this stuff getting added and why?

我嘗試使用res.cookie設置自己的cookie。在我創建cookie的服務器端,它看起來很好。但在那里和棱角之間的某個地方,它看起來像“j:{my json cookie}”這次沒有s:在前面,最后沒有狼吞虎咽。當然我可以解析那些東西並找到我的json,但這些東西在哪里被添加,為什么?

I did some poking around and the express response.cookie adds the j: in front of the json if you pass in an object. and the s: with the blurb after the json object is their way of signing a cookie. So I found that I didn't really need the session cookies that passports was sending. I could just use the userid in the session cookies for authentication on the server side and create my own email and role cookies created using:

我做了一些戳,並且快速response.cookie在json前面添加j:如果你傳入一個對象。和s:json對象之后的blurb是他們簽署cookie的方式。所以我發現我並不真正需要護照發送的會話cookie。我可以在會話cookie中使用userid進行服務器端的身份驗證,並使用以下命令創建自己的電子郵件和角色cookie:

res.cookie("email", user.email, {my cookie options}) 
res.cookie("role", user.role, {my cookie options})

instead of trying to populate the session cookies with a json object. The answer I've chosen below is the best option given what I know today. Although I find it hard to believe that you have to strip characters off an object to get client cookies properly. Seems odd. I'm a cookie newbie, so if anyone has an explanation or a good link for reading I'd love to hear it.

而不是嘗試使用json對象填充會話cookie。根據我今天所知,我在下面選擇的答案是最好的選擇。雖然我發現很難相信您必須從對象上剝離字符才能正確獲取客戶端cookie。看起來很奇怪。我是一個餅干新手,所以如果有人有解釋或閱讀的好鏈接,我很樂意聽到它。

1 个解决方案

#1


2  

Cookies are always stored as string, so that is the reason you are getting a string when you do console.log($cookies["connect.sess"]) and this string is the json representation of the object.

Cookie始終存儲為字符串,因此這是您在執行console.log($ cookies [“connect.sess”])時獲取字符串的原因,此字符串是對象的json表示形式。

As your cookie has some non-json stuff, first you need to extract everything between the brackets "{}", then deserialize the string to a javascript object.

由於你的cookie有一些非json的東西,首先你需要提取括號“{}”之間的所有內容,然后將字符串反序列化為javascript對象。

To deserialize a JSON string to a javascript object you must use angular.fromJson(json).

要將JSON字符串反序列化為javascript對象,必須使用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 from the crazy cookie string
//There is also an option to to this with regular expressions
var jsonString = s.substring(s.indexOf("{"), s.lastIndexOf("}") + 1);

// Then convert to a javascript object
var obj = angular.fromJson(jsonString);
var passport = obj.passport;

https://docs.angularjs.org/api/ng/function/angular.fromJson

https://docs.angularjs.org/api/ng/function/angular.fromJson


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2014/08/19/729f8fd6cde9b47e19bf8ae0be958520.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com