Adding authentication
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 ...
Get TypeScript: Modern JavaScript Development now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.