@aiveth/react 0.5.0 → 0.7.0

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.7.0 — 2026-11-26
4
+
5
+ - `client.myWorkspace` — the signed-in workspace from your own pages (Aiveth's My workspace API): `get`, `members`, `setRole`, `remove` (or leave), `invitations`, `invite`, `withdraw`. Set `audience` to `myWorkspaceAudience(issuer)` and sign in with `workspace`.
6
+
7
+ ## 0.6.0 — 2026-11-26
8
+
9
+ - `signIn({ workspace })` signs the person in to a workspace; `switchWorkspace(ref)` (client and `useAiveth()`) switches it with the refresh token. `user` carries `workspace_id`, `workspace_slug`, `workspace_role`, `workspace_permissions`, and `workspaces` with the scope of that name.
10
+
3
11
  ## 0.5.0 — 2026-11-26
4
12
 
5
13
  - `audience` (on `AivethProvider` / `AivethClient`): your own API's address. The access token is then a signed token for that API, which it checks with `verifyAccessToken` (`@aiveth/node`).
package/README.md CHANGED
@@ -23,6 +23,11 @@ const token = await getAccessToken(); // Authorization: Bearer … for your API
23
23
  - Refresh tokens (`offline_access`, asked by default) are swapped at every use.
24
24
  - One app belongs to one organization in Aiveth, so there is no organization switcher.
25
25
 
26
+ ## Workspaces
27
+
28
+ `signIn({ workspace: 'acme' })` signs in to a workspace; `switchWorkspace('acme-two')` switches it
29
+ (needs `offline_access`). `user.workspace_id`, `workspace_role` and `workspace_permissions` follow.
30
+
26
31
  ## A token for your own API
27
32
 
28
33
  Add `audience="https://api.your-app.com"` (registered under Developers → Your APIs) to
package/dist/client.d.ts CHANGED
@@ -16,6 +16,36 @@ export interface AivethConfig {
16
16
  storage?: Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>;
17
17
  navigate?: (url: string) => void;
18
18
  }
19
+ /** The audience of Aiveth's My workspace API for this issuer. */
20
+ export declare function myWorkspaceAudience(issuer: string): string;
21
+ export interface MyWorkspace {
22
+ workspace: {
23
+ id: string;
24
+ name: string;
25
+ slug: string;
26
+ external_id: string | null;
27
+ members: number;
28
+ created_at: string;
29
+ };
30
+ role: string;
31
+ permissions: string[];
32
+ is_admin: boolean;
33
+ }
34
+ export interface WorkspaceMember {
35
+ user_id: string;
36
+ email: string;
37
+ name: string;
38
+ role: string;
39
+ since: string;
40
+ }
41
+ export interface WorkspaceInvitationView {
42
+ id: string;
43
+ email: string;
44
+ role: string;
45
+ invited_at: string;
46
+ expires_at: string;
47
+ new_person: boolean;
48
+ }
19
49
  /** Someone acting as the person (Aiveth's "Acting for others"; RFC 8693's `act` claim). */
20
50
  export interface Actor {
21
51
  /** The support person's Aiveth id. */
@@ -39,6 +69,18 @@ export interface User {
39
69
  picture?: string;
40
70
  /** The organization's feature flags that are on for this person — present once it has one. */
41
71
  feature_flags?: string[];
72
+ /** The active workspace (`signIn({ workspace })`, or `switchWorkspace`). */
73
+ workspace_id?: string;
74
+ workspace_slug?: string;
75
+ workspace_role?: string;
76
+ workspace_permissions?: string[];
77
+ /** With the `workspaces` scope: every workspace the person is in, for your switcher. */
78
+ workspaces?: {
79
+ id: string;
80
+ slug: string;
81
+ name: string;
82
+ role: string;
83
+ }[];
42
84
  [claim: string]: unknown;
43
85
  }
44
86
  /** What `handleRedirect` did: `handled` — this load was a return from Aiveth (clean the address
@@ -76,13 +118,40 @@ export declare class AivethClient {
76
118
  private changed;
77
119
  private discover;
78
120
  /** To Aiveth's sign-in page; back at `redirectUri`, `handleRedirect` finishes. */
79
- signIn({ returnTo }?: {
121
+ signIn({ returnTo, workspace }?: {
80
122
  returnTo?: string;
123
+ workspace?: string;
81
124
  }): Promise<void>;
82
125
  /** On the return address: swap the code for tokens. Answers where the app wanted to go
83
126
  * (`returnTo`), or null when this page load isn't a return from Aiveth. */
84
127
  handleRedirect(url: string): Promise<Returned>;
85
128
  private exchange;
129
+ /** The signed-in workspace from your own pages (Aiveth's My workspace API): set `audience` to
130
+ * `myWorkspaceAudience(issuer)` and sign in with `workspace`. Every member reads; only the
131
+ * workspace's admins change anything. */
132
+ readonly myWorkspace: {
133
+ get: () => Promise<MyWorkspace>;
134
+ members: (offset?: number) => Promise<{
135
+ members: WorkspaceMember[];
136
+ total: number;
137
+ offset: number;
138
+ page_size: number;
139
+ }>;
140
+ setRole: (userId: string, role: string) => Promise<void>;
141
+ /** An admin removes someone; anyone may leave with their own id. */
142
+ remove: (userId: string) => Promise<void>;
143
+ invitations: () => Promise<WorkspaceInvitationView[]>;
144
+ invite: (email: string, role?: string) => Promise<{
145
+ invitation: WorkspaceInvitationView;
146
+ emailed: boolean;
147
+ link: string | null;
148
+ }>;
149
+ withdraw: (invitationId: string) => Promise<void>;
150
+ };
151
+ private mine;
152
+ /** Switch the active workspace (id or slug): the new tokens name it, and `user` changes with
153
+ * them. The person must be a member. Needs the refresh token (`offline_access`). */
154
+ switchWorkspace(workspace: string): Promise<void>;
86
155
  /** A live access token for calling your API — renewed with the refresh token when it is near
87
156
  * its end; null when signed out (or the refresh token has ended: sign in again). */
88
157
  getAccessToken(): Promise<string | null>;
package/dist/client.js CHANGED
@@ -7,6 +7,10 @@
7
7
  * A reload forgets the tokens: `signIn()` again comes straight back while the person is still
8
8
  * signed in at Aiveth. The app must be a PUBLIC app (no secret) whose return address is
9
9
  * `redirectUri`; its origin may then call Aiveth's token endpoint (CORS). */
10
+ /** The audience of Aiveth's My workspace API for this issuer. */
11
+ export function myWorkspaceAudience(issuer) {
12
+ return `${issuer.replace(/\/+$/, '')}/my-workspace`;
13
+ }
10
14
  const PENDING = 'aiveth:pending';
11
15
  const DEFAULT_SCOPE = 'openid email profile offline_access';
12
16
  /** An access token this close to its end is renewed first. */
@@ -99,7 +103,7 @@ export class AivethClient {
99
103
  return this.discovery;
100
104
  }
101
105
  /** To Aiveth's sign-in page; back at `redirectUri`, `handleRedirect` finishes. */
102
- async signIn({ returnTo } = {}) {
106
+ async signIn({ returnTo, workspace } = {}) {
103
107
  const found = await this.discover();
104
108
  const verifier = random(48);
105
109
  const pending = { state: random(), nonce: random(), verifier, returnTo: returnTo ?? null };
@@ -111,6 +115,8 @@ export class AivethClient {
111
115
  });
112
116
  if (this.config.audience)
113
117
  query.set('resource', this.config.audience); // RFC 8707
118
+ if (workspace)
119
+ query.set('workspace', workspace); // signed in to that workspace (a member's only)
114
120
  this.navigate(`${found.authorization_endpoint}?${query}`);
115
121
  }
116
122
  /** On the return address: swap the code for tokens. Answers where the app wanted to go
@@ -171,6 +177,42 @@ export class AivethClient {
171
177
  this.changed();
172
178
  return this.tokens;
173
179
  }
180
+ /** The signed-in workspace from your own pages (Aiveth's My workspace API): set `audience` to
181
+ * `myWorkspaceAudience(issuer)` and sign in with `workspace`. Every member reads; only the
182
+ * workspace's admins change anything. */
183
+ myWorkspace = {
184
+ get: () => this.mine('GET', ''),
185
+ members: (offset = 0) => this.mine('GET', `/members?offset=${offset}`),
186
+ setRole: (userId, role) => this.mine('PATCH', `/members/${encodeURIComponent(userId)}`, { role }),
187
+ /** An admin removes someone; anyone may leave with their own id. */
188
+ remove: (userId) => this.mine('DELETE', `/members/${encodeURIComponent(userId)}`),
189
+ invitations: () => this.mine('GET', '/invitations'),
190
+ invite: (email, role) => this.mine('POST', '/invitations', role ? { email, role } : { email }),
191
+ withdraw: (invitationId) => this.mine('DELETE', `/invitations/${encodeURIComponent(invitationId)}`),
192
+ };
193
+ async mine(method, path, body) {
194
+ const token = await this.getAccessToken();
195
+ if (!token)
196
+ throw new SignInError('Sign in first.');
197
+ const res = await this.fetcher(`${this.issuer}/v1/my-workspace${path}`, {
198
+ method, headers: { Authorization: `Bearer ${token}`, ...(body === undefined ? {} : { 'Content-Type': 'application/json' }) },
199
+ ...(body === undefined ? {} : { body: JSON.stringify(body) }),
200
+ });
201
+ if (res.status === 204)
202
+ return undefined;
203
+ const found = (await res.json().catch(() => ({})));
204
+ if (!res.ok)
205
+ throw new RefusedError(String(res.status), typeof found.detail === 'string' ? found.detail : `Aiveth answered ${res.status}.`);
206
+ return found;
207
+ }
208
+ /** Switch the active workspace (id or slug): the new tokens name it, and `user` changes with
209
+ * them. The person must be a member. Needs the refresh token (`offline_access`). */
210
+ async switchWorkspace(workspace) {
211
+ const held = this.tokens?.refreshToken;
212
+ if (!held)
213
+ throw new SignInError('Switching a workspace needs offline_access.');
214
+ await this.exchange({ grant_type: 'refresh_token', refresh_token: held, workspace }, null);
215
+ }
174
216
  /** A live access token for calling your API — renewed with the refresh token when it is near
175
217
  * its end; null when signed out (or the refresh token has ended: sign in again). */
176
218
  async getAccessToken() {
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { AivethClient, RefusedError, SignInError, claimsOf, type Actor, type AivethConfig, type Returned, type User } from './client.js';
1
+ export { AivethClient, RefusedError, SignInError, claimsOf, myWorkspaceAudience, type MyWorkspace, type WorkspaceMember, type WorkspaceInvitationView, type Actor, type AivethConfig, type Returned, type User } from './client.js';
2
2
  export { AivethProvider, useAiveth, SignedIn, SignedOut, SignInButton, AivethButton, SignOutButton, UserButton, ActingBanner, type AivethProviderProps, type UseAiveth } from './react.js';
3
3
  export { BrandedButton, useBrandLook, type BrandLook, type BrandedButtonProps } from './brand.js';
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
- export { AivethClient, RefusedError, SignInError, claimsOf } from './client.js';
1
+ export { AivethClient, RefusedError, SignInError, claimsOf, myWorkspaceAudience } from './client.js';
2
2
  export { AivethProvider, useAiveth, SignedIn, SignedOut, SignInButton, AivethButton, SignOutButton, UserButton, ActingBanner } from './react.js';
3
3
  export { BrandedButton, useBrandLook } from './brand.js';
package/dist/react.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type CSSProperties, type ReactNode } from 'react';
2
- import { type Actor, type AivethConfig, type User } from './client.js';
2
+ import { AivethClient, type Actor, type AivethConfig, type User } from './client.js';
3
3
  export interface AivethProviderProps extends AivethConfig {
4
4
  children: ReactNode;
5
5
  /** After a return from Aiveth: where to go (your router's navigate), given `returnTo`. */
@@ -15,6 +15,7 @@ export interface UseAiveth {
15
15
  error: Error | null;
16
16
  signIn: (options?: {
17
17
  returnTo?: string;
18
+ workspace?: string;
18
19
  }) => Promise<void>;
19
20
  signOut: () => Promise<void>;
20
21
  /** For your API calls: `Authorization: Bearer …`. Null when signed out. */
@@ -27,6 +28,10 @@ export interface UseAiveth {
27
28
  hasFlag: (key: string) => boolean;
28
29
  /** Aiveth's page that ends acting as someone. */
29
30
  actingUrl: string;
31
+ /** Switch the active workspace (id or slug); `user` changes with the new tokens. */
32
+ switchWorkspace: (workspace: string) => Promise<void>;
33
+ /** The signed-in workspace from your pages (needs `audience={myWorkspaceAudience(issuer)}`). */
34
+ myWorkspace: AivethClient['myWorkspace'];
30
35
  }
31
36
  export declare function useAiveth(): UseAiveth;
32
37
  export declare function SignedIn({ children }: {
package/dist/react.js CHANGED
@@ -49,6 +49,8 @@ export function useAiveth() {
49
49
  getAccessToken: () => client.getAccessToken(), accountUrl: client.accountUrl(),
50
50
  actor: actorOf(user), actingUrl: client.actingUrl(),
51
51
  hasFlag: (key) => Array.isArray(user?.feature_flags) && user.feature_flags.includes(key),
52
+ switchWorkspace: (workspace) => client.switchWorkspace(workspace),
53
+ myWorkspace: client.myWorkspace,
52
54
  };
53
55
  }
54
56
  export function SignedIn({ children }) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiveth/react",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Aiveth by Gooclaim for React: sign in on Aiveth's page (PKCE), tokens in memory, ready-made pieces.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Gooclaim",