@bymax-one/nest-auth 1.4.22 → 1.5.1

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,3 +1,4 @@
1
+ import { WorkspaceAccessClient } from '@bymax-one/nest-auth/shared';
1
2
  import { NextRequest, NextResponse } from 'next/server';
2
3
 
3
4
  /**
@@ -71,6 +72,48 @@ interface RequestWithHeaders {
71
72
  */
72
73
  declare function isBackgroundRequest(request: RequestWithHeaders): boolean;
73
74
 
75
+ /**
76
+ * Server-side read of the caller's access to one workspace, for a Next.js layout or page.
77
+ *
78
+ * `createWorkspaceAccessReader` returns the function a `/w/[workspaceId]` layout awaits to decide
79
+ * between `notFound()`, a role gate and rendering. It asks the server (`GET workspaces/:id/access`)
80
+ * with the request's own cookies, because the proxy cannot decide membership: it sees only a decoded
81
+ * token, and a role decoded there is stale by construction. The answer is the server's, and the
82
+ * server decides again on every later request.
83
+ */
84
+
85
+ /** What {@link createWorkspaceAccessReader} needs to reach the API. */
86
+ interface WorkspaceAccessReaderConfig {
87
+ /** Absolute base URL of the NestJS API, as given to `createAuthProxy`. */
88
+ readonly apiBase: string;
89
+ /** The server's route prefix. Default `auth`. */
90
+ readonly routePrefix?: string;
91
+ /** Milliseconds before the request is abandoned. Default 5000. */
92
+ readonly timeoutMs?: number;
93
+ /** The `fetch` to use. Default the global one. */
94
+ readonly fetch?: typeof fetch;
95
+ }
96
+ /**
97
+ * Builds the reader a layout awaits, memoized per request.
98
+ *
99
+ * Create it once at module scope and import it where it is needed:
100
+ * `export const getWorkspaceAccess = createWorkspaceAccessReader({ apiBase })`. Calls with the same
101
+ * workspace id during one request share one upstream call, through React's `cache`.
102
+ *
103
+ * The reader answers `{ workspace, role }` when the caller is an active member, and `null` when the
104
+ * workspace is not usable for them: not a member or no such workspace (404), a role the route refuses
105
+ * (403), or a workspace pending deletion (409). A workspace id that is not a non-empty string, or is
106
+ * `.` or `..`, is `null` without a request: it came from a URL, and a URL must not be able to make a page fail. Any
107
+ * other failure (an expired token the proxy did not refresh, a rate limit, a server error, a body of
108
+ * the wrong shape) throws an `AuthClientError`, so Next's error boundary shows it rather than a
109
+ * `notFound()` that would be wrong.
110
+ *
111
+ * @param config - Where the API is.
112
+ * @returns The reader.
113
+ * @throws {Error} When `apiBase` is not an absolute http(s) URL.
114
+ */
115
+ declare function createWorkspaceAccessReader(config: WorkspaceAccessReaderConfig): (workspaceId: string) => Promise<WorkspaceAccessClient | null>;
116
+
74
117
  /**
75
118
  * Silent-refresh URL builder for the Next.js auth proxy.
76
119
  *
@@ -636,8 +679,13 @@ interface AuthProxyConfig {
636
679
  * role. MUST return a same-origin path starting with `/` (not
637
680
  * `//`). Call sites guard the return value against open-redirect
638
681
  * attempts.
682
+ *
683
+ * The second argument is `{ lastActiveWorkspaceId }` when {@link lastWorkspaceCookie} is
684
+ * configured and the cookie holds a UUID, and `{}` otherwise. It is a landing hint with no
685
+ * authority; build the path with `encodeURIComponent`, and let the layout's `getWorkspaceAccess`
686
+ * decide whether the caller may see it.
639
687
  */
640
- readonly getDefaultDashboard: (role: string) => string;
688
+ readonly getDefaultDashboard: (role: string, hint: DashboardHint) => string;
641
689
  /**
642
690
  * Absolute base URL of the upstream NestJS API (e.g.,
643
691
  * `https://api.example.com`). Used by the silent-refresh and
@@ -685,6 +733,14 @@ interface AuthProxyConfig {
685
733
  * before equality is tested.
686
734
  */
687
735
  readonly blockedUserStatuses: readonly string[];
