Search code examples
javascriptexpresscookiesjquery-cookie

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?


Solution

  • 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.