Hono provides utilities to help you handle cookies.
The basic ones are getCookie()
, setCookie()
and deleteCookie()
:
import { getCookie, setCookie, deleteCookie } from 'hono/cookie'
setCookie('username', 'joe')
You can set all the cookie options passing an object as 3rd parameter:
setCookie('username', 'Flavio', {
domain: 'thevalleyofcode.com',
path: '/administrator',
expires: new Date(Date.now() + 900000),
httpOnly: true
})
The most useful parameters you can set are:
Value | Description |
---|---|
domain | The cookie domain name |
expires | Set the cookie expiration date. If missing, or 0, the cookie is a session cookie |
httpOnly | Set the cookie to be accessible only by the web server. See HttpOnly |
maxAge | Set the expiry time relative to the current time, expressed in milliseconds |
path | The cookie path. Set to ’/’ to apply to the whole site |
secure | Marks the cookie HTTPS only |
signed | Set the cookie to be signed |
sameSite | Value of SameSite |
Get a cookie using:
const cookie = getCookie('username')
A cookie can be cleared with:
deleteCookie('username')
Then we have 2 more functions to work with signed cookies:
import { getSignedCookie, setSignedCookie } from 'hono/cookie'
They work in the same way but have a signature so you can detect if the client modified the cookie.
Lessons in this unit:
0: | Introduction |
1: | Your first Hono app |
2: | The Request object |
3: | Send a response to the client |
4: | ▶︎ Manage cookies |
5: | Work with HTTP headers |
6: | Handling redirects |
7: | Routing |
8: | JSX templates |
9: | Middleware |
10: | Hono on Node.js |
11: | Handling forms COMING SOON |
Are you intimidated by Git? Can’t figure out
merge vs rebase? Are you afraid of screwing up
something any time you have to do something in
Git? Do you rely on ChatGPT or random people’s
answer on StackOverflow to fix your problems?
Your coworkers are tired of explaining Git to
you all the time? Git is something we all need
to use, but few of us really master it. I
created this course to improve your Git (and
GitHub) knowledge at a radical level. Launching
May 21, 2024. Join the waiting list!