736
+ /**
737
+ * Name of a cookie the HOST sets to the workspace the caller last used, for example after
738
+ * `listWorkspaces()`, as `HttpOnly; Secure; SameSite=Lax`. The library never writes it. When set,
739
+ * the proxy reads it for {@link getDefaultDashboard}'s second argument, but only a UUID is used;
740
+ * anything else is ignored. It must be a cookie-name token and differ from the three cookie names
741
+ * above.
742
+ */
743
+ readonly lastWorkspaceCookie?: string;
688
744
  }
689
745
  /**
690
746
  * Return shape of {@link createAuthProxy}.
@@ -732,6 +788,23 @@ interface ResolvedAuthProxyConfig extends AuthProxyConfig {
732
788
  */
733
789
  declare function createAuthProxy(config: AuthProxyConfig): AuthProxyInstance;
734
790
 
791
+ /**
792
+ * The landing hint the proxy hands to `getDefaultDashboard`: the workspace the signed-in caller used
793
+ * last, read from a cookie the host sets.
794
+ *
795
+ * It is a hint to choose a landing path and nothing more. The cookie is writable by the browser, so
796
+ * its value carries no authority: whatever path the host builds from it is still gated by the
797
+ * layout's `getWorkspaceAccess`, which answers the server's decision. What the value does have to
798
+ * be is safe to put in a path, so it is read only when it is a UUID, which is what the library mints
799
+ * for every workspace id; anything else is treated as absent and the default dashboard is used.
800
+ */
801
+
802
+ /** What the proxy knows about where to land the caller, beyond their role. */
803
+ interface DashboardHint {
804
+ /** The workspace the caller last used, when the host's cookie names one that is safe to use. */
805
+ readonly lastActiveWorkspaceId?: string;
806
+ }
807
+
735
808
  /**
736
809
  * `createSilentRefreshHandler` — factory for the GET
737
810
  * `/api/auth/silent-refresh` route handler.
@@ -1153,5 +1226,5 @@ declare function createLogoutHandler(config: LogoutHandlerConfig): LogoutHandler
1153
1226
  */
1154
1227
  declare const LOGOUT_ROUTE: "/api/auth/logout";
1155
1228
 
1156
- export { CLIENT_REFRESH_ROUTE, LOGOUT_ROUTE, SILENT_REFRESH_ROUTE, buildSilentRefreshUrl, createAuthProxy, createClientRefreshHandler, createLogoutHandler, createSilentRefreshHandler, decodeJwtToken, dedupeSetCookieHeaders, getSetCookieHeaders, getTenantId, getUserId, getUserRole, isBackgroundRequest, isTokenExpired, parseSetCookieHeader, resolveSafeDestination, verifyJwtToken };
1157
- export type { AuthProxyConfig, AuthProxyInstance, ClientRefreshHandler, ClientRefreshHandlerConfig, DecodedToken, HeadersLike, JwtHeader, LogoutHandler, LogoutHandlerConfig, LogoutHandlerRedirectConfig, LogoutHandlerStatusConfig, ParsedSetCookie, ProtectedRoutePattern, RequestWithHeaders, RequestWithUrl, ResolvedAuthProxyConfig, SilentRefreshHandler, SilentRefreshHandlerConfig };
1229
+ export { CLIENT_REFRESH_ROUTE, LOGOUT_ROUTE, SILENT_REFRESH_ROUTE, buildSilentRefreshUrl, createAuthProxy, createClientRefreshHandler, createLogoutHandler, createSilentRefreshHandler, createWorkspaceAccessReader, decodeJwtToken, dedupeSetCookieHeaders, getSetCookieHeaders, getTenantId, getUserId, getUserRole, isBackgroundRequest, isTokenExpired, parseSetCookieHeader, resolveSafeDestination, verifyJwtToken };
1230
+ export type { AuthProxyConfig, AuthProxyInstance, ClientRefreshHandler, ClientRefreshHandlerConfig, DashboardHint, DecodedToken, HeadersLike, JwtHeader, LogoutHandler, LogoutHandlerConfig, LogoutHandlerRedirectConfig, LogoutHandlerStatusConfig, ParsedSetCookie, ProtectedRoutePattern, RequestWithHeaders, RequestWithUrl, ResolvedAuthProxyConfig, SilentRefreshHandler, SilentRefreshHandlerConfig, WorkspaceAccessReaderConfig };
@@ -1,3 +1,4 @@
1
+ import { WorkspaceAccessClient } from '@bymax-one/nest-auth/shared';
1
2
  import { NextRequest, NextResponse } from 'next/server';
