diff --git a/apps/mobile/src/state/session.test.ts b/apps/mobile/src/state/session.test.ts index b47054e258663..8040952f4adc3 100644 --- a/apps/mobile/src/state/session.test.ts +++ b/apps/mobile/src/state/session.test.ts @@ -21,7 +21,8 @@ vi.mock("@effect/atom-react", async () => { }; }); vi.mock("../connection/runtime", () => ({ connectionAtomRuntime: {} })); -vi.mock("@t3tools/client-runtime/state/session", () => ({ +vi.mock("@t3tools/client-runtime/state/session", async (importOriginal) => ({ + ...(await importOriginal()), createEnvironmentSessionAtoms: () => ({ sessionStateAtom: Atom.family((_id: EnvironmentId) => Atom.make(AsyncResult.initial()).pipe(Atom.keepAlive), diff --git a/apps/mobile/src/state/session.ts b/apps/mobile/src/state/session.ts index 5d6a441e9b9e4..02e39278de2da 100644 --- a/apps/mobile/src/state/session.ts +++ b/apps/mobile/src/state/session.ts @@ -1,10 +1,12 @@ import { useAtomValue } from "@effect/atom-react"; -import { createEnvironmentSessionAtoms } from "@t3tools/client-runtime/state/session"; +import { + createEnvironmentSessionAtoms, + sessionHasScope, +} from "@t3tools/client-runtime/state/session"; import { type AuthEnvironmentScope, type AuthSessionState, type EnvironmentId, - sessionGrantsScope, } from "@t3tools/contracts"; import * as Option from "effect/Option"; import { AsyncResult, Atom } from "effect/reactivity"; @@ -17,13 +19,7 @@ export const environmentSession = createEnvironmentSessionAtoms(connectionAtomRu const EMPTY_SESSION_STATE_ATOM = Atom.make(AsyncResult.initial()); -export function sessionHasScope( - result: AsyncResult.AsyncResult, - scope: AuthEnvironmentScope, -): boolean { - const session = AsyncResult.getOrElse(result, () => null); - return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope); -} +export { sessionHasScope } from "@t3tools/client-runtime/state/session"; export function useEnvironmentScope( environmentId: EnvironmentId | null, diff --git a/apps/web/src/state/session.ts b/apps/web/src/state/session.ts index 2c2de0a107d36..2b4881eae56b7 100644 --- a/apps/web/src/state/session.ts +++ b/apps/web/src/state/session.ts @@ -1,10 +1,12 @@ import { useAtomValue } from "@effect/atom-react"; -import { createEnvironmentSessionAtoms } from "@t3tools/client-runtime/state/session"; +import { + createEnvironmentSessionAtoms, + sessionHasScope, +} from "@t3tools/client-runtime/state/session"; import { type AuthEnvironmentScope, type AuthSessionState, type EnvironmentId, - sessionGrantsScope, } from "@t3tools/contracts"; import * as Option from "effect/Option"; import { AsyncResult, Atom } from "effect/reactivity"; @@ -34,13 +36,7 @@ export function useEnvironmentScope( ); } -export function sessionHasScope( - result: AsyncResult.AsyncResult, - scope: AuthEnvironmentScope, -): boolean { - const session = AsyncResult.getOrElse(result, () => null); - return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope); -} +export { sessionHasScope } from "@t3tools/client-runtime/state/session"; const environmentsWithScopeAtom = Atom.family((key: string) => { const [scope, idsKey = ""] = key.split(/\n(.*)/s) as [AuthEnvironmentScope, string?]; diff --git a/packages/client-runtime/src/state/session.ts b/packages/client-runtime/src/state/session.ts index 9312ae048e6e0..ea81379910b61 100644 --- a/packages/client-runtime/src/state/session.ts +++ b/packages/client-runtime/src/state/session.ts @@ -1,5 +1,11 @@ import { pipe } from "effect/Function"; -import type { AuthSessionState, EnvironmentId, ServerConfig } from "@t3tools/contracts"; +import { + type AuthEnvironmentScope, + type AuthSessionState, + type EnvironmentId, + type ServerConfig, + sessionGrantsScope, +} from "@t3tools/contracts"; import * as Schema from "effect/Schema"; import * as Effect from "effect/Effect"; import * as Option from "effect/Option"; @@ -238,3 +244,11 @@ export function createEnvironmentSessionAtoms( sessionAtomsByRuntime.set(runtime, atoms); return atoms; } + +export function sessionHasScope( + result: AsyncResult.AsyncResult, + scope: AuthEnvironmentScope, +): boolean { + const session = AsyncResult.getOrElse(result, () => null); + return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope); +}