vojo/src/app/hooks/useAuthedUserId.ts

16 lines
793 B
TypeScript

import { useMatrixClient } from './useMatrixClient';
// Returns the current user's MXID in any subtree that mounts only after
// authentication has resolved (`<ClientLayout>` and everything inside it).
// Throws if the SDK has no user ID — that's an invariant violation, not a
// soft-failure path: by the time post-login components render, the SDK
// already has a session. Prefer this over `mx.getUserId() ?? ''` so a
// broken invariant surfaces loudly instead of silently defaulting to '' and
// writing empty-string user IDs into account-data / storage keys.
//
// For utility functions that aren't React hooks, call `mx.getSafeUserId()`
// directly — it's the same contract.
export function useAuthedUserId(): string {
const mx = useMatrixClient();
return mx.getSafeUserId();
}