2
3
 
3
4
  /**
@@ -71,6 +72,48 @@ interface RequestWithHeaders {
71
72
  */
72
73
  declare function isBackgroundRequest(request: RequestWithHeaders): boolean;
73
74
 
75
+ /**
76
+ * Server-side read of the caller's access to one workspace, for a Next.js layout or page.
77
+ *
78
+ * `createWorkspaceAccessReader` returns the function a `/w/[workspaceId]` layout awaits to decide
79
+ * between `notFound()`, a role gate and rendering. It asks the server (`GET workspaces/:id/access`)
80
+ * with the request's own cookies, because the proxy cannot decide membership: it sees only a decoded
81
+ * token, and a role decoded there is stale by construction. The answer is the server's, and the
82
+ * server decides again on every later request.
83
+ */
84
+
85
+ /** What {@link createWorkspaceAccessReader} needs to reach the API. */
86
+ interface WorkspaceAccessReaderConfig {
87
+ /** Absolute base URL of the NestJS API, as given to `createAuthProxy`. */
88
+ readonly apiBase: string;
89
+ /** The server's route prefix. Default `auth`. */
90
+ readonly routePrefix?: string;
91
+ /** Milliseconds before the request is abandoned. Default 5000. */
92
+ readonly timeoutMs?: number;
93
+ /** The `fetch` to use. Default the global one. */
94
+ readonly fetch?: typeof fetch;
95
+ }
96
+ /**
97
+ * Builds the reader a layout awaits, memoized per request.
98
+ *
99
+ * Create it once at module scope and import it where it is needed:
100
+ * `export const getWorkspaceAccess = createWorkspaceAccessReader({ apiBase })`. Calls with the same
101
+ * workspace id during one request share one upstream call, through React's `cache`.
102
+ *
103
+ * The reader answers `{ workspace, role }` when the caller is an active member, and `null` when the
104
+ * workspace is not usable for them: not a member or no such workspace (404), a role the route refuses
105
+ * (403), or a workspace pending deletion (409). A workspace id that is not a non-empty string, or is
106
+ * `.` or `..`, is `null` without a request: it came from a URL, and a URL must not be able to make a page fail. Any
107
+ * other failure (an expired token the proxy did not refresh, a rate limit, a server error, a body of
108
+ * the wrong shape) throws an `AuthClientError`, so Next's error boundary shows it rather than a
109
+ * `notFound()` that would be wrong.
110
+ *
111
+ * @param config - Where the API is.
112
+ * @returns The reader.
113
+ * @throws {Error} When `apiBase` is not an absolute http(s) URL.
114
+ */
115
+ declare function createWorkspaceAccessReader(config: WorkspaceAccessReaderConfig): (workspaceId: string) => Promise<WorkspaceAccessClient | null>;
116
+
74
117
  /**
75
118
  * Silent-refresh URL builder for the Next.js auth proxy.
76
119
  *
@@ -636,8 +679,13 @@ interface AuthProxyConfig {
636
679
  * role. MUST return a same-origin path starting with `/` (not
637
680
  * `//`). Call sites guard the return value against open-redirect
638
681
  * attempts.
682
+ *
683
+ * The second argument is `{ lastActiveWorkspaceId }` when {@link lastWorkspaceCookie} is
684
+ * configured and the cookie holds a UUID, and `{}` otherwise. It is a landing hint with no
685
+ * authority; build the path with `encodeURIComponent`, and let the layout's `getWorkspaceAccess`
686
+ * decide whether the caller may see it.
639
687
  */
640
- readonly getDefaultDashboard: (role: string) => string;
688
+ readonly getDefaultDashboard: (role: string, hint: DashboardHint) => string;
641
689
  /**
642
690
  * Absolute base URL of the upstream NestJS API (e.g.,
643
691
  * `https://api.example.com`). Used by the silent-refresh and
@@ -685,6 +733,14 @@ interface AuthProxyConfig {
685
733
  * before equality is tested.
686
734
  */
687
735
  readonly blockedUserStatuses: readonly string[];
