Skip to main content
Module

std/http/mod.ts>parseSignedCookie

Deno standard library
Go to Latest
function parseSignedCookie
import { parseSignedCookie } from "https://deno.land/std@0.221.0/http/mod.ts";

Parses a signed cookie to get its value.

Important: always verify the cookie using verifyCookie first.

Examples

Example 1

import { verifyCookie, parseSignedCookie } from "https://deno.land/std@0.221.0/http/unstable_signed_cookie.ts";
import { getCookies } from "https://deno.land/std@0.221.0/http/cookie.ts";

const key = await crypto.subtle.generateKey(
  { name: "HMAC", hash: "SHA-256" },
  true,
  ["sign", "verify"],
);

const headers = new Headers({
  Cookie: "location=tokyo.37f7481039762eef5cd46669f93c0a3214dfecba7d0cdc0b0dc40036063fb22e",
});
const signedCookie = getCookies(headers)["location"];
if (signedCookie === undefined) throw new Error("Cookie not found");
await verifyCookie(signedCookie, key);
const cookie = parseSignedCookie(signedCookie);

Parameters

signedCookie: string

Returns

string