@bymax-one/nest-auth 1.5.0 → 1.5.2
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 +123 -1
- package/README.md +49 -7
- package/dist/client/index.cjs +9 -9
- package/dist/client/index.mjs +10 -10
- package/dist/react/index.cjs +18 -8
- package/dist/react/index.mjs +18 -8
- package/dist/server/index.cjs +225 -27
- package/dist/server/index.d.cts +127 -12
- package/dist/server/index.d.ts +127 -12
- package/dist/server/index.mjs +225 -28
- package/dist/shared/index.cjs +1 -0
- package/dist/shared/index.d.cts +55 -1
- package/dist/shared/index.d.ts +55 -1
- package/dist/shared/index.mjs +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -18,6 +18,126 @@ what moves, and that note is the compatibility contract until strict SemVer begi
|
|
|
18
18
|
|
|
19
19
|
## [Unreleased]
|
|
20
20
|
|
|
21
|
+
## [1.5.2] - 2026-11-12
|
|
22
|
+
|
|
23
|
+
A patch release for workspaces: the bundled email provider can deliver a workspace invitation, and a
|
|
24
|
+
registration mode that could never create an account is refused at startup.
|
|
25
|
+
|
|
26
|
+
### Apply to a derived backend
|
|
27
|
+
|
|
28
|
+
- **`WorkspaceInviteData` gains a required field, `kind: 'member' | 'bootstrap'`.** The service sets it
|
|
29
|
+
from the stored invitation: `'member'` joins a workspace that exists, `'bootstrap'` is a platform
|
|
30
|
+
invitation to set up and own one that is created when the invitee accepts (its `workspaceName` names a
|
|
31
|
+
workspace that does not exist yet, and its `inviterName` is a display name the platform chose, which may
|
|
32
|
+
be empty). An implementer of `sendWorkspaceInvitation` that reads the object is unaffected; a test double
|
|
33
|
+
or a caller that builds one must add the field.
|
|
34
|
+
- **`registration: 'invitation-only'` is refused at startup unless an invitation controller is mounted.**
|
|
35
|
+
The workspace layer being on was taken for a way in, but with no invitation controller mounted no account
|
|
36
|
+
could ever be created, and OAuth acceptance rides on the workspace invitation controller, so it is no way
|
|
37
|
+
in on its own. The mode now needs `controllers.workspaceInvitations` with `workspaces.enabled`, or
|
|
38
|
+
`controllers.invitations` with `invitations.enabled`, and the error names what each path lacks. A host
|
|
39
|
+
that runs `invitation-only` with its own invitation flow and none of these controllers must mount one.
|
|
40
|
+
|
|
41
|
+
### Fixed
|
|
42
|
+
|
|
43
|
+
- **The bundled `DefaultAuthEmailProvider` implements `sendWorkspaceInvitation`.** The module refuses to
|
|
44
|
+
boot with `controllers.workspaceInvitations` on unless the provider implements it, and the default
|
|
45
|
+
provider did not, so a host that took the default could not enable workspace invitations at all. It
|
|
46
|
+
renders the invitation like the tenant one: the inviter, the workspace, a token and its expiry,
|
|
47
|
+
with sign-in or sign-up wording, the text the sender chose escaped, and the same credential flag and
|
|
48
|
+
delivery-error policy as every other message; a bootstrap invitation is worded as an invitation to set up
|
|
49
|
+
the workspace as its owner. Like the tenant invitation, it renders a **token**, not a link, because the
|
|
50
|
+
provider has no base URL: override `messages.workspaceInvitation` to send a link. A test now derives, from
|
|
51
|
+
the source, the methods a feature refuses to boot without and the optional methods of `IEmailProvider`,
|
|
52
|
+
and checks the default provider implements each.
|
|
53
|
+
|
|
54
|
+
### Maintenance
|
|
55
|
+
|
|
56
|
+
Repository tooling; nothing changes for a host. The weekly cold mutation job now fails when a unit test did not run in the mutation run
|
|
57
|
+
(`inventory: enforce`), and the exempt list gains the one spec that executes no source and was missing,
|
|
58
|
+
`src/server/conformance-well-formed.spec.ts`. The sharded run of the 1.5.1 release commit finished Stryker
|
|
59
|
+
on all four shards with no surviving and no uncovered mutant in any shard's own report, over a plan that
|
|
60
|
+
puts each of the 272 files in exactly one shard. Its aggregate job stopped on a test id that carries an
|
|
61
|
+
unformatted `it.each` placeholder, a defect of the shared workflow that was reported there, and an
|
|
62
|
+
inventory of its four reports named the missing exempt entry.
|
|
63
|
+
|
|
64
|
+
## [1.5.1] - 2026-11-11
|
|
65
|
+
|
|
66
|
+
A patch release for workspaces: it adds the way to remove an account's memberships before the host deletes
|
|
67
|
+
the account, fixes a wrong-account list in a tab that hears a broadcast, makes the shared contract valid for
|
|
68
|
+
a strict parser, and measures the frontend bundle. **One required port method is added: read the first
|
|
69
|
+
note below.**
|
|
70
|
+
|
|
71
|
+
### Apply to a derived backend
|
|
72
|
+
|
|
73
|
+
- **`IMembershipRepository` gains a REQUIRED method, `removeAllForUser`: every implementer must add
|
|
74
|
+
it.** It removes every membership of an account, in every membership status (active or suspended)
|
|
75
|
+
and in every workspace status, in one transaction, persisting one `member.removed` audit row per
|
|
76
|
+
removal, and answers the removed `{ workspaceId, role }` list. In the same transaction it applies the
|
|
77
|
+
last-owner rule: when the account is the only active owner of a workspace that is not
|
|
78
|
+
`pending_deletion` or `deleted`, it removes nothing, writes no audit row and answers `{ result:
|
|
79
|
+
'last_owner', workspaceIds }`. It is required, not optional, because `listForUser` lists only the
|
|
80
|
+
active memberships of active workspaces, so a fallback built on it would silently leave the suspended
|
|
81
|
+
memberships and the memberships in other workspaces behind. A host that does not enable workspaces
|
|
82
|
+
implements nothing.
|
|
83
|
+
- **Call `MembershipService.removeAllForUser(tenantId, userId, actorUserId, context)` before you
|
|
84
|
+
delete or anonymise a user.** The library never deletes a user, and until now the membership rows of
|
|
85
|
+
a deleted user stayed behind: they still showed in `listForWorkspace`, a deleted owner still counted,
|
|
86
|
+
and a workspace whose only owner was deleted was never seen as orphaned. The call refuses with `409 auth.last_owner` and
|
|
87
|
+
`details.workspaces` (`{ id, name, status }`, at most 100; `{ id }` alone for one the repository found
|
|
88
|
+
during the write) while the account is the only owner of an active or suspended workspace, so you can
|
|
89
|
+
transfer ownership or schedule the workspace's deletion first. A workspace pending deletion does not block, because no one can finalize
|
|
90
|
+
it before its grace period ends: the owner's membership there is removed, the workspace can no longer
|
|
91
|
+
be restored, and your finalize job deletes it when the grace period ends. If a cache bump fails after
|
|
92
|
+
the commit it throws `MembershipBumpFailedError` with `removed` and `failed`; repeating the call
|
|
93
|
+
removes nothing, so drop the access of each `failed` entry with
|
|
94
|
+
`WorkspaceAccessService.invalidateMembership`.
|
|
95
|
+
- **The shared wire contract is at version 4, and its bytes changed.** The new section
|
|
96
|
+
`workspaceAccountRemoval` holds the rules and the blocking vectors, and the `workspaceScopedName`
|
|
97
|
+
refusal vector for an unpaired surrogate now carries `unpairedSurrogateCodeUnits: [55296]` (one more
|
|
98
|
+
final part, built from those UTF-16 code units) instead of a lone surrogate escape, which a parser
|
|
99
|
+
that builds well-formed strings, `serde_json` among them, rejects, so the sibling library lost the
|
|
100
|
+
whole file. Every string in `conformance/` is now held to well-formed Unicode by a test. The
|
|
101
|
+
contract's sha256 is `1e4c38e1741c21e639e4099cd33ece9b04518cf69699fb39fd9d5b9d37352edd`; a copy kept
|
|
102
|
+
by another library is refreshed from `conformance/wire-contract.json`, and a library that reads the
|
|
103
|
+
vector builds the lone surrogate itself.
|
|
104
|
+
- **`AUTH_WORKSPACE_ROUTES` is exported from `./shared`,** as the README and the specification already
|
|
105
|
+
said it was; before, it was reachable only as `AUTH_ROUTES.workspaces`.
|
|
106
|
+
- **A frontend that uses no workspace ships less.** `createAuthClient` imports its own route groups
|
|
107
|
+
instead of the aggregate `AUTH_ROUTES`, which kept every route family, the 16 workspace routes among
|
|
108
|
+
them, in the bundle. Measured with the same minified, tree-shaken minimal import: `createAuthClient`
|
|
109
|
+
is **3.12 KiB** brotli, against 3.45 KiB in 1.5.0 and **3.57 KiB in 1.4.22**. The 1.5.0 note that
|
|
110
|
+
said "from 3.82 to 3.45" was wrong about the starting point: 1.4.22 measures 3.57, so the 1.4.22 to
|
|
111
|
+
1.5.0 change is 3.57 to 3.45, and `AuthProvider` plus `useSession` 1.26 to 1.13 KiB stands.
|
|
112
|
+
|
|
113
|
+
### Added
|
|
114
|
+
|
|
115
|
+
- **`MembershipService.removeAllForUser` and `MembershipBumpFailedError`,** described above, with the
|
|
116
|
+
port method `IMembershipRepository.removeAllForUser` and its types (`RemoveAllMembershipsParams`,
|
|
117
|
+
`RemoveAllMembershipsResult`, `RemovedMembership`, `AccountRemovalAudit`).
|
|
118
|
+
- **A "Deleting an account" section in the README** and §22.3a in the technical specification.
|
|
119
|
+
|
|
120
|
+
### Fixed
|
|
121
|
+
|
|
122
|
+
- **A tab whose session still named the previous account no longer shows the new account's workspaces
|
|
123
|
+
after a broadcast.** After another tab changed the signed-in account, the tab reloaded the list with
|
|
124
|
+
the browser's cookie, which already belonged to the new account, and showed it under the old
|
|
125
|
+
identity until the session revalidated. A broadcast now revalidates the session first and reloads the
|
|
126
|
+
list only when the account is unchanged; when it changed, the account change loads the new account's
|
|
127
|
+
list. A signed-out tab still makes no request. A revalidation that fails with an error other than an
|
|
128
|
+
expired session ends the tab's session view, as every revalidation of the auth provider already does.
|
|
129
|
+
- **The shared contract can be read by a strict parser:** see the contract note above.
|
|
130
|
+
- **The README states where a workspace invitation token travels:** in a POST body on the API routes;
|
|
131
|
+
the one use of a query string is the OAuth sign-in start, `GET /oauth/:provider?workspaceInvitation=`,
|
|
132
|
+
which stores only the token's sha256 and answers with `Referrer-Policy: no-referrer`.
|
|
133
|
+
|
|
134
|
+
### Maintenance
|
|
135
|
+
|
|
136
|
+
Repository tooling; nothing changes for a host. The weekly cold mutation job runs as four shards of two
|
|
137
|
+
workers each on the shared sharded workflow, after a run at four workers lost its runner twice; the
|
|
138
|
+
size budgets of the two auth surfaces are tightened to the measured size plus about 5% (client 3.3 KiB,
|
|
139
|
+
react 1.2 KiB) and the react workspace surface and entry are 2.5 and 3.6 KiB.
|
|
140
|
+
|
|
21
141
|
## [1.5.0] - 2026-11-11
|
|
22
142
|
|
|
23
143
|
**Workspaces.** One identity can now hold a role in each of many workspaces inside a tenant,
|
|
@@ -4831,7 +4951,9 @@ ever installable.
|
|
|
4831
4951
|
- Phase 4 password-reset tests cover: both `token` and `otp` flows, mutual exclusivity validation, `verifiedToken` exchange, resend cooldown, anti-enumeration (no error on unknown email), and session invalidation on reset
|
|
4832
4952
|
- Phase 5 tests cover: platform login with MFA path and brute-force lockout, `JwtPlatformGuard` cross-context rejection, `PlatformRolesGuard` hierarchy enforcement, OAuth CSRF state lifecycle, `onOAuthLogin` hook resolution strategies, and invitation role-authorization + acceptance single-use enforcement
|
|
4833
4953
|
|
|
4834
|
-
[Unreleased]: https://github.com/bymaxone/nest-auth/compare/v1.5.
|
|
4954
|
+
[Unreleased]: https://github.com/bymaxone/nest-auth/compare/v1.5.2...HEAD
|
|
4955
|
+
[1.5.2]: https://github.com/bymaxone/nest-auth/compare/v1.5.1...v1.5.2
|
|
4956
|
+
[1.5.1]: https://github.com/bymaxone/nest-auth/compare/v1.5.0...v1.5.1
|
|
4835
4957
|
[1.5.0]: https://github.com/bymaxone/nest-auth/compare/v1.4.22...v1.5.0
|
|
4836
4958
|
[1.4.22]: https://github.com/bymaxone/nest-auth/compare/v1.4.21...v1.4.22
|
|
4837
4959
|
[1.4.21]: https://github.com/bymaxone/nest-auth/compare/v1.4.20...v1.4.21
|
package/README.md
CHANGED
|
@@ -371,6 +371,17 @@ Email delivery is fully delegated to the consumer — the library never imports
|
|
|
371
371
|
> If you use the bundled `DefaultAuthEmailProvider` with `onDeliveryError: 'rethrow'`, the error it
|
|
372
372
|
> re-throws is the channel's original and is still yours to contain the same way.
|
|
373
373
|
|
|
374
|
+
> [!NOTE]
|
|
375
|
+
> The bundled `DefaultAuthEmailProvider` implements every method of `IEmailProvider`, the optional
|
|
376
|
+
> ones included (`sendWorkspaceInvitation` among them), so a host that takes it can enable every
|
|
377
|
+
> controller. It renders a **token**, not a link: the invitation messages say "use this token to
|
|
378
|
+
> accept: ..." and it has no base URL to build one from. To send a link, override the message,
|
|
379
|
+
> `new DefaultAuthEmailProvider(sink, { messages: { invitation, workspaceInvitation } })`, with a
|
|
380
|
+
> renderer that returns your own `html`; the escaping of caller-supplied text, the credential flag
|
|
381
|
+
> and the delivery-error policy still apply to it. A workspace invitation says whether it is for a
|
|
382
|
+
> workspace that exists (`kind: 'member'`) or a platform invitation to set one up and own it
|
|
383
|
+
> (`kind: 'bootstrap'`).
|
|
384
|
+
|
|
374
385
|
```typescript
|
|
375
386
|
// email.provider.ts
|
|
376
387
|
import { Injectable } from '@nestjs/common'
|
|
@@ -1477,12 +1488,13 @@ BymaxAuthModule.registerAsync({
|
|
|
1477
1488
|
|
|
1478
1489
|
`controllers.workspaces` mounts the routes and needs `workspaces.enabled` and both repositories.
|
|
1479
1490
|
`controllers.workspaceInvitations` also needs an email provider that implements
|
|
1480
|
-
`sendWorkspaceInvitation
|
|
1491
|
+
`sendWorkspaceInvitation`, which the bundled `DefaultAuthEmailProvider` does. The module refuses to
|
|
1492
|
+
boot when a piece is missing. With
|
|
1481
1493
|
`workspaces.enabled: false`, the default, nothing in this section exists in your application.
|
|
1482
1494
|
|
|
1483
1495
|
You implement two ports: `IWorkspaceRepository` (`createWithOwner`, `findById`, `rename`,
|
|
1484
1496
|
`updateStatus`) and `IMembershipRepository` (`find`, `listForUser`, `listForWorkspace`, `create`,
|
|
1485
|
-
`changeRole`, `remove`, `transferOwnership`, `countOwned`, `listSoleOwned`). Every call carries the
|
|
1497
|
+
`changeRole`, `remove`, `removeAllForUser`, `transferOwnership`, `countOwned`, `listSoleOwned`). Every call carries the
|
|
1486
1498
|
tenant and the workspace. The writes that must not race (a role change, a removal, an ownership
|
|
1487
1499
|
transfer) are guarded writes the repository answers atomically, so two requests cannot both remove
|
|
1488
1500
|
the last owner.
|
|
@@ -1505,8 +1517,9 @@ the last owner.
|
|
|
1505
1517
|
|
|
1506
1518
|
`registration: 'invitation-only'` closes sign-up except through an invitation for that exact address
|
|
1507
1519
|
(password registration and the OAuth `create` action answer `403 auth.registration_closed`; the
|
|
1508
|
-
invitation accept routes stay open; existing accounts keep signing in). Startup refuses the mode
|
|
1509
|
-
|
|
1520
|
+
invitation accept routes stay open; existing accounts keep signing in). Startup refuses the mode unless an
|
|
1521
|
+
invitation controller is mounted (`controllers.workspaceInvitations` with `workspaces.enabled`, or
|
|
1522
|
+
`controllers.invitations` with `invitations.enabled`), and names what is missing.
|
|
1510
1523
|
|
|
1511
1524
|
### Protect a route
|
|
1512
1525
|
|
|
@@ -1565,9 +1578,11 @@ header.
|
|
|
1565
1578
|
With `controllers.workspaceInvitations: true`: `POST` and `GET workspaces/:workspaceId/invitations`
|
|
1566
1579
|
(invite, list pending), `POST .../invitations/resend` and `.../invitations/revoke`, and the
|
|
1567
1580
|
token-based `POST workspaces/invitations/preview` (public), `.../accept` (the signed-in account the
|
|
1568
|
-
invitation was addressed to) and `.../accept-signup` (public, creates the account).
|
|
1569
|
-
in a POST body, never in a query string
|
|
1570
|
-
|
|
1581
|
+
invitation was addressed to) and `.../accept-signup` (public, creates the account). On these routes
|
|
1582
|
+
the token travels in a POST body, never in a query string. The one exception is the OAuth sign-in
|
|
1583
|
+
start, `GET /oauth/:provider?workspaceInvitation=<token>`: it stores only the token's sha256 in the
|
|
1584
|
+
state record and answers with `Referrer-Policy: no-referrer`. On your accept page, put the token in
|
|
1585
|
+
the URL fragment and set `Referrer-Policy: no-referrer` there.
|
|
1571
1586
|
|
|
1572
1587
|
### Hooks and events
|
|
1573
1588
|
|
|
@@ -1578,6 +1593,33 @@ context)` receives, after the change has committed, `workspace.created`, `.renam
|
|
|
1578
1593
|
`workspace.switched`; and `access.denied` (with the `surface`, the public `code` and a host-only
|
|
1579
1594
|
`reason`). It is best-effort and informational: the repository's persisted audit row is the record.
|
|
1580
1595
|
|
|
1596
|
+
### Deleting an account
|
|
1597
|
+
|
|
1598
|
+
The library never deletes a user: `IUserRepository` has no delete. A host that deletes or anonymises
|
|
1599
|
+
a user row must remove that user's memberships first, or the rows stay behind, still count as
|
|
1600
|
+
members of their workspaces, and a deleted owner keeps a workspace from ever being seen as
|
|
1601
|
+
orphaned. Call `MembershipService.removeAllForUser(tenantId, userId, actorUserId, context)` before
|
|
1602
|
+
the delete. `actorUserId` is the account itself, an administrator, or `null` for a job.
|
|
1603
|
+
|
|
1604
|
+
It removes every membership of the account, in every membership status and every workspace, with one
|
|
1605
|
+
`member.removed` event per removal and the cached access of each dropped, so access ends at the next
|
|
1606
|
+
request. Only the repository can find them all (`listForUser` lists active memberships of active
|
|
1607
|
+
workspaces only), so it is one call on the membership port, `removeAllForUser`, which you implement
|
|
1608
|
+
in one transaction together with the audit rows.
|
|
1609
|
+
|
|
1610
|
+
- **It refuses while the account is the only owner of an active or suspended workspace:** `409
|
|
1611
|
+
auth.last_owner` with `details.workspaces` (`{ id, name, status }`, at most 100; `{ id }` alone for one the repository found during the write), and nothing is
|
|
1612
|
+
removed. Transfer ownership or schedule the workspace's deletion first.
|
|
1613
|
+
- **A workspace pending deletion does not block.** No one can finalize a pending deletion before
|
|
1614
|
+
its grace period ends, so an owner who scheduled one would otherwise be unable to delete their own
|
|
1615
|
+
account for the whole period. Their membership there is removed with the rest, the workspace can no
|
|
1616
|
+
longer be restored (restoring is owner-only), and your finalize job deletes it when the grace period
|
|
1617
|
+
ends.
|
|
1618
|
+
- **If a cache bump fails after the commit,** the call throws `MembershipBumpFailedError` with
|
|
1619
|
+
`removed` and `failed`. The removal is already committed, so calling it again removes nothing: drop
|
|
1620
|
+
the access of each entry of `failed` with `WorkspaceAccessService.invalidateMembership`, or wait out
|
|
1621
|
+
`membershipCacheTtlSeconds`.
|
|
1622
|
+
|
|
1581
1623
|
### Accepting an invitation through OAuth, and MFA
|
|
1582
1624
|
|
|
1583
1625
|
An OAuth flow started with a workspace invitation vets the invitation against the provider's profile
|
package/dist/client/index.cjs
CHANGED
|
@@ -341,22 +341,22 @@ function createAuthClient(config) {
|
|
|
341
341
|
}
|
|
342
342
|
return {
|
|
343
343
|
async login(input) {
|
|
344
|
-
return post(shared.
|
|
344
|
+
return post(shared.AUTH_DASHBOARD_ROUTES.login, input);
|
|
345
345
|
},
|
|
346
346
|
async register(data) {
|
|
347
|
-
return post(shared.
|
|
347
|
+
return post(shared.AUTH_DASHBOARD_ROUTES.register, data);
|
|
348
348
|
},
|
|
349
349
|
async logout() {
|
|
350
|
-
return postNoContent(shared.
|
|
350
|
+
return postNoContent(shared.AUTH_DASHBOARD_ROUTES.logout, {});
|
|
351
351
|
},
|
|
352
352
|
async refresh() {
|
|
353
|
-
return post(shared.
|
|
353
|
+
return post(shared.AUTH_DASHBOARD_ROUTES.refresh, {});
|
|
354
354
|
},
|
|
355
355
|
async getMe() {
|
|
356
|
-
return get(shared.
|
|
356
|
+
return get(shared.AUTH_DASHBOARD_ROUTES.me);
|
|
357
357
|
},
|
|
358
358
|
async mfaChallenge(tempToken, code) {
|
|
359
|
-
const result = await post(shared.
|
|
359
|
+
const result = await post(shared.AUTH_MFA_ROUTES.challenge, {
|
|
360
360
|
mfaTempToken: tempToken,
|
|
361
361
|
code
|
|
362
362
|
});
|
|
@@ -369,7 +369,7 @@ function createAuthClient(config) {
|
|
|
369
369
|
return result;
|
|
370
370
|
},
|
|
371
371
|
async forgotPassword(email, tenantId) {
|
|
372
|
-
return postNoContent(shared.
|
|
372
|
+
return postNoContent(shared.AUTH_PASSWORD_ROUTES.forgotPassword, {
|
|
373
373
|
email,
|
|
374
374
|
...tenantId !== void 0 ? { tenantId } : {}
|
|
375
375
|
});
|
|
@@ -383,11 +383,11 @@ function createAuthClient(config) {
|
|
|
383
383
|
if (input.token !== void 0) payload["token"] = input.token;
|
|
384
384
|
if (input.otp !== void 0) payload["otp"] = input.otp;
|
|
385
385
|
if (input.verifiedToken !== void 0) payload["verifiedToken"] = input.verifiedToken;
|
|
386
|
-
return postNoContent(shared.
|
|
386
|
+
return postNoContent(shared.AUTH_PASSWORD_ROUTES.resetPassword, payload);
|
|
387
387
|
}
|
|
388
388
|
};
|
|
389
389
|
}
|
|
390
|
-
var ROUTES = shared.
|
|
390
|
+
var ROUTES = shared.AUTH_WORKSPACE_ROUTES;
|
|
391
391
|
function paged(path, page) {
|
|
392
392
|
const query = new URLSearchParams();
|
|
393
393
|
if (page?.cursor !== void 0) query.set("cursor", page.cursor);
|
package/dist/client/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AUTH_ERROR_CODES,
|
|
1
|
+
import { AUTH_ERROR_CODES, AUTH_PROXY_ROUTES, buildAuthRefreshSkipSuffixes, AUTH_PASSWORD_ROUTES, AUTH_MFA_ROUTES, AuthClientError, AUTH_DASHBOARD_ROUTES, AUTH_WORKSPACE_ROUTES, trimSlashes, fillRoute } from '@bymax-one/nest-auth/shared';
|
|
2
2
|
export { AuthClientError } from '@bymax-one/nest-auth/shared';
|
|
3
3
|
|
|
4
4
|
// src/client/createAuthFetch.ts
|
|
@@ -340,22 +340,22 @@ function createAuthClient(config) {
|
|
|
340
340
|
}
|
|
341
341
|
return {
|
|
342
342
|
async login(input) {
|
|
343
|
-
return post(
|
|
343
|
+
return post(AUTH_DASHBOARD_ROUTES.login, input);
|
|
344
344
|
},
|
|
345
345
|
async register(data) {
|
|
346
|
-
return post(
|
|
346
|
+
return post(AUTH_DASHBOARD_ROUTES.register, data);
|
|
347
347
|
},
|
|
348
348
|
async logout() {
|
|
349
|
-
return postNoContent(
|
|
349
|
+
return postNoContent(AUTH_DASHBOARD_ROUTES.logout, {});
|
|
350
350
|
},
|
|
351
351
|
async refresh() {
|
|
352
|
-
return post(
|
|
352
|
+
return post(AUTH_DASHBOARD_ROUTES.refresh, {});
|
|
353
353
|
},
|
|
354
354
|
async getMe() {
|
|
355
|
-
return get(
|
|
355
|
+
return get(AUTH_DASHBOARD_ROUTES.me);
|
|
356
356
|
},
|
|
357
357
|
async mfaChallenge(tempToken, code) {
|
|
358
|
-
const result = await post(
|
|
358
|
+
const result = await post(AUTH_MFA_ROUTES.challenge, {
|
|
359
359
|
mfaTempToken: tempToken,
|
|
360
360
|
code
|
|
361
361
|
});
|
|
@@ -368,7 +368,7 @@ function createAuthClient(config) {
|
|
|
368
368
|
return result;
|
|
369
369
|
},
|
|
370
370
|
async forgotPassword(email, tenantId) {
|
|
371
|
-
return postNoContent(
|
|
371
|
+
return postNoContent(AUTH_PASSWORD_ROUTES.forgotPassword, {
|
|
372
372
|
email,
|
|
373
373
|
...tenantId !== void 0 ? { tenantId } : {}
|
|
374
374
|
});
|
|
@@ -382,11 +382,11 @@ function createAuthClient(config) {
|
|
|
382
382
|
if (input.token !== void 0) payload["token"] = input.token;
|
|
383
383
|
if (input.otp !== void 0) payload["otp"] = input.otp;
|
|
384
384
|
if (input.verifiedToken !== void 0) payload["verifiedToken"] = input.verifiedToken;
|
|
385
|
-
return postNoContent(
|
|
385
|
+
return postNoContent(AUTH_PASSWORD_ROUTES.resetPassword, payload);
|
|
386
386
|
}
|
|
387
387
|
};
|
|
388
388
|
}
|
|
389
|
-
var ROUTES =
|
|
389
|
+
var ROUTES = AUTH_WORKSPACE_ROUTES;
|
|
390
390
|
function paged(path, page) {
|
|
391
391
|
const query = new URLSearchParams();
|
|
392
392
|
if (page?.cursor !== void 0) query.set("cursor", page.cursor);
|
package/dist/react/index.cjs
CHANGED
|
@@ -354,7 +354,7 @@ function useWorkspaceList(client, authStatus, identity) {
|
|
|
354
354
|
};
|
|
355
355
|
}, [authStatus, identity, refresh]);
|
|
356
356
|
const loaded = stored.owner === identity ? stored : identity === null ? IDLE : loadingFor(identity);
|
|
357
|
-
return { loaded, setLoaded, clientRef, identityRef, refresh };
|
|
357
|
+
return { loaded, setLoaded, clientRef, identityRef, statusRef, refresh };
|
|
358
358
|
}
|
|
359
359
|
function useChangeNotice(status, active, optionsRef) {
|
|
360
360
|
const previousRef = react.useRef(
|
|
@@ -372,20 +372,29 @@ function useChangeNotice(status, active, optionsRef) {
|
|
|
372
372
|
);
|
|
373
373
|
}, [status, active, optionsRef]);
|
|
374
374
|
}
|
|
375
|
-
function
|
|
375
|
+
function useBroadcastReload(sessionRefresh, statusRef, identityRef, reload) {
|
|
376
|
+
const [request, setRequest] = react.useState(null);
|
|
377
|
+
react.useEffect(() => {
|
|
378
|
+
if (request !== null && request.heardAs === identityRef.current) void reload();
|
|
379
|
+
}, [request, identityRef, reload]);
|
|
380
|
+
return react.useCallback(() => {
|
|
381
|
+
if (statusRef.current !== "authenticated") return;
|
|
382
|
+
const heardAs = identityRef.current;
|
|
383
|
+
void sessionRefresh().then(() => setRequest({ heardAs }));
|
|
384
|
+
}, [sessionRefresh, statusRef, identityRef]);
|
|
385
|
+
}
|
|
386
|
+
function useWorkspaceChannel(onMessage) {
|
|
376
387
|
const channelRef = react.useRef(null);
|
|
377
388
|
react.useEffect(() => {
|
|
378
389
|
if (typeof BroadcastChannel === "undefined") return void 0;
|
|
379
390
|
const channel = new BroadcastChannel(CHANNEL_NAME);
|
|
380
|
-
channel.onmessage =
|
|
381
|
-
void refresh();
|
|
382
|
-
};
|
|
391
|
+
channel.onmessage = onMessage;
|
|
383
392
|
channelRef.current = channel;
|
|
384
393
|
return () => {
|
|
385
394
|
channelRef.current = null;
|
|
386
395
|
channel.close();
|
|
387
396
|
};
|
|
388
|
-
}, [
|
|
397
|
+
}, [onMessage]);
|
|
389
398
|
return channelRef;
|
|
390
399
|
}
|
|
391
400
|
function useRoleHint(active, items, optionsRef) {
|
|
@@ -449,7 +458,7 @@ function WorkspaceProvider({
|
|
|
449
458
|
optionsRef.current = options;
|
|
450
459
|
});
|
|
451
460
|
const identity = ctx.user === null ? null : JSON.stringify([ctx.user.tenantId, ctx.user.id]);
|
|
452
|
-
const { loaded, setLoaded, clientRef, identityRef, refresh } = useWorkspaceList(
|
|
461
|
+
const { loaded, setLoaded, clientRef, identityRef, statusRef, refresh } = useWorkspaceList(
|
|
453
462
|
client,
|
|
454
463
|
ctx.status,
|
|
455
464
|
identity
|
|
@@ -460,7 +469,8 @@ function WorkspaceProvider({
|
|
|
460
469
|
[loaded.items, activeId]
|
|
461
470
|
);
|
|
462
471
|
useChangeNotice(loaded.status, activeWorkspace, optionsRef);
|
|
463
|
-
const
|
|
472
|
+
const onBroadcast = useBroadcastReload(ctx.refresh, statusRef, identityRef, refresh);
|
|
473
|
+
const channelRef = useWorkspaceChannel(onBroadcast);
|
|
464
474
|
const hasWorkspaceRole = useRoleHint(activeWorkspace, loaded.items, optionsRef);
|
|
465
475
|
const handleWorkspaceError = useRefusalHandler(activeId, optionsRef, refresh);
|
|
466
476
|
const switchWorkspace = useSwitch(clientRef, identityRef, setLoaded, channelRef);
|
package/dist/react/index.mjs
CHANGED
|
@@ -352,7 +352,7 @@ function useWorkspaceList(client, authStatus, identity) {
|
|
|
352
352
|
};
|
|
353
353
|
}, [authStatus, identity, refresh]);
|
|
354
354
|
const loaded = stored.owner === identity ? stored : identity === null ? IDLE : loadingFor(identity);
|
|
355
|
-
return { loaded, setLoaded, clientRef, identityRef, refresh };
|
|
355
|
+
return { loaded, setLoaded, clientRef, identityRef, statusRef, refresh };
|
|
356
356
|
}
|
|
357
357
|
function useChangeNotice(status, active, optionsRef) {
|
|
358
358
|
const previousRef = useRef(
|
|
@@ -370,20 +370,29 @@ function useChangeNotice(status, active, optionsRef) {
|
|
|
370
370
|
);
|
|
371
371
|
}, [status, active, optionsRef]);
|
|
372
372
|
}
|
|
373
|
-
function
|
|
373
|
+
function useBroadcastReload(sessionRefresh, statusRef, identityRef, reload) {
|
|
374
|
+
const [request, setRequest] = useState(null);
|
|
375
|
+
useEffect(() => {
|
|
376
|
+
if (request !== null && request.heardAs === identityRef.current) void reload();
|
|
377
|
+
}, [request, identityRef, reload]);
|
|
378
|
+
return useCallback(() => {
|
|
379
|
+
if (statusRef.current !== "authenticated") return;
|
|
380
|
+
const heardAs = identityRef.current;
|
|
381
|
+
void sessionRefresh().then(() => setRequest({ heardAs }));
|
|
382
|
+
}, [sessionRefresh, statusRef, identityRef]);
|
|
383
|
+
}
|
|
384
|
+
function useWorkspaceChannel(onMessage) {
|
|
374
385
|
const channelRef = useRef(null);
|
|
375
386
|
useEffect(() => {
|
|
376
387
|
if (typeof BroadcastChannel === "undefined") return void 0;
|
|
377
388
|
const channel = new BroadcastChannel(CHANNEL_NAME);
|
|
378
|
-
channel.onmessage =
|
|
379
|
-
void refresh();
|
|
380
|
-
};
|
|
389
|
+
channel.onmessage = onMessage;
|
|
381
390
|
channelRef.current = channel;
|
|
382
391
|
return () => {
|
|
383
392
|
channelRef.current = null;
|
|
384
393
|
channel.close();
|
|
385
394
|
};
|
|
386
|
-
}, [
|
|
395
|
+
}, [onMessage]);
|
|
387
396
|
return channelRef;
|
|
388
397
|
}
|
|
389
398
|
function useRoleHint(active, items, optionsRef) {
|
|
@@ -447,7 +456,7 @@ function WorkspaceProvider({
|
|
|
447
456
|
optionsRef.current = options;
|
|
448
457
|
});
|
|
449
458
|
const identity = ctx.user === null ? null : JSON.stringify([ctx.user.tenantId, ctx.user.id]);
|
|
450
|
-
const { loaded, setLoaded, clientRef, identityRef, refresh } = useWorkspaceList(
|
|
459
|
+
const { loaded, setLoaded, clientRef, identityRef, statusRef, refresh } = useWorkspaceList(
|
|
451
460
|
client,
|
|
452
461
|
ctx.status,
|
|
453
462
|
identity
|
|
@@ -458,7 +467,8 @@ function WorkspaceProvider({
|
|
|
458
467
|
[loaded.items, activeId]
|
|
459
468
|
);
|
|
460
469
|
useChangeNotice(loaded.status, activeWorkspace, optionsRef);
|
|
461
|
-
const
|
|
470
|
+
const onBroadcast = useBroadcastReload(ctx.refresh, statusRef, identityRef, refresh);
|
|
471
|
+
const channelRef = useWorkspaceChannel(onBroadcast);
|
|
462
472
|
const hasWorkspaceRole = useRoleHint(activeWorkspace, loaded.items, optionsRef);
|
|
463
473
|
const handleWorkspaceError = useRefusalHandler(activeId, optionsRef, refresh);
|
|
464
474
|
const switchWorkspace = useSwitch(clientRef, identityRef, setLoaded, channelRef);
|