Rework user types: create AuthenticatedUser and AnonymousUser class

Both are subclasses of an abstract User class which contains almost everything
interesting.
This commit is contained in:
2026-01-17 17:45:36 -06:00
parent 350bf7c865
commit d921679058
9 changed files with 102 additions and 62 deletions

View File

@@ -5,10 +5,10 @@
// needing to pass Call through every function.
import { AsyncLocalStorage } from "node:async_hooks";
import { AnonymousUser, type MaybeUser } from "./user";
import { anonymousUser, type User } from "./user";
type RequestContext = {
user: MaybeUser;
user: User;
};
const asyncLocalStorage = new AsyncLocalStorage<RequestContext>();
@@ -19,9 +19,9 @@ function runWithContext<T>(context: RequestContext, fn: () => T): T {
}
// Get the current user from context, or AnonymousUser if not in a request
function getCurrentUser(): MaybeUser {
function getCurrentUser(): User {
const context = asyncLocalStorage.getStore();
return context?.user ?? AnonymousUser;
return context?.user ?? anonymousUser;
}
export { getCurrentUser, runWithContext, type RequestContext };