You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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],]})