@marmeon/session
v0.3.0
Published
Sessions for Marmeon apps: an injectable session, file, database, Redis and memory stores, flash data and CSRF protection.
Readme
@marmeon/session
Sessions for Marmeon apps: an injectable session on file, database, Redis or memory stores, flash data and CSRF protection.
Installation
Part of every new Marmeon app: pnpm create marmeon installs it.
The database driver needs @marmeon/database, which a new app has, and the redis driver @marmeon/redis, which brings the
Redis client: pnpm add @marmeon/redis. A driver whose package is missing stops the start with the command that installs it.
Using the session
import type { Authenticated } from '@marmeon/auth';
import type { Row } from '@marmeon/database';
import { Controller, type HttpContext } from '@marmeon/http';
import { Session } from '@marmeon/session';
declare module '@marmeon/session' {
interface SessionData {
/** The notes this visitor opened last, newest first. */
recentNotes: number[];
}
}
export class ShowNoteController extends Controller {
readonly #session: Session;
constructor(session: Session) {
super();
this.#session = session;
}
handle(ctx: HttpContext<Authenticated, { note: Row<'notes'> }>) {
const { note } = ctx.params;
const recent = this.#session.get('recentNotes') ?? [];
this.#session.put('recentNotes', [note.id, ...recent.filter((id) => id !== note.id)].slice(0, 5));
return this.view('notes/Show', { note, recent });
}
}Documentation
MIT · part of Marmeon
