Current User
A single module-level slot for “the logged-in user”, set and read from anywhere.
API
import { setCurrentUser, getCurrentUser } from "@mongez/user";
setCurrentUser(user); // store a user instancegetCurrentUser(); // retrieve it (returns the same User, or undefined)Caveats
- It’s a module-level variable. In a Node process serving multiple requests, all requests share the slot. This is fine in a browser (one user per tab) but a footgun in SSR — do NOT use it server-side; thread the user instance through your request context instead.
- Tests need to reset it between cases. Either call
setCurrentUser(undefined as any)inafterEach, or never callsetCurrentUserin a test that the next test will run after. getCurrentUser()is typedUser | undefined— it returnsundefineduntilsetCurrentUserhas been called, so always null-check (or use?.) before calling methods on the result.
Lifecycle
The pointer doesn’t auto-update on logout(). If your code reads getCurrentUser()?.isLoggedIn(), that returns false after logout (because the user instance reports it), but getCurrentUser() itself still returns the same instance. There’s no way to “unset” the slot except by setting it to undefined:
user.logout();setCurrentUser(undefined as any);Most apps don’t need to do this — they just check isLoggedIn() and gate on that.