UNPKG

next-session

Version:

Simple promise-based session for Next.js

51 lines (37 loc) 1.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; 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; } 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)); } } exports.default = MemoryStore;