@aiveth/nextjs 0.4.1 → 0.6.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.6.0 — 2026-11-26
4
+
5
+ - `/auth/login?workspace=…` signs the person in to a workspace; `switchWorkspace(ref)` (Server Actions, Route Handlers) switches it with the refresh token. `User` carries the workspace claims.
6
+
7
+ ## 0.5.0 — 2026-11-26
8
+
9
+ - `audience` / env `AIVETH_API_AUDIENCE`: your own API's address. `getAccessToken()` then gives a signed token for that API, which it checks with `verifyAccessToken` (`@aiveth/node`).
10
+
3
11
  ## 0.4.1 — 2026-11-25
4
12
 
5
13
  - `/auth/branding` is never cached (it was 5 minutes): a Save in Aiveth → Branding shows on
package/README.md CHANGED
@@ -16,6 +16,8 @@ AIVETH_CLIENT_ID=okc_...
16
16
  AIVETH_CLIENT_SECRET=oks_...
17
17
  AIVETH_COOKIE_SECRET=$(openssl rand -base64 32) # at least 32 characters
18
18
  AIVETH_APP_URL=https://your-app.com
19
+ # /auth/login?workspace=acme signs in to a workspace; switchWorkspace(ref) switches it
20
+ # AIVETH_API_AUDIENCE=https://api.your-app.com # optional: a signed token for your own API (Developers → Your APIs)
19
21
  ```
20
22
 
21
23
  3. `proxy.ts` (Next.js 16) — or `middleware.ts` (15):
package/dist/config.d.ts CHANGED
@@ -11,6 +11,10 @@ export interface AivethNextConfig {
11
11
  appUrl: string;
12
12
  /** Space-separated. `offline_access` keeps the person signed in past the access token. */
13
13
  scope: string;
14
+ /** Your own API's address, as registered under Developers → Your APIs (env
15
+ * `AIVETH_API_AUDIENCE`). Set it and `getAccessToken()` gives a signed token for that API, which
16
+ * it checks with `verifyAccessToken` (`@aiveth/node`) — no call to Aiveth. */
17
+ audience?: string;
14
18
  /** For tests, or a runtime without a global fetch. */
15
19
  fetch?: typeof fetch;
16
20
  }
package/dist/config.js CHANGED
@@ -49,6 +49,7 @@ function fromEnv() {
49
49
  cookieSecret: env.AIVETH_COOKIE_SECRET ?? '',
50
50
  appUrl: env.AIVETH_APP_URL ?? '',
51
51
  scope: env.AIVETH_SCOPES || DEFAULT_SCOPE,
52
+ audience: env.AIVETH_API_AUDIENCE || undefined,
52
53
  };
53
54
  }
54
55
  export function config() {
package/dist/handlers.js CHANGED
@@ -23,6 +23,7 @@ export async function login(req, c) {
23
23
  const to = await authorizeUrl(c, {
24
24
  state: tx.state, nonce: tx.nonce, challenge: await challengeOf(tx.verifier),
25
25
  loginHint: req.nextUrl.searchParams.get('login_hint') ?? undefined,
26
+ workspace: req.nextUrl.searchParams.get('workspace') ?? undefined,
26
27
  });
27
28
  return write(NextResponse.redirect(to), [txCookie(await seal(tx, c.cookieSecret, TX_SECONDS), secureFor(c))]);
28
29
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { aivethProxy, REFRESH_BEFORE_SECONDS, type AivethProxyOptions, type Protect } from './proxy.js';
2
- export { getAccessToken, getSession, getUser, requireUser } from './server.js';
2
+ export { getAccessToken, getSession, getUser, requireUser, switchWorkspace } from './server.js';
3
3
  export { configureAiveth, ROUTES, type AivethNextConfig } from './config.js';
4
4
  export { AivethAuthError } from './oidc.js';
5
5
  export type { Session, User } from './session.js';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  export { aivethProxy, REFRESH_BEFORE_SECONDS } from './proxy.js';
2
- export { getAccessToken, getSession, getUser, requireUser } from './server.js';
2
+ export { getAccessToken, getSession, getUser, requireUser, switchWorkspace } from './server.js';
3
3
  export { configureAiveth, ROUTES } from './config.js';
4
4
  export { AivethAuthError } from './oidc.js';
package/dist/oidc.d.ts CHANGED
@@ -28,12 +28,14 @@ export declare function authorizeUrl(c: AivethNextConfig, a: {
28
28
  nonce: string;
29
29
  challenge: string;
30
30
  loginHint?: string;
31
+ workspace?: string;
31
32
  }): Promise<string>;
32
33
  /** The ID token's claims: signed by Aiveth, for this app, live — and, on sign-in, this nonce. */
33
34
  export declare function verifyIdToken(c: AivethNextConfig, idToken: string, nonce?: string): Promise<User>;
34
35
  export declare function sessionFrom(t: TokenAnswer, user: User, idToken: string, createdAt?: number): Session;
35
36
  export declare function exchangeCode(c: AivethNextConfig, code: string, verifier: string, nonce: string): Promise<Session>;
36
- /** A new access token (and, as Aiveth turns them, a new refresh token). */
37
- export declare function refreshSession(c: AivethNextConfig, s: Session): Promise<Session>;
37
+ /** A new access token (and, as Aiveth turns them, a new refresh token). ``workspace``: switch the
38
+ * active workspace (id or slug) — the person must be a member. */
39
+ export declare function refreshSession(c: AivethNextConfig, s: Session, workspace?: string): Promise<Session>;
38
40
  export declare function logoutUrl(c: AivethNextConfig, idToken: string | undefined): Promise<string>;
39
41
  export {};
package/dist/oidc.js CHANGED
@@ -54,6 +54,10 @@ export async function authorizeUrl(c, a) {
54
54
  });
55
55
  if (a.loginHint)
56
56
  q.set('login_hint', a.loginHint);
57
+ if (a.workspace)
58
+ q.set('workspace', a.workspace); // signed in to that workspace (a member's only)
59
+ if (c.audience)
60
+ q.set('resource', c.audience); // RFC 8707: a signed access token for that API
57
61
  return `${d.authorization_endpoint}?${q}`;
58
62
  }
59
63
  async function token(c, form) {
@@ -112,11 +116,12 @@ export async function exchangeCode(c, code, verifier, nonce) {
112
116
  throw new AivethAuthError('Aiveth sent no ID token: the scope needs openid.');
113
117
  return sessionFrom(t, await verifyIdToken(c, t.id_token, nonce), t.id_token);
114
118
  }
115
- /** A new access token (and, as Aiveth turns them, a new refresh token). */
116
- export async function refreshSession(c, s) {
119
+ /** A new access token (and, as Aiveth turns them, a new refresh token). ``workspace``: switch the
120
+ * active workspace (id or slug) — the person must be a member. */
121
+ export async function refreshSession(c, s, workspace) {
117
122
  if (!s.refreshToken)
118
123
  throw new AivethAuthError('No refresh token: ask for offline_access.');
119
- const t = await token(c, { grant_type: 'refresh_token', refresh_token: s.refreshToken });
124
+ const t = await token(c, { grant_type: 'refresh_token', refresh_token: s.refreshToken, ...(workspace ? { workspace } : {}) });
120
125
  const idToken = t.id_token ?? s.idToken;
121
126
  const user = t.id_token ? await verifyIdToken(c, t.id_token) : s.user;
122
127
  return { ...sessionFrom(t, user, idToken, s.createdAt), refreshToken: t.refresh_token ?? s.refreshToken };
package/dist/server.d.ts CHANGED
@@ -7,3 +7,7 @@ export declare function requireUser(returnTo?: string): Promise<User>;
7
7
  * ends; the new session is written where Next lets a cookie be written (Route Handlers, Server
8
8
  * Actions) — in a Server Component it is used for this render, and the proxy writes it next time. */
9
9
  export declare function getAccessToken(): Promise<string | null>;
10
+ /** Switch the active workspace (id or slug) — in a Server Action or a Route Handler, where the
11
+ * session cookie can be written. The person must be a member; the new ID and access tokens name
12
+ * that workspace from now on (WorkOS's switch: a refresh with the workspace). Needs offline_access. */
13
+ export declare function switchWorkspace(workspace: string): Promise<Session>;
package/dist/server.js CHANGED
@@ -42,3 +42,18 @@ export async function getAccessToken() {
42
42
  }
43
43
  return session.accessToken;
44
44
  }
45
+ /** Switch the active workspace (id or slug) — in a Server Action or a Route Handler, where the
46
+ * session cookie can be written. The person must be a member; the new ID and access tokens name
47
+ * that workspace from now on (WorkOS's switch: a refresh with the workspace). Needs offline_access. */
48
+ export async function switchWorkspace(workspace) {
49
+ const c = config();
50
+ const jar = await cookies();
51
+ const session = await readSession(jar, c.cookieSecret);
52
+ if (!session)
53
+ throw new Error('Nobody is signed in.');
54
+ const next = await refreshSession(c, session, workspace);
55
+ for (const { name, value, options } of await sessionCookies(next, c.cookieSecret, secureFor(c), jar)) {
56
+ jar.set(name, value, options);
57
+ }
58
+ return next;
59
+ }
package/dist/session.d.ts CHANGED
@@ -10,6 +10,18 @@ export interface User {
10
10
  role?: string;
11
11
  permissions?: string[];
12
12
  feature_flags?: string[];
13
+ /** The active workspace (`/auth/login?workspace=…`, or `switchWorkspace`). */
14
+ workspace_id?: string;
15
+ workspace_slug?: string;
16
+ workspace_role?: string;
17
+ workspace_permissions?: string[];
18
+ /** With the `workspaces` scope: every workspace the person is in, for your switcher. */
19
+ workspaces?: {
20
+ id: string;
21
+ slug: string;
22
+ name: string;
23
+ role: string;
24
+ }[];
13
25
  [claim: string]: unknown;
14
26
  }
15
27
  export interface Session {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiveth/nextjs",
3
- "version": "0.4.1",
3
+ "version": "0.6.0",
4
4
  "description": "Aiveth by Gooclaim for Next.js: sign in on Aiveth's page, an encrypted session cookie, the user on the server and in the browser.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Gooclaim",