hazo_auth 5.3.0 → 6.0.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/README.md +110 -17
- package/SETUP_CHECKLIST.md +7 -7
- package/cli-src/cli/generate.ts +10 -1
- package/cli-src/lib/auth/auth_types.ts +21 -12
- package/cli-src/lib/auth/hazo_get_auth.server.ts +1 -1
- package/cli-src/lib/auth/hazo_get_tenant_auth.server.ts +25 -24
- package/cli-src/lib/auth/index.ts +3 -3
- package/cli-src/lib/auth/with_auth.server.ts +15 -15
- package/dist/cli/generate.d.ts.map +1 -1
- package/dist/cli/generate.js +10 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/lib/auth/auth_types.d.ts +13 -12
- package/dist/lib/auth/auth_types.d.ts.map +1 -1
- package/dist/lib/auth/auth_types.js +8 -0
- package/dist/lib/auth/hazo_get_auth.server.d.ts +6 -0
- package/dist/lib/auth/hazo_get_auth.server.d.ts.map +1 -1
- package/dist/lib/auth/hazo_get_auth.server.js +1 -1
- package/dist/lib/auth/hazo_get_tenant_auth.server.d.ts +8 -7
- package/dist/lib/auth/hazo_get_tenant_auth.server.d.ts.map +1 -1
- package/dist/lib/auth/hazo_get_tenant_auth.server.js +23 -22
- package/dist/lib/auth/index.d.ts +3 -3
- package/dist/lib/auth/index.d.ts.map +1 -1
- package/dist/lib/auth/index.js +1 -1
- package/dist/lib/auth/with_auth.server.d.ts +13 -13
- package/dist/lib/auth/with_auth.server.d.ts.map +1 -1
- package/dist/lib/auth/with_auth.server.js +2 -2
- package/dist/server_pages/forgot_password.d.ts +1 -1
- package/dist/server_pages/forgot_password.d.ts.map +1 -1
- package/dist/server_pages/forgot_password.js +2 -1
- package/dist/server_pages/login.d.ts +1 -1
- package/dist/server_pages/login.d.ts.map +1 -1
- package/dist/server_pages/login.js +2 -1
- package/dist/server_pages/my_settings.d.ts +1 -1
- package/dist/server_pages/my_settings.d.ts.map +1 -1
- package/dist/server_pages/my_settings.js +2 -1
- package/dist/server_pages/register.d.ts +1 -1
- package/dist/server_pages/register.d.ts.map +1 -1
- package/dist/server_pages/register.js +2 -1
- package/dist/server_pages/reset_password.d.ts +1 -1
- package/dist/server_pages/reset_password.d.ts.map +1 -1
- package/dist/server_pages/reset_password.js +2 -1
- package/dist/server_pages/verify_email.d.ts +1 -1
- package/dist/server_pages/verify_email.d.ts.map +1 -1
- package/dist/server_pages/verify_email.js +2 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -2,6 +2,97 @@
|
|
|
2
2
|
|
|
3
3
|
A reusable authentication UI component package powered by Next.js, TailwindCSS, and shadcn. It integrates `hazo_config` for configuration management and `hazo_connect` for data access, enabling future components to stay aligned with platform conventions.
|
|
4
4
|
|
|
5
|
+
### What's New in v6.0.0 🚨 BREAKING CHANGE
|
|
6
|
+
|
|
7
|
+
**`TenantAuthResult.organization` / `.organization_id` renamed to `.selected_scope` / `.selected_scope_id`.**
|
|
8
|
+
|
|
9
|
+
The multi-tenancy model has been scope-based for several releases — `auth.user_scopes`, `auth.scope_access_via`, `auth.scope_ok`, the `hazo_auth_scope_id` cookie, the `X-Hazo-Scope-Id` header. The only holdouts using the legacy "organization" name were two fields on `TenantAuthResult` that were always just *"the currently selected scope"* under the hood. This release eliminates the inconsistency.
|
|
10
|
+
|
|
11
|
+
> 📖 **Full migration guide:** [MIGRATION.md → v5.x → v6.0](./MIGRATION.md#v5x--v60-organization--selected_scope)
|
|
12
|
+
|
|
13
|
+
**No behavioral change.** `selected_scope_id` is derived from the same scope-selection cookie/header that `organization_id` was. Wire-format auth responses change field names, but the underlying scope lookup is identical.
|
|
14
|
+
|
|
15
|
+
**No deprecation shim.** A clean find-replace is the upgrade path. If you absolutely need a transitional period, pin to `hazo_auth@^5.3` until you can do the rename in one shot.
|
|
16
|
+
|
|
17
|
+
**What to replace (find ↔ replace, all in your app code):**
|
|
18
|
+
|
|
19
|
+
| Find | Replace with |
|
|
20
|
+
| ------------------------------------------------- | -------------------------------------------------- |
|
|
21
|
+
| `auth.organization` | `auth.selected_scope` |
|
|
22
|
+
| `auth.organization_id` | `auth.selected_scope_id` |
|
|
23
|
+
| `import type { TenantOrganization }` | `import type { SelectedScope }` |
|
|
24
|
+
| `: TenantOrganization` | `: SelectedScope` |
|
|
25
|
+
| `AuthenticatedTenantAuthWithOrg` | `AuthenticatedTenantAuthWithSelectedScope` |
|
|
26
|
+
|
|
27
|
+
**Before / after example:**
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
// BEFORE (v5.x)
|
|
31
|
+
import { hazo_get_tenant_auth } from "hazo_auth/server-lib";
|
|
32
|
+
import type { TenantOrganization } from "hazo_auth";
|
|
33
|
+
|
|
34
|
+
export async function GET(request: NextRequest) {
|
|
35
|
+
const auth = await hazo_get_tenant_auth(request);
|
|
36
|
+
if (!auth.authenticated || !auth.organization) {
|
|
37
|
+
return NextResponse.json({ error: "no tenant" }, { status: 403 });
|
|
38
|
+
}
|
|
39
|
+
const data = await getData(auth.organization_id); // or auth.organization.id
|
|
40
|
+
return NextResponse.json({ org: auth.organization, data });
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// AFTER (v6.0)
|
|
44
|
+
import { hazo_get_tenant_auth } from "hazo_auth/server-lib";
|
|
45
|
+
import type { SelectedScope } from "hazo_auth";
|
|
46
|
+
|
|
47
|
+
export async function GET(request: NextRequest) {
|
|
48
|
+
const auth = await hazo_get_tenant_auth(request);
|
|
49
|
+
if (!auth.authenticated || !auth.selected_scope) {
|
|
50
|
+
return NextResponse.json({ error: "no tenant scope" }, { status: 403 });
|
|
51
|
+
}
|
|
52
|
+
const data = await getData(auth.selected_scope_id); // or auth.selected_scope.id
|
|
53
|
+
return NextResponse.json({ selected_scope: auth.selected_scope, data });
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
**What did NOT change** (same names, same behavior, same wire format):
|
|
58
|
+
- `auth.user_scopes` — array of all scopes the user has access to
|
|
59
|
+
- `auth.scope_ok`, `auth.scope_access_via` — scope-access fields
|
|
60
|
+
- `hazo_auth_scope_id` cookie name and `X-Hazo-Scope-Id` request header
|
|
61
|
+
- All `Tenant*` type and class names: `TenantAuthResult`, `TenantAuthOptions`, `RequiredTenantAuthResult`, `TenantRequiredError`, `TenantAccessDeniedError`, `hazo_get_tenant_auth`, `require_tenant_auth`, `withAuth`'s `require_tenant` option
|
|
62
|
+
- The error response `{ code: "TENANT_REQUIRED" }` shape — only the human-readable `error` string was rephrased ("Organization context required" → "Tenant scope context required")
|
|
63
|
+
|
|
64
|
+
**One-liner upgrade for typical consumers:**
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
# Run from your app repo (NOT inside hazo_auth itself).
|
|
68
|
+
# Update the path glob to match your code layout.
|
|
69
|
+
git grep -l "organization\b\|TenantOrganization\|AuthenticatedTenantAuthWithOrg" -- 'src/**' 'app/**' 'lib/**' \
|
|
70
|
+
| xargs sed -i.bak '
|
|
71
|
+
s/auth\.organization_id\b/auth.selected_scope_id/g;
|
|
72
|
+
s/auth\.organization\b/auth.selected_scope/g;
|
|
73
|
+
s/\bTenantOrganization\b/SelectedScope/g;
|
|
74
|
+
s/\bAuthenticatedTenantAuthWithOrg\b/AuthenticatedTenantAuthWithSelectedScope/g;
|
|
75
|
+
'
|
|
76
|
+
# Then: review the diff carefully — sed is blunt. Remove .bak files when satisfied.
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
> ⚠️ The sed line is a starting point, not a blanket "trust me." It only catches `auth.organization*` and the type names. If you destructure (`const { organization } = auth`), aliased imports (`TenantOrganization as Org`), or reference `organization` for unrelated reasons (HTML autocomplete attribute, your own variables), the script either misses or wrongly rewrites them. **Always diff before committing.**
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
### What's New in v5.3.1 🔧
|
|
84
|
+
|
|
85
|
+
**`get_client_ip(request)` exported from `hazo_auth/server-lib`** — extracts the client IP from `x-forwarded-for` (first element), falling back to `x-real-ip`, then `"unknown"`. Previously private to `hazo_get_auth.server.ts`. Useful for consumers that need consistent IP extraction across handlers (e.g., `hazo_feedback` audit logging).
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import { get_client_ip } from "hazo_auth/server-lib";
|
|
89
|
+
|
|
90
|
+
export async function POST(request: NextRequest) {
|
|
91
|
+
const ip = get_client_ip(request);
|
|
92
|
+
// ...
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
5
96
|
### What's New in v5.1.39 🔧
|
|
6
97
|
|
|
7
98
|
**Postgres-Compatibility Schema Alignment** — fixes two long-standing drifts between the canonical SQLite schema and what the runtime actually writes. SQLite consumers see no behaviour change; Postgres + PostgREST consumers can now sign-up users without bespoke schema patches.
|
|
@@ -1631,7 +1722,7 @@ export async function proxy(request: NextRequest) {
|
|
|
1631
1722
|
|
|
1632
1723
|
#### `hazo_get_tenant_auth` (Recommended for Multi-Tenant Apps)
|
|
1633
1724
|
|
|
1634
|
-
**New:** Tenant-aware authentication function that extracts scope context from request headers or cookies and returns enriched result
|
|
1725
|
+
**New:** Tenant-aware authentication function that extracts scope context from request headers or cookies and returns enriched result including the currently selected scope.
|
|
1635
1726
|
|
|
1636
1727
|
**Location:** `src/lib/auth/hazo_get_tenant_auth.server.ts`
|
|
1637
1728
|
|
|
@@ -1665,8 +1756,9 @@ type TenantAuthResult =
|
|
|
1665
1756
|
permissions: string[];
|
|
1666
1757
|
permission_ok: boolean;
|
|
1667
1758
|
missing_permissions?: string[];
|
|
1668
|
-
|
|
1669
|
-
|
|
1759
|
+
selected_scope: SelectedScope | null; // Currently selected tenant/scope
|
|
1760
|
+
selected_scope_id: string | null; // Shorthand for selected_scope?.id
|
|
1761
|
+
user_scopes: ScopeDetails[]; // All user's scopes for switching
|
|
1670
1762
|
scope_ok: boolean;
|
|
1671
1763
|
}
|
|
1672
1764
|
| {
|
|
@@ -1674,12 +1766,13 @@ type TenantAuthResult =
|
|
|
1674
1766
|
user: null;
|
|
1675
1767
|
permissions: [];
|
|
1676
1768
|
permission_ok: false;
|
|
1677
|
-
|
|
1769
|
+
selected_scope: null;
|
|
1770
|
+
selected_scope_id: null;
|
|
1678
1771
|
user_scopes: [];
|
|
1679
1772
|
scope_ok: false;
|
|
1680
1773
|
};
|
|
1681
1774
|
|
|
1682
|
-
type
|
|
1775
|
+
type SelectedScope = {
|
|
1683
1776
|
id: string;
|
|
1684
1777
|
name: string;
|
|
1685
1778
|
slug: string | null; // URL-friendly identifier
|
|
@@ -1721,10 +1814,10 @@ export async function GET(request: NextRequest) {
|
|
|
1721
1814
|
return NextResponse.json({ error: "Authentication required" }, { status: 401 });
|
|
1722
1815
|
}
|
|
1723
1816
|
|
|
1724
|
-
if (!auth.
|
|
1817
|
+
if (!auth.selected_scope) {
|
|
1725
1818
|
return NextResponse.json(
|
|
1726
1819
|
{
|
|
1727
|
-
error: "No
|
|
1820
|
+
error: "No tenant scope context",
|
|
1728
1821
|
available_scopes: auth.user_scopes.map(s => ({ id: s.id, name: s.name }))
|
|
1729
1822
|
},
|
|
1730
1823
|
{ status: 403 }
|
|
@@ -1732,10 +1825,10 @@ export async function GET(request: NextRequest) {
|
|
|
1732
1825
|
}
|
|
1733
1826
|
|
|
1734
1827
|
// Access tenant-specific data
|
|
1735
|
-
const data = await getTenantData(auth.
|
|
1828
|
+
const data = await getTenantData(auth.selected_scope.id);
|
|
1736
1829
|
|
|
1737
1830
|
return NextResponse.json({
|
|
1738
|
-
|
|
1831
|
+
selected_scope: auth.selected_scope,
|
|
1739
1832
|
data,
|
|
1740
1833
|
// Include available scopes for UI scope switcher
|
|
1741
1834
|
available_scopes: auth.user_scopes,
|
|
@@ -1753,8 +1846,8 @@ export async function GET(request: NextRequest) {
|
|
|
1753
1846
|
required_permissions: ["view_reports"],
|
|
1754
1847
|
});
|
|
1755
1848
|
|
|
1756
|
-
// auth.
|
|
1757
|
-
const reports = await getReports(auth.
|
|
1849
|
+
// auth.selected_scope is guaranteed non-null here
|
|
1850
|
+
const reports = await getReports(auth.selected_scope.id);
|
|
1758
1851
|
return NextResponse.json({ reports });
|
|
1759
1852
|
} catch (error) {
|
|
1760
1853
|
if (error instanceof HazoAuthError) {
|
|
@@ -1804,16 +1897,16 @@ Helper function that wraps `hazo_get_tenant_auth` and throws typed errors for co
|
|
|
1804
1897
|
- `TenantRequiredError` (403) - No tenant context in request
|
|
1805
1898
|
- `TenantAccessDeniedError` (403) - User lacks access to requested tenant
|
|
1806
1899
|
|
|
1807
|
-
**Returns:** `RequiredTenantAuthResult` with guaranteed non-null `
|
|
1900
|
+
**Returns:** `RequiredTenantAuthResult` with guaranteed non-null `selected_scope`
|
|
1808
1901
|
|
|
1809
1902
|
**Example:**
|
|
1810
1903
|
```typescript
|
|
1811
1904
|
export async function GET(request: NextRequest) {
|
|
1812
1905
|
try {
|
|
1813
|
-
//
|
|
1814
|
-
const {
|
|
1906
|
+
// selected_scope is guaranteed to exist
|
|
1907
|
+
const { selected_scope, user, permissions } = await require_tenant_auth(request);
|
|
1815
1908
|
|
|
1816
|
-
const data = await getData(
|
|
1909
|
+
const data = await getData(selected_scope.id);
|
|
1817
1910
|
return NextResponse.json(data);
|
|
1818
1911
|
} catch (error) {
|
|
1819
1912
|
if (error instanceof HazoAuthError) {
|
|
@@ -1866,10 +1959,10 @@ export const DELETE = withAuth<{ id: string }>(
|
|
|
1866
1959
|
{ required_permissions: ["admin_system"] }
|
|
1867
1960
|
);
|
|
1868
1961
|
|
|
1869
|
-
// With tenant requirement (auth.
|
|
1962
|
+
// With tenant requirement (auth.selected_scope guaranteed non-null)
|
|
1870
1963
|
export const GET = withAuth<{ id: string }>(
|
|
1871
1964
|
async (request, auth, { id }) => {
|
|
1872
|
-
const data = await getData(auth.
|
|
1965
|
+
const data = await getData(auth.selected_scope.id, id);
|
|
1873
1966
|
return NextResponse.json(data);
|
|
1874
1967
|
},
|
|
1875
1968
|
{ require_tenant: true }
|
package/SETUP_CHECKLIST.md
CHANGED
|
@@ -1924,10 +1924,10 @@ import { hazo_get_tenant_auth } from "hazo_auth/server-lib";
|
|
|
1924
1924
|
export async function GET(request: NextRequest) {
|
|
1925
1925
|
const auth = await hazo_get_tenant_auth(request);
|
|
1926
1926
|
|
|
1927
|
-
if (auth.authenticated && auth.
|
|
1928
|
-
// auth.
|
|
1927
|
+
if (auth.authenticated && auth.selected_scope) {
|
|
1928
|
+
// auth.selected_scope contains the currently selected tenant/scope
|
|
1929
1929
|
// auth.user_scopes contains all scopes user can access (for UI switcher)
|
|
1930
|
-
const data = await getTenantData(auth.
|
|
1930
|
+
const data = await getTenantData(auth.selected_scope.id);
|
|
1931
1931
|
}
|
|
1932
1932
|
}
|
|
1933
1933
|
```
|
|
@@ -1939,8 +1939,8 @@ import { require_tenant_auth, HazoAuthError } from "hazo_auth/server-lib";
|
|
|
1939
1939
|
export async function GET(request: NextRequest) {
|
|
1940
1940
|
try {
|
|
1941
1941
|
const auth = await require_tenant_auth(request);
|
|
1942
|
-
// auth.
|
|
1943
|
-
return NextResponse.json(await getData(auth.
|
|
1942
|
+
// auth.selected_scope is guaranteed non-null
|
|
1943
|
+
return NextResponse.json(await getData(auth.selected_scope.id));
|
|
1944
1944
|
} catch (error) {
|
|
1945
1945
|
if (error instanceof HazoAuthError) {
|
|
1946
1946
|
return NextResponse.json(
|
|
@@ -1960,7 +1960,7 @@ import { withAuth } from "hazo_auth/server-lib";
|
|
|
1960
1960
|
// Auth + params + error handling all automatic
|
|
1961
1961
|
export const GET = withAuth<{ id: string }>(
|
|
1962
1962
|
async (request, auth, { id }) => {
|
|
1963
|
-
const data = await getData(auth.
|
|
1963
|
+
const data = await getData(auth.selected_scope.id, id);
|
|
1964
1964
|
return NextResponse.json(data);
|
|
1965
1965
|
},
|
|
1966
1966
|
{ require_tenant: true }
|
|
@@ -2009,7 +2009,7 @@ const auth = await hazo_get_tenant_auth(request);
|
|
|
2009
2009
|
|
|
2010
2010
|
// Return available scopes to frontend
|
|
2011
2011
|
return NextResponse.json({
|
|
2012
|
-
current_scope: auth.
|
|
2012
|
+
current_scope: auth.selected_scope,
|
|
2013
2013
|
available_scopes: auth.user_scopes.map(s => ({
|
|
2014
2014
|
id: s.id,
|
|
2015
2015
|
name: s.name,
|
package/cli-src/cli/generate.ts
CHANGED
|
@@ -127,11 +127,20 @@ ${exports}
|
|
|
127
127
|
}
|
|
128
128
|
|
|
129
129
|
function generate_page_content(page: PageDefinition): string {
|
|
130
|
+
// Next 16 requires page default exports to satisfy `PageProps` (i.e. accept
|
|
131
|
+
// `{ params?, searchParams? }` and nothing else). Re-exporting the named
|
|
132
|
+
// component directly fails that check because the component carries custom
|
|
133
|
+
// props (image_src, layout, …). The wrapper below is a parameter-less
|
|
134
|
+
// function that returns the component — Next sees a `() => JSX` signature
|
|
135
|
+
// which trivially satisfies PageProps. Direct JSX consumers still use the
|
|
136
|
+
// named export from `hazo_auth/pages` and get the full custom-prop API.
|
|
130
137
|
return `// Generated by hazo_auth - do not edit manually
|
|
131
138
|
// Page: /${page.path}
|
|
132
139
|
import { ${page.component_name} } from "hazo_auth/pages";
|
|
133
140
|
|
|
134
|
-
export default
|
|
141
|
+
export default function Page() {
|
|
142
|
+
return <${page.component_name} />;
|
|
143
|
+
}
|
|
135
144
|
`;
|
|
136
145
|
}
|
|
137
146
|
|
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
// file_description: Type definitions and error classes for hazo_get_auth utility
|
|
2
|
+
//
|
|
3
|
+
// Naming note (v6.0.0): the field previously called `organization` (and
|
|
4
|
+
// `organization_id`) on `TenantAuthResult` was renamed to `selected_scope`
|
|
5
|
+
// (and `selected_scope_id`), and the type `TenantOrganization` was renamed
|
|
6
|
+
// to `SelectedScope`. The multi-tenancy model is scopes throughout; the
|
|
7
|
+
// old name was a legacy synonym for "the currently selected scope" derived
|
|
8
|
+
// from the scope-selection cookie/header. No deprecation shim is provided.
|
|
9
|
+
//
|
|
2
10
|
// section: types
|
|
3
11
|
|
|
4
12
|
/**
|
|
@@ -123,10 +131,11 @@ export type ScopeDetails = {
|
|
|
123
131
|
};
|
|
124
132
|
|
|
125
133
|
/**
|
|
126
|
-
*
|
|
127
|
-
* Simplified view of scope
|
|
134
|
+
* Currently selected scope information returned in tenant auth results.
|
|
135
|
+
* Simplified view of the scope chosen via the scope-selection cookie or
|
|
136
|
+
* `X-Hazo-Scope-Id` header.
|
|
128
137
|
*/
|
|
129
|
-
export type
|
|
138
|
+
export type SelectedScope = {
|
|
130
139
|
id: string;
|
|
131
140
|
name: string;
|
|
132
141
|
slug: string | null;
|
|
@@ -167,9 +176,9 @@ export type TenantAuthResult =
|
|
|
167
176
|
permissions: string[];
|
|
168
177
|
permission_ok: boolean;
|
|
169
178
|
missing_permissions?: string[];
|
|
170
|
-
|
|
171
|
-
/** Shorthand for
|
|
172
|
-
|
|
179
|
+
selected_scope: SelectedScope | null;
|
|
180
|
+
/** Shorthand for selected_scope?.id - commonly used for DB query filters. */
|
|
181
|
+
selected_scope_id: string | null;
|
|
173
182
|
user_scopes: ScopeDetails[];
|
|
174
183
|
scope_ok?: boolean;
|
|
175
184
|
scope_access_via?: ScopeAccessInfo;
|
|
@@ -179,20 +188,20 @@ export type TenantAuthResult =
|
|
|
179
188
|
user: null;
|
|
180
189
|
permissions: [];
|
|
181
190
|
permission_ok: false;
|
|
182
|
-
|
|
183
|
-
/** Shorthand for
|
|
184
|
-
|
|
191
|
+
selected_scope: null;
|
|
192
|
+
/** Shorthand for selected_scope?.id - commonly used for DB query filters. */
|
|
193
|
+
selected_scope_id: null;
|
|
185
194
|
user_scopes: [];
|
|
186
195
|
scope_ok?: false;
|
|
187
196
|
};
|
|
188
197
|
|
|
189
198
|
/**
|
|
190
|
-
* Guaranteed authenticated result with non-null
|
|
191
|
-
* Returned by require_tenant_auth when validation passes
|
|
199
|
+
* Guaranteed authenticated result with non-null selected_scope.
|
|
200
|
+
* Returned by require_tenant_auth when validation passes.
|
|
192
201
|
*/
|
|
193
202
|
export type RequiredTenantAuthResult = TenantAuthResult & {
|
|
194
203
|
authenticated: true;
|
|
195
|
-
|
|
204
|
+
selected_scope: SelectedScope;
|
|
196
205
|
};
|
|
197
206
|
|
|
198
207
|
// section: tenant_error_classes
|
|
@@ -64,7 +64,7 @@ function parse_app_user_data(
|
|
|
64
64
|
* @param request - NextRequest object
|
|
65
65
|
* @returns IP address string
|
|
66
66
|
*/
|
|
67
|
-
function get_client_ip(request: NextRequest): string {
|
|
67
|
+
export function get_client_ip(request: NextRequest): string {
|
|
68
68
|
const forwarded = request.headers.get("x-forwarded-for");
|
|
69
69
|
if (forwarded) {
|
|
70
70
|
return forwarded.split(",")[0].trim();
|
|
@@ -14,7 +14,7 @@ import type {
|
|
|
14
14
|
TenantAuthOptions,
|
|
15
15
|
TenantAuthResult,
|
|
16
16
|
RequiredTenantAuthResult,
|
|
17
|
-
|
|
17
|
+
SelectedScope,
|
|
18
18
|
ScopeDetails,
|
|
19
19
|
} from "./auth_types";
|
|
20
20
|
import {
|
|
@@ -62,15 +62,15 @@ export function extract_scope_id_from_request(
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
|
-
* Builds
|
|
65
|
+
* Builds SelectedScope from scope details and access info.
|
|
66
66
|
* @param scope_details - Full scope details from cache
|
|
67
67
|
* @param is_super_admin - Whether user is accessing as super admin
|
|
68
|
-
* @returns
|
|
68
|
+
* @returns SelectedScope object
|
|
69
69
|
*/
|
|
70
|
-
function
|
|
70
|
+
function build_selected_scope(
|
|
71
71
|
scope_details: ScopeDetails,
|
|
72
72
|
is_super_admin: boolean,
|
|
73
|
-
):
|
|
73
|
+
): SelectedScope {
|
|
74
74
|
return {
|
|
75
75
|
id: scope_details.id,
|
|
76
76
|
name: scope_details.name,
|
|
@@ -96,20 +96,21 @@ function build_tenant_organization(
|
|
|
96
96
|
* Tenant-aware authentication function
|
|
97
97
|
*
|
|
98
98
|
* Extracts tenant/scope context from request headers or cookies,
|
|
99
|
-
* validates access, and returns enriched result
|
|
99
|
+
* validates access, and returns enriched result including the currently
|
|
100
|
+
* selected scope.
|
|
100
101
|
*
|
|
101
102
|
* Header priority: X-Hazo-Scope-Id > Cookie
|
|
102
103
|
*
|
|
103
104
|
* @param request - NextRequest object
|
|
104
105
|
* @param options - TenantAuthOptions for customization
|
|
105
|
-
* @returns TenantAuthResult with user, permissions,
|
|
106
|
+
* @returns TenantAuthResult with user, permissions, selected_scope, and user_scopes
|
|
106
107
|
*
|
|
107
108
|
* @example
|
|
108
109
|
* ```typescript
|
|
109
110
|
* const auth = await hazo_get_tenant_auth(request);
|
|
110
|
-
* if (auth.authenticated && auth.
|
|
111
|
+
* if (auth.authenticated && auth.selected_scope) {
|
|
111
112
|
* // Access tenant-specific data
|
|
112
|
-
* const data = await getData(auth.
|
|
113
|
+
* const data = await getData(auth.selected_scope.id);
|
|
113
114
|
* }
|
|
114
115
|
* ```
|
|
115
116
|
*/
|
|
@@ -136,8 +137,8 @@ export async function hazo_get_tenant_auth(
|
|
|
136
137
|
user: null,
|
|
137
138
|
permissions: [],
|
|
138
139
|
permission_ok: false,
|
|
139
|
-
|
|
140
|
-
|
|
140
|
+
selected_scope: null,
|
|
141
|
+
selected_scope_id: null,
|
|
141
142
|
user_scopes: [],
|
|
142
143
|
scope_ok: false,
|
|
143
144
|
};
|
|
@@ -155,8 +156,8 @@ export async function hazo_get_tenant_auth(
|
|
|
155
156
|
// User scopes from cache or empty array
|
|
156
157
|
const user_scopes: ScopeDetails[] = cached?.scopes || [];
|
|
157
158
|
|
|
158
|
-
// Build
|
|
159
|
-
let
|
|
159
|
+
// Build selected_scope info if scope access was successful
|
|
160
|
+
let selected_scope: SelectedScope | null = null;
|
|
160
161
|
|
|
161
162
|
if (scope_id && auth_result.scope_ok && auth_result.scope_access_via) {
|
|
162
163
|
// Find the scope in user's scopes that matches the access_via scope
|
|
@@ -165,7 +166,7 @@ export async function hazo_get_tenant_auth(
|
|
|
165
166
|
);
|
|
166
167
|
|
|
167
168
|
if (access_scope) {
|
|
168
|
-
|
|
169
|
+
selected_scope = build_selected_scope(
|
|
169
170
|
access_scope,
|
|
170
171
|
auth_result.scope_access_via.is_super_admin || false,
|
|
171
172
|
);
|
|
@@ -174,7 +175,7 @@ export async function hazo_get_tenant_auth(
|
|
|
174
175
|
const hazoConnect = get_hazo_connect_instance();
|
|
175
176
|
const scope_result = await get_scope_by_id(hazoConnect, scope_id);
|
|
176
177
|
if (scope_result.success && scope_result.scope) {
|
|
177
|
-
|
|
178
|
+
selected_scope = {
|
|
178
179
|
id: scope_result.scope.id,
|
|
179
180
|
name: scope_result.scope.name,
|
|
180
181
|
slug: null, // Could fetch from scope if slug column exists
|
|
@@ -200,8 +201,8 @@ export async function hazo_get_tenant_auth(
|
|
|
200
201
|
permissions: auth_result.permissions,
|
|
201
202
|
permission_ok: auth_result.permission_ok,
|
|
202
203
|
missing_permissions: auth_result.missing_permissions,
|
|
203
|
-
|
|
204
|
-
|
|
204
|
+
selected_scope,
|
|
205
|
+
selected_scope_id: selected_scope?.id || null,
|
|
205
206
|
user_scopes,
|
|
206
207
|
scope_ok: auth_result.scope_ok,
|
|
207
208
|
scope_access_via: auth_result.scope_access_via,
|
|
@@ -218,15 +219,15 @@ export async function hazo_get_tenant_auth(
|
|
|
218
219
|
*
|
|
219
220
|
* @param request - NextRequest object
|
|
220
221
|
* @param options - TenantAuthOptions for customization
|
|
221
|
-
* @returns RequiredTenantAuthResult with guaranteed non-null
|
|
222
|
+
* @returns RequiredTenantAuthResult with guaranteed non-null selected_scope
|
|
222
223
|
* @throws AuthenticationRequiredError, TenantRequiredError, TenantAccessDeniedError
|
|
223
224
|
*
|
|
224
225
|
* @example
|
|
225
226
|
* ```typescript
|
|
226
227
|
* try {
|
|
227
228
|
* const auth = await require_tenant_auth(request);
|
|
228
|
-
* // auth.
|
|
229
|
-
* const data = await getData(auth.
|
|
229
|
+
* // auth.selected_scope is guaranteed non-null here
|
|
230
|
+
* const data = await getData(auth.selected_scope.id);
|
|
230
231
|
* } catch (error) {
|
|
231
232
|
* if (error instanceof HazoAuthError) {
|
|
232
233
|
* return NextResponse.json(
|
|
@@ -257,14 +258,14 @@ export async function require_tenant_auth(
|
|
|
257
258
|
throw new TenantAccessDeniedError(scope_id, result.user_scopes);
|
|
258
259
|
}
|
|
259
260
|
|
|
260
|
-
// Check if
|
|
261
|
-
if (!result.
|
|
261
|
+
// Check if scope context is required but missing
|
|
262
|
+
if (!result.selected_scope) {
|
|
262
263
|
throw new TenantRequiredError(
|
|
263
|
-
"No
|
|
264
|
+
"No tenant scope context provided. Include X-Hazo-Scope-Id header or scope cookie.",
|
|
264
265
|
result.user_scopes,
|
|
265
266
|
);
|
|
266
267
|
}
|
|
267
268
|
|
|
268
|
-
// Type assertion: at this point we know
|
|
269
|
+
// Type assertion: at this point we know selected_scope is non-null
|
|
269
270
|
return result as RequiredTenantAuthResult;
|
|
270
271
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
export * from "./auth_types.js";
|
|
4
4
|
|
|
5
5
|
// section: server_exports
|
|
6
|
-
export { hazo_get_auth } from "./hazo_get_auth.server.js";
|
|
6
|
+
export { hazo_get_auth, get_client_ip } from "./hazo_get_auth.server.js";
|
|
7
7
|
export {
|
|
8
8
|
get_authenticated_user,
|
|
9
9
|
require_auth,
|
|
@@ -22,7 +22,7 @@ export {
|
|
|
22
22
|
} from "./hazo_get_tenant_auth.server.js";
|
|
23
23
|
export type {
|
|
24
24
|
ScopeDetails,
|
|
25
|
-
|
|
25
|
+
SelectedScope,
|
|
26
26
|
TenantAuthOptions,
|
|
27
27
|
TenantAuthResult,
|
|
28
28
|
RequiredTenantAuthResult,
|
|
@@ -50,7 +50,7 @@ export {
|
|
|
50
50
|
} from "./with_auth.server.js";
|
|
51
51
|
export type {
|
|
52
52
|
AuthenticatedTenantAuth,
|
|
53
|
-
|
|
53
|
+
AuthenticatedTenantAuthWithSelectedScope,
|
|
54
54
|
WithAuthOptions,
|
|
55
55
|
} from "./with_auth.server";
|
|
56
56
|
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
PermissionError,
|
|
11
11
|
type TenantAuthOptions,
|
|
12
12
|
type TenantAuthResult,
|
|
13
|
-
type
|
|
13
|
+
type SelectedScope,
|
|
14
14
|
type HazoAuthUser,
|
|
15
15
|
type ScopeDetails,
|
|
16
16
|
type ScopeAccessInfo,
|
|
@@ -27,19 +27,19 @@ export type AuthenticatedTenantAuth = {
|
|
|
27
27
|
permissions: string[];
|
|
28
28
|
permission_ok: boolean;
|
|
29
29
|
missing_permissions?: string[];
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
selected_scope: SelectedScope | null;
|
|
31
|
+
selected_scope_id: string | null;
|
|
32
32
|
user_scopes: ScopeDetails[];
|
|
33
33
|
scope_ok?: boolean;
|
|
34
34
|
scope_access_via?: ScopeAccessInfo;
|
|
35
35
|
};
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* Authenticated branch with guaranteed non-null
|
|
38
|
+
* Authenticated branch with guaranteed non-null selected_scope
|
|
39
39
|
*/
|
|
40
|
-
export type
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
export type AuthenticatedTenantAuthWithSelectedScope = AuthenticatedTenantAuth & {
|
|
41
|
+
selected_scope: SelectedScope;
|
|
42
|
+
selected_scope_id: string;
|
|
43
43
|
};
|
|
44
44
|
|
|
45
45
|
/**
|
|
@@ -48,8 +48,8 @@ export type AuthenticatedTenantAuthWithOrg = AuthenticatedTenantAuth & {
|
|
|
48
48
|
*/
|
|
49
49
|
export type WithAuthOptions = TenantAuthOptions & {
|
|
50
50
|
/**
|
|
51
|
-
* If true, requires
|
|
52
|
-
* Narrows auth type to
|
|
51
|
+
* If true, requires tenant/scope context (403 if missing)
|
|
52
|
+
* Narrows auth type to AuthenticatedTenantAuthWithSelectedScope
|
|
53
53
|
*/
|
|
54
54
|
require_tenant?: boolean;
|
|
55
55
|
};
|
|
@@ -75,7 +75,7 @@ type AuthenticatedHandler<TParams> = (
|
|
|
75
75
|
*/
|
|
76
76
|
type AuthenticatedTenantHandler<TParams> = (
|
|
77
77
|
request: NextRequest,
|
|
78
|
-
auth:
|
|
78
|
+
auth: AuthenticatedTenantAuthWithSelectedScope,
|
|
79
79
|
params: TParams,
|
|
80
80
|
) => Promise<NextResponse> | NextResponse;
|
|
81
81
|
|
|
@@ -138,7 +138,7 @@ async function resolve_params<TParams>(
|
|
|
138
138
|
*
|
|
139
139
|
* - Calls `hazo_get_tenant_auth` and returns 401 if not authenticated
|
|
140
140
|
* - Returns 403 if `required_permissions` are specified and not satisfied
|
|
141
|
-
* - Returns 403 if `require_tenant: true` and no
|
|
141
|
+
* - Returns 403 if `require_tenant: true` and no tenant/scope context
|
|
142
142
|
* - Resolves `await context.params` (Next.js 15 pattern)
|
|
143
143
|
* - Catches HazoAuthError, PermissionError, and unexpected errors
|
|
144
144
|
*
|
|
@@ -161,8 +161,8 @@ async function resolve_params<TParams>(
|
|
|
161
161
|
* // With tenant requirement
|
|
162
162
|
* export const GET = withAuth<{ id: string }>(
|
|
163
163
|
* async (request, auth, { id }) => {
|
|
164
|
-
* // auth.
|
|
165
|
-
* const data = await getData(auth.
|
|
164
|
+
* // auth.selected_scope is guaranteed non-null
|
|
165
|
+
* const data = await getData(auth.selected_scope.id, id);
|
|
166
166
|
* return NextResponse.json(data);
|
|
167
167
|
* },
|
|
168
168
|
* { require_tenant: true }
|
|
@@ -227,10 +227,10 @@ export function withAuth<TParams = Record<string, never>>(
|
|
|
227
227
|
}
|
|
228
228
|
|
|
229
229
|
// Check tenant requirement
|
|
230
|
-
if (options.require_tenant && !auth.
|
|
230
|
+
if (options.require_tenant && !auth.selected_scope) {
|
|
231
231
|
return NextResponse.json(
|
|
232
232
|
{
|
|
233
|
-
error: "
|
|
233
|
+
error: "Tenant scope context required",
|
|
234
234
|
code: "TENANT_REQUIRED",
|
|
235
235
|
},
|
|
236
236
|
{ status: 403 },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../../src/cli/generate.ts"],"names":[],"mappings":"AAqBA,MAAM,MAAM,eAAe,GAAG;IAC5B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,GAAG,CAAC,EAAE,OAAO,CAAC;CACf,CAAC;
|
|
1
|
+
{"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../../src/cli/generate.ts"],"names":[],"mappings":"AAqBA,MAAM,MAAM,eAAe,GAAG;IAC5B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,GAAG,CAAC,EAAE,OAAO,CAAC;CACf,CAAC;AA0MF,wBAAgB,eAAe,CAAC,OAAO,GAAE,eAAoB,GAAG,IAAI,CA8DnE"}
|
package/dist/cli/generate.js
CHANGED
|
@@ -93,11 +93,20 @@ ${exports}
|
|
|
93
93
|
`;
|
|
94
94
|
}
|
|
95
95
|
function generate_page_content(page) {
|
|
96
|
+
// Next 16 requires page default exports to satisfy `PageProps` (i.e. accept
|
|
97
|
+
// `{ params?, searchParams? }` and nothing else). Re-exporting the named
|
|
98
|
+
// component directly fails that check because the component carries custom
|
|
99
|
+
// props (image_src, layout, …). The wrapper below is a parameter-less
|
|
100
|
+
// function that returns the component — Next sees a `() => JSX` signature
|
|
101
|
+
// which trivially satisfies PageProps. Direct JSX consumers still use the
|
|
102
|
+
// named export from `hazo_auth/pages` and get the full custom-prop API.
|
|
96
103
|
return `// Generated by hazo_auth - do not edit manually
|
|
97
104
|
// Page: /${page.path}
|
|
98
105
|
import { ${page.component_name} } from "hazo_auth/pages";
|
|
99
106
|
|
|
100
|
-
export default
|
|
107
|
+
export default function Page() {
|
|
108
|
+
return <${page.component_name} />;
|
|
109
|
+
}
|
|
101
110
|
`;
|
|
102
111
|
}
|
|
103
112
|
// section: api_route_generation
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * from "./contexts/hazo_auth_provider.js";
|
|
2
2
|
export * from "./contexts/hazo_auth_config.js";
|
|
3
3
|
export * from "./components/index.js";
|
|
4
|
-
export type { HazoAuthUser, HazoAuthResult, HazoAuthError, HazoAuthOptions, ScopeDetails,
|
|
4
|
+
export type { HazoAuthUser, HazoAuthResult, HazoAuthError, HazoAuthOptions, ScopeDetails, SelectedScope, TenantAuthOptions, TenantAuthResult, RequiredTenantAuthResult, } from "./lib/auth/auth_types";
|
|
5
5
|
export { AuthenticationRequiredError, TenantRequiredError, TenantAccessDeniedError, } from "./lib/auth/auth_types.js";
|
|
6
6
|
export { cn, merge_class_names } from "./lib/utils.js";
|
|
7
7
|
export { HAZO_AUTH_PERMISSIONS, ALL_ADMIN_PERMISSIONS } from "./lib/constants.js";
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAG5C,cAAc,oBAAoB,CAAC;AAGnC,YAAY,EACV,YAAY,EACZ,cAAc,EACd,aAAa,EACb,eAAe,EACf,YAAY,EACZ,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,cAAc,+BAA+B,CAAC;AAC9C,cAAc,6BAA6B,CAAC;AAG5C,cAAc,oBAAoB,CAAC;AAGnC,YAAY,EACV,YAAY,EACZ,cAAc,EACd,aAAa,EACb,eAAe,EACf,YAAY,EACZ,aAAa,EACb,iBAAiB,EACjB,gBAAgB,EAChB,wBAAwB,GACzB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,2BAA2B,EAC3B,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EAAE,EAAE,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAGpD,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC"}
|