736
+ /**
737
+ * Name of a cookie the HOST sets to the workspace the caller last used, for example after
738
+ * `listWorkspaces()`, as `HttpOnly; Secure; SameSite=Lax`. The library never writes it. When set,
739
+ * the proxy reads it for {@link getDefaultDashboard}'s second argument, but only a UUID is used;
740
+ * anything else is ignored. It must be a cookie-name token and differ from the three cookie names
741
+ * above.
742
+ */
743
+ readonly lastWorkspaceCookie?: string;
688
744
  }
689
745
  /**
690
746
  * Return shape of {@link createAuthProxy}.
@@ -732,6 +788,23 @@ interface ResolvedAuthProxyConfig extends AuthProxyConfig {
732
788
  */
733
789
  declare function createAuthProxy(config: AuthProxyConfig): AuthProxyInstance;
734
790
 
791
+ /**
792
+ * The landing hint the proxy hands to `getDefaultDashboard`: the workspace the signed-in caller used
793
+ * last, read from a cookie the host sets.
794
+ *
795
+ * It is a hint to choose a landing path and nothing more. The cookie is writable by the browser, so
796
+ * its value carries no authority: whatever path the host builds from it is still gated by the
797
+ * layout's `getWorkspaceAccess`, which answers the server's decision. What the value does have to
798
+ * be is safe to put in a path, so it is read only when it is a UUID, which is what the library mints
799
+ * for every workspace id; anything else is treated as absent and the default dashboard is used.
800
+ */
801
+
802
+ /** What the proxy knows about where to land the caller, beyond their role. */
803
+ interface DashboardHint {
804
+ /** The workspace the caller last used, when the host's cookie names one that is safe to use. */
805
+ readonly lastActiveWorkspaceId?: string;
806
+ }
807
+
735
808
  /**
736
809
  * `createSilentRefreshHandler` — factory for the GET
737
810
  * `/api/auth/silent-refresh` route handler.
@@ -1153,5 +1226,5 @@ declare function createLogoutHandler(config: LogoutHandlerConfig): LogoutHandler
1153
1226
  */
1154
1227
  declare const LOGOUT_ROUTE: "/api/auth/logout";
1155
1228
 
1156
- export { CLIENT_REFRESH_ROUTE, LOGOUT_ROUTE, SILENT_REFRESH_ROUTE, buildSilentRefreshUrl, createAuthProxy, createClientRefreshHandler, createLogoutHandler, createSilentRefreshHandler, decodeJwtToken, dedupeSetCookieHeaders, getSetCookieHeaders, getTenantId, getUserId, getUserRole, isBackgroundRequest, isTokenExpired, parseSetCookieHeader, resolveSafeDestination, verifyJwtToken };
1157
- export type { AuthProxyConfig, AuthProxyInstance, ClientRefreshHandler, ClientRefreshHandlerConfig, DecodedToken, HeadersLike, JwtHeader, LogoutHandler, LogoutHandlerConfig, LogoutHandlerRedirectConfig, LogoutHandlerStatusConfig, ParsedSetCookie, ProtectedRoutePattern, RequestWithHeaders, RequestWithUrl, ResolvedAuthProxyConfig, SilentRefreshHandler, SilentRefreshHandlerConfig };
1229
+ export { CLIENT_REFRESH_ROUTE, LOGOUT_ROUTE, SILENT_REFRESH_ROUTE, buildSilentRefreshUrl, createAuthProxy, createClientRefreshHandler, createLogoutHandler, createSilentRefreshHandler, createWorkspaceAccessReader, decodeJwtToken, dedupeSetCookieHeaders, getSetCookieHeaders, getTenantId, getUserId, getUserRole, isBackgroundRequest, isTokenExpired, parseSetCookieHeader, resolveSafeDestination, verifyJwtToken };
1230
+ export type { AuthProxyConfig, AuthProxyInstance, ClientRefreshHandler, ClientRefreshHandlerConfig, DashboardHint, DecodedToken, HeadersLike, JwtHeader, LogoutHandler, LogoutHandlerConfig, LogoutHandlerRedirectConfig, LogoutHandlerStatusConfig, ParsedSetCookie, ProtectedRoutePattern, RequestWithHeaders, RequestWithUrl, ResolvedAuthProxyConfig, SilentRefreshHandler, SilentRefreshHandlerConfig, WorkspaceAccessReaderConfig };