UNPKG

next-session

Version:

Simple promise-based session for Next.js

42 lines (31 loc) 974 B
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } export default class MemoryStore { constructor() { _defineProperty(this, "store", void 0); this.store = new Map(); } async get(sid) { const sess = this.store.get(sid); if (sess) { const session = JSON.parse(sess, (key, value) => { if (key === "expires") return new Date(value); return value; }); if (session.cookie.expires && session.cookie.expires.getTime() <= Date.now()) { await this.destroy(sid); return null; } return session; } return null; } async set(sid, sess) { this.store.set(sid, JSON.stringify(sess)); } async destroy(sid) { this.store.delete(sid); } async touch(sid, sess) { this.store.set(sid, JSON.stringify(sess)); } }