import{createSignedCookie,verifySignedCookie,}from'https://deno.land/x/squishy_cookies/mod.ts'constCOOKIE_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 }=awaitcreateSignedCookie('id','1',COOKIE_SECRET,{httpOnly: true,path: '/'})returnnewResponse(page,{ headers })// orconstheaders=newHeaders()headers.append('set-cookie',cookie)// 2. Verifying a cookieheaders.append('cookie',cookie)// verifySignedCookie will search for 'cookie' headerconstuserId=awaitverifySignedCookie(headers,'id',COOKIE_SECRET)// userId is false if the verification failed or the cookie valueif(userId){constuser=awaitgetUserById(userId.split('.')[0])}
Tip to add multiple cookies
const{cookie: cookie1}=awaitcreateSignedCookie(/* ... */)const{cookie: cookie2}=awaitcreateSignedCookie(/* ... */)constresponse=newResponse(someHTML,{// using headers as an array of arrays let you use// multiple headers with the same nameheaders: [['Set-cookie',cookie1],['Set-cookie',cookie2],]})
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 ?