diff --git a/express/routes.ts b/express/routes.ts index 26028b2..3e31e7f 100644 --- a/express/routes.ts +++ b/express/routes.ts @@ -59,6 +59,29 @@ const routes: Route[] = [ }; }, }, + { + path: "/whoami", + methods: ["GET"], + handler: async (_call: Call): Promise => { + const me = services.session.getUser(); + const template = ` + + + + {{ me }} + + +`; + + const result = nunjucks.renderString(template, { me }); + + return { + code: httpCodes.success.OK, + contentType: contentTypes.text.html, + result, + }; + }, + }, { path: "/ok", methods: ["GET", "POST", "PUT"], diff --git a/express/services/index.ts b/express/services/index.ts index 0d22f51..ec28bcb 100644 --- a/express/services/index.ts +++ b/express/services/index.ts @@ -3,6 +3,7 @@ import { AuthService, InMemoryAuthStore } from "../auth"; import { config } from "../config"; import { getLogs, log } from "../logging"; +import { AnonymousUser, anonymousUser, type User } from "../user"; //const database = Client({ @@ -27,16 +28,24 @@ const misc = { }, }; +const session = { + getUser: (): User => { + return anonymousUser; + }, +}; + // Initialize auth with in-memory store const authStore = new InMemoryAuthStore(); const auth = new AuthService(authStore); +// Keep this asciibetically sorted const services = { + auth, database, logging, misc, random, - auth, + session, }; export { services }; diff --git a/express/user.ts b/express/user.ts index 078a344..6c32646 100644 --- a/express/user.ts +++ b/express/user.ts @@ -181,8 +181,19 @@ export class User { toJSON(): UserData { return { ...this.data }; } + + toString(): string { + return `User(id ${this.id})`; + } } // For representing "no user" in contexts where user is optional export const AnonymousUser = Symbol("AnonymousUser"); + +export const anonymousUser = User.create("anonymous@example.com", { + id: "-1", + displayName: "Anonymous User", + // FIXME: set createdAt and updatedAt to start of epoch +}); + export type MaybeUser = User | typeof AnonymousUser;