In lib/server/index.ts
, we will first add sessions. A session is a place to store data, which is persistent for a client on the server. On the client side, a cookie will be saved, which contains an identifier of the session. If a request contains a valid cookie with such an identifier, you will get the same session object. Otherwise, a new session will be created:
import { Server, ServerRequest, ServerResponse, ServerError, StatusCode, SessionStore } from "phaethon"; import { ObjectID } from "mongodb"; import { User, login, logout } from "./user"; import * as note from "./note";
Tip
With import { ... }
, we can import a set of entities from another file. With import * as ...
, we import the whole file as an object. The following two snippets are equivalent:
import * as foo from "./foo"; foo.bar(); import { bar } from "./foo"; bar();
We define the type of the content of the session as follows:
export interface Session { userId: ObjectID; ...