@krak-stack/auth 0.2.13 → 0.2.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,16 @@
1
+ import { Cause, Schema } from "effect";
2
+ import { AsyncResult } from "effect/unstable/reactivity";
1
3
  import { type ReactNode } from "react";
2
4
  import type { ExtraProjectPublicConfig } from "../extra/schema.js";
3
5
  import type { ProjectAccessCatalog, ProjectAccessLabelCatalog } from "../access.js";
4
6
  export type KrakstackAuthLocale = "en" | "fr";
7
+ export type KrakstackAuthStatus = "loading" | "ready" | "degraded" | "error";
8
+ declare const KrakstackAuthUnavailableError_base: Schema.Class<KrakstackAuthUnavailableError, Schema.TaggedStruct<"KrakstackAuthUnavailableError", {
9
+ readonly cause: Schema.Defect;
10
+ }>, Cause.YieldableError>;
11
+ export declare class KrakstackAuthUnavailableError extends KrakstackAuthUnavailableError_base {
12
+ readonly status = 503;
13
+ }
5
14
  export type KrakstackAuthProviderProps = {
6
15
  children: ReactNode;
7
16
  locale?: KrakstackAuthLocale | undefined;
@@ -14,10 +23,18 @@ export type KrakstackAuthContextValue = {
14
23
  locale: KrakstackAuthLocale;
15
24
  baseUrl?: string | undefined;
16
25
  projectId?: string | null | undefined;
26
+ status: KrakstackAuthStatus;
27
+ error: KrakstackAuthUnavailableError | null;
17
28
  projectConfig: ExtraProjectPublicConfig | null;
18
29
  access: ProjectAccessCatalog | null;
19
30
  accessLabels: ProjectAccessLabelCatalog | null;
20
31
  };
32
+ type ProjectConfigState = {
33
+ status: KrakstackAuthStatus;
34
+ error: KrakstackAuthUnavailableError | null;
35
+ projectConfig: ExtraProjectPublicConfig | null;
36
+ };
37
+ export declare const resolveProjectConfigState: (result: AsyncResult.AsyncResult<ExtraProjectPublicConfig | null, unknown>) => ProjectConfigState;
21
38
  export declare function KrakstackAuthProvider({ children, locale, baseUrl, projectId, access, accessLabels, }: KrakstackAuthProviderProps): import("react").JSX.Element;
22
39
  export declare const useKrakstackAuth: () => KrakstackAuthContextValue | null;
23
40
  export declare const useKrakstackAuthProjectConfig: () => {
@@ -33,6 +50,11 @@ export declare const useKrakstackAuthProjectConfig: () => {
33
50
  readonly google: boolean;
34
51
  };
35
52
  } | null;
53
+ export type KrakstackAuthRequiredProps = {
54
+ children: ReactNode;
55
+ fallback?: ReactNode | undefined;
56
+ };
57
+ export declare const KrakstackAuthRequired: ({ children, fallback, }: KrakstackAuthRequiredProps) => ReactNode;
36
58
  export declare const usePermissions: () => {
37
59
  loading: boolean;
38
60
  permissions: Set<string>;
@@ -40,3 +62,4 @@ export declare const usePermissions: () => {
40
62
  canAll: (required: ReadonlyArray<string>) => boolean;
41
63
  canAny: (required: ReadonlyArray<string>) => boolean;
42
64
  };
65
+ export {};
@@ -1,11 +1,18 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { useAtomSuspense, useAtomValue } from "@effect/atom-react";
2
+ import { useAtomValue } from "@effect/atom-react";
3
3
  import { useRouterState } from "@tanstack/react-router";
4
- import { AsyncResult } from "effect/unstable/reactivity";
4
+ import { Cause, Option, Schema } from "effect";
5
+ import { HttpApiError } from "effect/unstable/httpapi";
6
+ import { AsyncResult, Atom } from "effect/unstable/reactivity";
5
7
  import { createContext, useContext, useEffect, useMemo, useRef, } from "react";
6
8
  import { activeAuthOrganizationAtom, authSessionAtom } from "./auth-atoms.js";
7
9
  import { authClientApi } from "./auth-client-api.js";
8
10
  import { parseRoleList } from "../roles.js";
11
+ export class KrakstackAuthUnavailableError extends Schema.TaggedError()("KrakstackAuthUnavailableError", {
12
+ cause: Schema.Defect(),
13
+ }) {
14
+ status = 503;
15
+ }
9
16
  const KrakstackAuthContext = createContext(null);
10
17
  const PROJECT_CONTEXT_COOKIE = "krakstack-auth.project_context";
11
18
  const setProjectContextCookie = (projectId) => {
@@ -42,7 +49,28 @@ const getRedirectHost = (searchString) => {
42
49
  return null;
43
50
  }
44
51
  };
45
- const useProjectConfig = (baseUrl, providedProjectId) => {
52
+ const serverProjectConfigAtom = Atom.make(AsyncResult.initial());
53
+ export const resolveProjectConfigState = (result) => {
54
+ if (AsyncResult.isFailure(result)) {
55
+ const projectConfig = AsyncResult.getOrElse(result, () => null);
56
+ return {
57
+ status: Option.isSome(result.previousSuccess) ? "degraded" : "error",
58
+ error: new KrakstackAuthUnavailableError({
59
+ cause: Cause.squash(result.cause),
60
+ }),
61
+ projectConfig,
62
+ };
63
+ }
64
+ if (AsyncResult.isSuccess(result)) {
65
+ return {
66
+ status: "ready",
67
+ error: null,
68
+ projectConfig: result.value,
69
+ };
70
+ }
71
+ return { status: "loading", error: null, projectConfig: null };
72
+ };
73
+ const useProjectConfigState = (baseUrl, providedProjectId) => {
46
74
  const searchString = useRouterState({
47
75
  select: (state) => state.location.searchStr,
48
76
  });
@@ -59,26 +87,29 @@ const useProjectConfig = (baseUrl, providedProjectId) => {
59
87
  query.host = host;
60
88
  if (rootHost)
61
89
  query.rootHost = rootHost;
62
- const result = useAtomSuspense(authClientApi(baseUrl).query("authExtra", "getProjectPublicConfig", {
63
- query,
64
- timeToLive: "5 minutes",
65
- reactivityKeys: [
66
- "project-public-config",
67
- ...(projectId ? [`project:${projectId}`] : []),
68
- ...(clientId ? [`client:${clientId}`] : []),
69
- ...(host ? [`host:${host}`] : []),
70
- ...(rootHost ? [`root-host:${rootHost}`] : []),
71
- ],
72
- serializationKey: `project-public-config:${projectId ?? ""}:${clientId ?? ""}:${host ?? ""}:${rootHost ?? ""}`,
73
- }), { suspendOnWaiting: true });
74
- return result.value;
90
+ const projectConfigAtom = globalThis.window
91
+ ? authClientApi(baseUrl).query("authExtra", "getProjectPublicConfig", {
92
+ query,
93
+ timeToLive: "5 minutes",
94
+ reactivityKeys: [
95
+ "project-public-config",
96
+ ...(projectId ? [`project:${projectId}`] : []),
97
+ ...(clientId ? [`client:${clientId}`] : []),
98
+ ...(host ? [`host:${host}`] : []),
99
+ ...(rootHost ? [`root-host:${rootHost}`] : []),
100
+ ],
101
+ serializationKey: `project-public-config:${projectId ?? ""}:${clientId ?? ""}:${host ?? ""}:${rootHost ?? ""}`,
102
+ })
103
+ : serverProjectConfigAtom;
104
+ const result = useAtomValue(projectConfigAtom);
105
+ return useMemo(() => resolveProjectConfigState(result), [result]);
75
106
  };
76
107
  export function KrakstackAuthProvider({ children, locale = "en", baseUrl, projectId, access, accessLabels, }) {
77
108
  const searchString = useRouterState({
78
109
  select: (state) => state.location.searchStr,
79
110
  });
80
111
  const resolvedProjectId = projectId ?? getSearchParam(searchString, "projectId");
81
- const projectConfig = useProjectConfig(baseUrl, projectId);
112
+ const projectConfigState = useProjectConfigState(baseUrl, projectId);
82
113
  const sessionAtom = authSessionAtom(baseUrl);
83
114
  const sessionResult = useAtomValue(sessionAtom);
84
115
  const wasAuthenticated = useRef(false);
@@ -101,14 +132,32 @@ export function KrakstackAuthProvider({ children, locale = "en", baseUrl, projec
101
132
  baseUrl,
102
133
  locale,
103
134
  projectId: resolvedProjectId,
104
- projectConfig,
135
+ ...projectConfigState,
105
136
  access: access ?? null,
106
137
  accessLabels: accessLabels ?? null,
107
- }), [baseUrl, locale, resolvedProjectId, projectConfig, access, accessLabels]);
138
+ }), [
139
+ baseUrl,
140
+ locale,
141
+ resolvedProjectId,
142
+ projectConfigState,
143
+ access,
144
+ accessLabels,
145
+ ]);
108
146
  return (_jsx(KrakstackAuthContext.Provider, { value: value, children: children }));
109
147
  }
