Git Product home page Git Product logo

squishy_cookies's Introduction

Squishy Cookies ๐Ÿช

Easily sign and verify cookies.

Usage

import {
  createSignedCookie, verifySignedCookie,
} from 'https://deno.land/x/squishy_cookies/mod.ts'

const COOKIE_SECRET = Deno.env.get('COOKIE_SECRET') || 'super_secret'

// 1. Create a signed cookie

// Optional, headers or cookie string (to be used in Set-cookie header)
const { headers, cookie } = await createSignedCookie(
    'id', '1', COOKIE_SECRET,
    { httpOnly: true, path: '/' }
)
return new Response(page, { headers })

// or

const headers = new Headers()
headers.append('set-cookie', cookie)


// 2. Verifying a cookie

headers.append('cookie', cookie) // verifySignedCookie will search for 'cookie' header
const userId = await verifySignedCookie(headers, 'id', COOKIE_SECRET)
// userId is false if the verification failed or the cookie value
if (userId) {
  const user = await getUserById(userId.split('.')[0])
}

Tip to add multiple cookies

const { cookie: cookie1 } = await createSignedCookie(/* ... */)
const { cookie: cookie2 } = await createSignedCookie(/* ... */)

const response = new Response(someHTML, {
  // using headers as an array of arrays let you use
  // multiple headers with the same name
  headers: [
    ['Set-cookie', cookie1],
    ['Set-cookie', cookie2],
  ]
})

squishy_cookies's People

Contributors

omar2205 avatar

Stargazers

Brad Pillow avatar Brad Dougherty avatar Akky AKIMOTO avatar Alyreza mahmoudy avatar v1rtl avatar  avatar Nikita avatar Jamie Barton avatar Luiz Felipe Warmling Amadeu avatar Christian Rotzoll avatar sudo sand avatar Mohammed Imran avatar Thomas Harr avatar Jae Jin avatar CJ avatar

Watchers

 avatar  avatar

squishy_cookies's Issues

Allow dots (.) in the cookie values

Hi Omar,

Nice cookies module you made here ๐Ÿ‘

I can't figure out why this code is not working when I have multiple cookies:

import { MiddlewareHandlerContext } from "$fresh/server.ts";
import { verifySignedCookie } from "squishyCookies";
import State from "@/schemas/state.ts";
import config from "@/utils/config.ts";

export async function handler(
  req: Request,
  ctx: MiddlewareHandlerContext<State>,
) {
  const secret = config.cookie;
  const memberId = await verifySignedCookie(req.headers, "id", secret);
  const username = await verifySignedCookie(req.headers, "username", secret);

  if (memberId) {
    ctx.state.memberId = memberId.split(".")[0];
  }

  if (username) {
    ctx.state.username = username.split(".")[0];
  }
  return ctx.next();
}

The error I get is :

Error: Invalid input
    at cookieVerify (https://deno.land/x/[email protected]/index.ts:40:11)
    at async verifySignedCookie (https://deno.land/x/[email protected]/index.ts:90:17)
    at async handler (file:///Users/torel/code/deno-ypp/routes/_middleware.ts:14:20)
    at async Server.handler (https://deno.land/x/[email protected]/src/server/context.ts:332:19)
    at async Server.#respond (https://deno.land/[email protected]/http/server.ts:298:18)

Ofcourse everything works if I use only the memberId cookie, but it seems you can't verify more then one ? I don't know, maybe you can help. Thanks

problems setting cookies in safari

Hi again Omar, I think I discovered another bug , it seems that squishy cookies doesn't set the cookie / cookies (i'm setting 2 cookies, haven't tried with 1) in safari and duckduckgo browsers and orion. Can you check it out please ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.