Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/omar2205/squishy_cookies
Easily sign and verify cookies
https://github.com/omar2205/squishy_cookies
cookie cookies deno typescript
Last synced: 1 day ago
JSON representation
Easily sign and verify cookies
- Host: GitHub
- URL: https://github.com/omar2205/squishy_cookies
- Owner: omar2205
- License: mit
- Created: 2022-08-20T04:15:08.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-03-23T01:11:10.000Z (over 1 year ago)
- Last Synced: 2024-10-31T10:51:47.716Z (8 days ago)
- Topics: cookie, cookies, deno, typescript
- Language: TypeScript
- Homepage: https://deno.land/x/squishy_cookies
- Size: 22.5 KB
- Stars: 15
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
- awesome-deno - squishy_cookies - Easily sign and verify cookies. (Modules / Web utils)
README
# Squishy Cookies 🍪
Easily sign and verify cookies.
## Usage
```ts
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
```ts
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],
]
})
```