110
148
  export const useKrakstackAuth = () => useContext(KrakstackAuthContext);
111
149
  export const useKrakstackAuthProjectConfig = () => useKrakstackAuth()?.projectConfig ?? null;
150
+ export const KrakstackAuthRequired = ({ children, fallback = null, }) => {
151
+ const auth = useKrakstackAuth();
152
+ if (!auth) {
153
+ throw new Error("KrakstackAuthProvider is required to use KrakstackAuthRequired.");
154
+ }
155
+ if (auth.status === "loading")
156
+ return fallback;
157
+ if (auth.status === "error")
158
+ throw auth.error;
159
+ return children;
160
+ };
112
161
  export const usePermissions = () => {
113
162
  const auth = useKrakstackAuth();
114
163
  if (!auth) {
@@ -1,4 +1,4 @@
1
- export { KrakstackAuthProvider, useKrakstackAuth, useKrakstackAuthProjectConfig, usePermissions, } from "./auth-provider.js";
1
+ export { KrakstackAuthProvider, KrakstackAuthRequired, KrakstackAuthUnavailableError, useKrakstackAuth, useKrakstackAuthProjectConfig, usePermissions, } from "./auth-provider.js";
2
2
  export { activeAuthOrganizationAtom, authOrganizationsAtom, authSessionAtom, } from "./auth-atoms.js";
3
3
  export { OrganizationSwitcher } from "./organization-switcher.js";
4
4
  export { MemberRequired } from "./member-required.js";
@@ -14,7 +14,7 @@ export { ApiKeyPermissionGrant, CurrentActor, Forbidden, all, actorUserId, any,
14
14
  export type { ApiKeyOwner, ApiKeyActor, Actor, Policy, ProjectAccessConfig, ProjectAccessCatalog, ProjectAccessLabelCatalog, ProjectAccessDefinition, ProjectAccessLabels, UserActor, } from "../access.js";
15
15
  export type { ProjectAccessMatrixAccess, ProjectAccessMatrixMessages, } from "./project-access-matrix.js";
16
16
  export type { AdminOrganizationsMessages } from "./admin-organizations.js";
17
- export type { KrakstackAuthContextValue, KrakstackAuthLocale, KrakstackAuthProviderProps, } from "./auth-provider.js";
17
+ export type { KrakstackAuthContextValue, KrakstackAuthLocale, KrakstackAuthProviderProps, KrakstackAuthRequiredProps, KrakstackAuthStatus, } from "./auth-provider.js";
18
18
  export type { OrganizationSwitcherDialog, OrganizationSwitcherFeatures, OrganizationSwitcherProps, } from "./organization-switcher.js";
19
19
  export type { UserButtonDialog, UserButtonProps } from "./user-button.js";
20
20
  export type { MemberRequiredMessages, MemberRequiredProps, } from "./member-required.js";
@@ -1,4 +1,4 @@
1
- export { KrakstackAuthProvider, useKrakstackAuth, useKrakstackAuthProjectConfig, usePermissions, } from "./auth-provider.js";
1
+ export { KrakstackAuthProvider, KrakstackAuthRequired, KrakstackAuthUnavailableError, useKrakstackAuth, useKrakstackAuthProjectConfig, usePermissions, } from "./auth-provider.js";
2
2
  export { activeAuthOrganizationAtom, authOrganizationsAtom, authSessionAtom, } from "./auth-atoms.js";
3
3
  export { OrganizationSwitcher } from "./organization-switcher.js";
4
4
  export { MemberRequired } from "./member-required.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@krak-stack/auth",
3
- "version": "0.2.13",
3
+ "version": "0.2.14",
4
4
  "description": "Effect HttpApi clients and schemas for KrakStack Auth.",
5
5
  "license": "MIT",
6
6
  "files": [