Skip to content

Question about Session Cookies (Cloudflare Worker) #2434

Answered by NicoPlyley
kennyth01 asked this question in Q&A
Discussion options

You must be logged in to vote

If you wrap it like this, you won't have to worry about types

import { createMiddleware } from 'hono/factory'

export const cookieSessionMiddleware = createMiddleware(async (c, next) => {
	const store = new CookieStore()

	const m = sessionMiddleware({
		store,
		encryptionKey: c.env.SESSION_ENCRYPTION_KEY,
		expireAfterSeconds: 604800,
		cookieOptions: {
			sameSite: 'Lax',
			path: '/',
			httpOnly: true,
		},
	})

	return m(c, next)
})

You can also add <{ Bindings: Env}> if need to createMiddleware

Replies: 4 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
5 replies
@NicoPlyley
Comment options

@InsaneNaman
Comment options

@InsaneNaman
Comment options

@InsaneNaman
Comment options

@NicoPlyley
Comment options

Comment options

You must be logged in to vote
1 reply
@NicoPlyley
Comment options

Answer selected by kennyth01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants