Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/mobile/src/state/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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<typeof import("@t3tools/client-runtime/state/session")>()),
createEnvironmentSessionAtoms: () => ({
sessionStateAtom: Atom.family((_id: EnvironmentId) =>
Atom.make(AsyncResult.initial<AuthSessionState, Error>()).pipe(Atom.keepAlive),
Expand Down
14 changes: 5 additions & 9 deletions apps/mobile/src/state/session.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -17,13 +19,7 @@ export const environmentSession = createEnvironmentSessionAtoms(connectionAtomRu

const EMPTY_SESSION_STATE_ATOM = Atom.make(AsyncResult.initial<AuthSessionState>());

export function sessionHasScope(
result: AsyncResult.AsyncResult<AuthSessionState, unknown>,
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,
Expand Down
14 changes: 5 additions & 9 deletions apps/web/src/state/session.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -33,13 +35,7 @@ export function useEnvironmentScope(
return sessionHasScope(result, scope);
}

export function sessionHasScope(
result: AsyncResult.AsyncResult<AuthSessionState, unknown>,
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?];
Expand Down
16 changes: 15 additions & 1 deletion packages/client-runtime/src/state/session.ts
Original file line number Diff line number Diff line change
@@ -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 Data from "effect/Data";
import * as Effect from "effect/Effect";
import * as Option from "effect/Option";
Expand Down Expand Up @@ -237,3 +243,11 @@ export function createEnvironmentSessionAtoms<R, E>(
sessionAtomsByRuntime.set(runtime, atoms);
return atoms;
}

export function sessionHasScope(
result: AsyncResult.AsyncResult<AuthSessionState, unknown>,
scope: AuthEnvironmentScope,
): boolean {
const session = AsyncResult.getOrElse(result, () => null);
return result._tag !== "Failure" && session !== null && sessionGrantsScope(session, scope);
}
Loading