dh_demo

DreamHanks demo project
git clone git://git.lair.cx/dh_demo
Log | Files | Refs | README

session.ts (689B)


      1 import { getRedis } from '@/lib/redis'
      2 
      3 export interface Session {
      4   id: string // `tid` in token
      5   uid: number
      6   aclGroups?: string[]
      7 }
      8 
      9 export function sessionKey (sid: string) {
     10   return `sess:${sid}`
     11 }
     12 
     13 export async function putSession (sess: Session) {
     14   const redis = await getRedis()
     15   await redis.set(sessionKey(sess.id), JSON.stringify(sess))
     16 }
     17 
     18 export async function getSession (sid: string) {
     19   const redis = await getRedis()
     20   const data = await redis.get(sessionKey(sid))
     21   if (data == null) {
     22     return null
     23   }
     24   return JSON.parse(data) as Session
     25 }
     26 
     27 export async function deleteSession (sid: string) {
     28   const redis = await getRedis()
     29   await redis.del(sessionKey(sid))
     30 }