Cookie 輔助工具
Cookie 輔助工具提供了一個簡單的介面來管理 Cookie,使開發人員能夠無縫地設定、解析和刪除 Cookie。
匯入
ts
import { Hono } from 'hono'
import {
getCookie,
getSignedCookie,
setCookie,
setSignedCookie,
deleteCookie,
} from 'hono/cookie'
用法
注意:由於 WebCrypto API 的非同步特性,設定和檢索簽署的 Cookie 會回傳 Promise,該 API 用於建立 HMAC SHA-256 簽章。
ts
const app = new Hono()
app.get('/cookie', (c) => {
const allCookies = getCookie(c)
const yummyCookie = getCookie(c, 'yummy_cookie')
// ...
setCookie(c, 'delicious_cookie', 'macha')
deleteCookie(c, 'delicious_cookie')
//
})
app.get('/signed-cookie', async (c) => {
const secret = 'secret ingredient'
// `getSignedCookie` will return `false` for a specified cookie if the signature was tampered with or is invalid
const allSignedCookies = await getSignedCookie(c, secret)
const fortuneCookie = await getSignedCookie(
c,
secret,
'fortune_cookie'
)
// ...
const anotherSecret = 'secret chocolate chips'
await setSignedCookie(c, 'great_cookie', 'blueberry', anotherSecret)
deleteCookie(c, 'great_cookie')
//
})
選項
setCookie
& setSignedCookie
- domain:
string
- expires:
Date
- httpOnly:
boolean
- maxAge:
number
- path:
string
- secure:
boolean
- sameSite:
'Strict'
|'Lax'
|'None'
- prefix:
secure
|'host'
- partitioned:
boolean
範例
ts
// Regular cookies
setCookie(c, 'great_cookie', 'banana', {
path: '/',
secure: true,
domain: 'example.com',
httpOnly: true,
maxAge: 1000,
expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),
sameSite: 'Strict',
})
// Signed cookies
await setSignedCookie(
c,
'fortune_cookie',
'lots-of-money',
'secret ingredient',
{
path: '/',
secure: true,
domain: 'example.com',
httpOnly: true,
maxAge: 1000,
expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),
sameSite: 'Strict',
}
)
deleteCookie
- path:
string
- secure:
boolean
- domain:
string
範例
ts
deleteCookie(c, 'banana', {
path: '/',
secure: true,
domain: 'example.com',
})
deleteCookie
會回傳被刪除的值
ts
const deletedCookie = deleteCookie(c, 'delicious_cookie')
__Secure-
和 __Host-
前綴
Cookie 輔助工具支援 Cookie 名稱的 __Secure-
和 __Host-
前綴。
如果您想驗證 Cookie 名稱是否具有前綴,請指定前綴選項。
ts
const securePrefixCookie = getCookie(c, 'yummy_cookie', 'secure')
const hostPrefixCookie = getCookie(c, 'yummy_cookie', 'host')
const securePrefixSignedCookie = await getSignedCookie(
c,
secret,
'fortune_cookie',
'secure'
)
const hostPrefixSignedCookie = await getSignedCookie(
c,
secret,
'fortune_cookie',
'host'
)
此外,如果您希望在設定 Cookie 時指定前綴,請為前綴選項指定一個值。
ts
setCookie(c, 'delicious_cookie', 'macha', {
prefix: 'secure', // or `host`
})
await setSignedCookie(
c,
'delicious_cookie',
'macha',
'secret choco chips',
{
prefix: 'secure', // or `host`
}
)
遵循最佳實踐
新的 Cookie RFC (又稱 cookie-bis) 和 CHIPS 包含開發人員應遵循的一些 Cookie 設定最佳實踐。
- RFC6265bis-13
Max-Age
/Expires
限制__Host-
/__Secure-
前綴限制
- CHIPS-01
Partitioned
限制
Hono 正在遵循最佳實踐。當在以下條件下解析 Cookie 時,Cookie 輔助工具會拋出 Error
- Cookie 名稱以
__Secure-
開頭,但未設定secure
選項。 - Cookie 名稱以
__Host-
開頭,但未設定secure
選項。 - Cookie 名稱以
__Host-
開頭,但path
不是/
。 - Cookie 名稱以
__Host-
開頭,但已設定domain
。 maxAge
選項值大於 400 天。expires
選項值比目前時間晚 400 天。