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:
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user