简体   繁体   中英

how to parse signed cookie in client through javascript or jquery

i set secret key by Express: res.cookie('name','value', {signed:true}); and want to parse the signed cookie in client javascript, such as document.cookie or $.cookie() , but i can't find out where there is any API for that. do anyone know about it?

Given a cookie value of s:0.vunrLqeIl9xNAJFmibUmCeWeI3vDS9m/mbnkAZiV‌​ubU , its value will be 0 . The prefix s: signifies that its a signed cookie, and the suffix .vunr...ubU is the actual signature.

So to extract the value, you can use something like this:

let value = $.cookie('name').match(/^s:(.*)\..*$/)[1];

Or, if you're using a bundler like Webpack or Browserify, you may be able to use the actual module that it used to create and verify signed cookies in Express: cookie-signature .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM