@koolbase/core 12.43.0 → 12.45.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/dist/cjs/app-admin.d.ts +55 -0
- package/dist/cjs/app-admin.js +107 -0
- package/dist/cjs/auth-errors.d.ts +32 -0
- package/dist/cjs/auth-errors.js +69 -0
- package/dist/cjs/auth.d.ts +22 -0
- package/dist/cjs/auth.js +107 -9
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/types.d.ts +74 -0
- package/dist/esm/app-admin.d.ts +55 -0
- package/dist/esm/app-admin.js +103 -0
- package/dist/esm/auth-errors.d.ts +32 -0
- package/dist/esm/auth-errors.js +61 -0
- package/dist/esm/auth.d.ts +22 -0
- package/dist/esm/auth.js +108 -10
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/types.d.ts +74 -0
- package/package.json +1 -1
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { KoolbaseAssignableRole, KoolbaseMember, KoolbaseMemberPage, KoolbaseMemberRoles } from './types.js';
|
|
2
|
+
type AdminRequest = (path: string, options?: {
|
|
3
|
+
method?: string;
|
|
4
|
+
body?: unknown;
|
|
5
|
+
}) => Promise<any>;
|
|
6
|
+
/**
|
|
7
|
+
* Managing the app's people from inside the app, for its own admins.
|
|
8
|
+
*
|
|
9
|
+
* Only a signed-in person whose `app_admin` claim is true may use these; the
|
|
10
|
+
* project owner gives that claim in the dashboard. Everyone else gets
|
|
11
|
+
* `NotAppAdminError`. The server checks on every request, so taking the claim
|
|
12
|
+
* away takes effect at once.
|
|
13
|
+
*
|
|
14
|
+
* When the admin has two-step sign-in on, disabling, enabling and changing
|
|
15
|
+
* roles need it confirmed in the last ten minutes: catch
|
|
16
|
+
* `RecentMfaRequiredError`, call `stepUpMfa`, and try again.
|
|
17
|
+
*
|
|
18
|
+
* Reached through `Koolbase.auth.admin`; not built by apps.
|
|
19
|
+
*/
|
|
20
|
+
export declare class KoolbaseAppAdmin {
|
|
21
|
+
private readonly request;
|
|
22
|
+
constructor(request: AdminRequest);
|
|
23
|
+
/** The app's people, newest first. `query` matches email or name. */
|
|
24
|
+
members(options?: {
|
|
25
|
+
query?: string;
|
|
26
|
+
limit?: number;
|
|
27
|
+
offset?: number;
|
|
28
|
+
}): Promise<KoolbaseMemberPage>;
|
|
29
|
+
/** The roles admins may give, for a role picker. */
|
|
30
|
+
roles(): Promise<KoolbaseAssignableRole[]>;
|
|
31
|
+
/**
|
|
32
|
+
* Invite someone by email. They get an email to set up their account, and
|
|
33
|
+
* `roles` are given at once. Throws `MemberExistsError` when the email
|
|
34
|
+
* already has an account.
|
|
35
|
+
*/
|
|
36
|
+
invite(params: {
|
|
37
|
+
email: string;
|
|
38
|
+
fullName?: string;
|
|
39
|
+
roles?: Record<string, unknown>;
|
|
40
|
+
}): Promise<KoolbaseMember>;
|
|
41
|
+
/** Stop someone signing in, and sign them out everywhere. */
|
|
42
|
+
disable(userId: string): Promise<void>;
|
|
43
|
+
/** Let a disabled person sign in again. */
|
|
44
|
+
enable(userId: string): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Change someone's roles. Only the keys given change; `null` removes a
|
|
47
|
+
* role. Pass the member's `version` as `expectedVersion`: when someone else
|
|
48
|
+
* changed them since, this throws `ClaimsVersionConflictError` carrying the
|
|
49
|
+
* new version, and nothing is changed.
|
|
50
|
+
*/
|
|
51
|
+
setRoles(userId: string, roles: Record<string, unknown>, options: {
|
|
52
|
+
expectedVersion: number;
|
|
53
|
+
}): Promise<KoolbaseMemberRoles>;
|
|
54
|
+
}
|
|
55
|
+
export {};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KoolbaseAppAdmin = void 0;
|
|
4
|
+
function mapMember(r) {
|
|
5
|
+
return {
|
|
6
|
+
id: String(r.id),
|
|
7
|
+
email: String(r.email ?? ''),
|
|
8
|
+
fullName: r.full_name ?? undefined,
|
|
9
|
+
avatarUrl: r.avatar_url ?? undefined,
|
|
10
|
+
status: r.status ?? 'active',
|
|
11
|
+
isAdmin: Boolean(r.is_admin),
|
|
12
|
+
roles: r.roles ?? {},
|
|
13
|
+
version: Number(r.version ?? 0),
|
|
14
|
+
joinedAt: String(r.joined_at),
|
|
15
|
+
lastSeenAt: r.last_seen_at ?? undefined,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Managing the app's people from inside the app, for its own admins.
|
|
20
|
+
*
|
|
21
|
+
* Only a signed-in person whose `app_admin` claim is true may use these; the
|
|
22
|
+
* project owner gives that claim in the dashboard. Everyone else gets
|
|
23
|
+
* `NotAppAdminError`. The server checks on every request, so taking the claim
|
|
24
|
+
* away takes effect at once.
|
|
25
|
+
*
|
|
26
|
+
* When the admin has two-step sign-in on, disabling, enabling and changing
|
|
27
|
+
* roles need it confirmed in the last ten minutes: catch
|
|
28
|
+
* `RecentMfaRequiredError`, call `stepUpMfa`, and try again.
|
|
29
|
+
*
|
|
30
|
+
* Reached through `Koolbase.auth.admin`; not built by apps.
|
|
31
|
+
*/
|
|
32
|
+
class KoolbaseAppAdmin {
|
|
33
|
+
constructor(request) {
|
|
34
|
+
this.request = request;
|
|
35
|
+
}
|
|
36
|
+
/** The app's people, newest first. `query` matches email or name. */
|
|
37
|
+
async members(options) {
|
|
38
|
+
const params = new URLSearchParams();
|
|
39
|
+
if (options?.query)
|
|
40
|
+
params.set('q', options.query);
|
|
41
|
+
if (options?.limit !== undefined)
|
|
42
|
+
params.set('limit', String(options.limit));
|
|
43
|
+
if (options?.offset !== undefined)
|
|
44
|
+
params.set('offset', String(options.offset));
|
|
45
|
+
const qs = params.toString();
|
|
46
|
+
const body = await this.request(`/v1/sdk/admin/members${qs ? `?${qs}` : ''}`);
|
|
47
|
+
return {
|
|
48
|
+
members: (body.members ?? []).map(mapMember),
|
|
49
|
+
total: Number(body.total ?? 0),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** The roles admins may give, for a role picker. */
|
|
53
|
+
async roles() {
|
|
54
|
+
const body = await this.request('/v1/sdk/admin/roles');
|
|
55
|
+
return (body.roles ?? []).map((r) => ({
|
|
56
|
+
key: String(r.key),
|
|
57
|
+
valueType: String(r.value_type ?? 'string'),
|
|
58
|
+
allowedValues: (r.allowed_values ?? []).map(String),
|
|
59
|
+
description: r.description ?? undefined,
|
|
60
|
+
holders: Number(r.holders ?? 0),
|
|
61
|
+
values: r.values ?? {},
|
|
62
|
+
}));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Invite someone by email. They get an email to set up their account, and
|
|
66
|
+
* `roles` are given at once. Throws `MemberExistsError` when the email
|
|
67
|
+
* already has an account.
|
|
68
|
+
*/
|
|
69
|
+
async invite(params) {
|
|
70
|
+
const body = await this.request('/v1/sdk/admin/members/invite', {
|
|
71
|
+
method: 'POST',
|
|
72
|
+
body: {
|
|
73
|
+
email: params.email,
|
|
74
|
+
...(params.fullName !== undefined ? { full_name: params.fullName } : {}),
|
|
75
|
+
...(params.roles && Object.keys(params.roles).length ? { roles: params.roles } : {}),
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
return mapMember(body);
|
|
79
|
+
}
|
|
80
|
+
/** Stop someone signing in, and sign them out everywhere. */
|
|
81
|
+
async disable(userId) {
|
|
82
|
+
await this.request(`/v1/sdk/admin/members/${encodeURIComponent(userId)}/disable`, {
|
|
83
|
+
method: 'POST',
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/** Let a disabled person sign in again. */
|
|
87
|
+
async enable(userId) {
|
|
88
|
+
await this.request(`/v1/sdk/admin/members/${encodeURIComponent(userId)}/enable`, {
|
|
89
|
+
method: 'POST',
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Change someone's roles. Only the keys given change; `null` removes a
|
|
94
|
+
* role. Pass the member's `version` as `expectedVersion`: when someone else
|
|
95
|
+
* changed them since, this throws `ClaimsVersionConflictError` carrying the
|
|
96
|
+
* new version, and nothing is changed.
|
|
97
|
+
*/
|
|
98
|
+
async setRoles(userId, roles, options) {
|
|
99
|
+
const body = await this.request(`/v1/sdk/admin/members/${encodeURIComponent(userId)}/roles`, { method: 'PUT', body: { roles, expected_version: options.expectedVersion } });
|
|
100
|
+
return {
|
|
101
|
+
id: String(body.id),
|
|
102
|
+
roles: body.roles ?? {},
|
|
103
|
+
version: Number(body.version ?? 0),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
exports.KoolbaseAppAdmin = KoolbaseAppAdmin;
|
|
@@ -299,3 +299,35 @@ export declare class MfaEnrollmentNotFoundError extends KoolbaseAuthError {
|
|
|
299
299
|
export declare class MfaNotEnabledError extends KoolbaseAuthError {
|
|
300
300
|
constructor();
|
|
301
301
|
}
|
|
302
|
+
/** The signed-in person is not an app admin. The project owner gives the `app_admin` claim in the dashboard. */
|
|
303
|
+
export declare class NotAppAdminError extends KoolbaseAuthError {
|
|
304
|
+
constructor();
|
|
305
|
+
}
|
|
306
|
+
/** An admin tried to disable themselves or change their own roles. */
|
|
307
|
+
export declare class AppAdminSelfError extends KoolbaseAuthError {
|
|
308
|
+
constructor();
|
|
309
|
+
}
|
|
310
|
+
/** The person is an app admin too; only the project owner can change them. */
|
|
311
|
+
export declare class AppAdminTargetIsAdminError extends KoolbaseAuthError {
|
|
312
|
+
constructor();
|
|
313
|
+
}
|
|
314
|
+
/** The project has not let app admins give this role. */
|
|
315
|
+
export declare class RoleNotAssignableError extends KoolbaseAuthError {
|
|
316
|
+
constructor(message?: string);
|
|
317
|
+
}
|
|
318
|
+
/** The role exists, but not with this value. */
|
|
319
|
+
export declare class RoleValueNotAllowedError extends KoolbaseAuthError {
|
|
320
|
+
constructor(message?: string);
|
|
321
|
+
}
|
|
322
|
+
/** The invited email already has an account in this app. */
|
|
323
|
+
export declare class MemberExistsError extends KoolbaseAuthError {
|
|
324
|
+
constructor();
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* The person's roles changed since they were read; nothing was changed. Read
|
|
328
|
+
* them again, or retry with `currentVersion`.
|
|
329
|
+
*/
|
|
330
|
+
export declare class ClaimsVersionConflictError extends KoolbaseAuthError {
|
|
331
|
+
readonly currentVersion: number | null;
|
|
332
|
+
constructor(currentVersion: number | null);
|
|
333
|
+
}
|
package/dist/cjs/auth-errors.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MfaNotEnabledError = exports.MfaEnrollmentNotFoundError = exports.MfaAlreadyEnabledError = exports.RecentMfaRequiredError = exports.RecentAuthRequiredError = exports.MfaRequiredError = exports.GoogleEmailRequiredError = exports.InvalidGoogleTokenError = exports.MalformedSessionResponseError = exports.ProviderIdentityAlreadyLinkedError = exports.IdentityNotFoundError = exports.InsufficientScopeError = exports.HideRequiresVerificationError = exports.LastCredentialError = exports.InsufficientAuthorityError = exports.SessionRequiredError = exports.UnsupportedOAuthProviderError = exports.OAuthOnlyAccountError = exports.AccountExistsError = exports.CurrentPasswordIncorrectError = exports.TokenAlreadyUsedError = exports.TokenExpiredError = exports.SignupsDisabledError = exports.ContactNotVerifiedError = exports.GoogleSignInNotConfiguredError = exports.OAuthEmailConflictError = exports.AppleEmailRequiredError = exports.InvalidAppleTokenError = exports.AppleSignInNotConfiguredError = exports.SmsConfigMissingError = exports.PhoneAlreadyLinkedError = exports.OtpRateLimitError = exports.OtpMaxAttemptsError = exports.OtpInvalidError = exports.OtpExpiredError = exports.EmailCodeDisabledError = exports.InvalidPhoneNumberError = exports.NetworkError = exports.VerificationResendDailyCapError = exports.VerificationResendCooldownError = exports.RateLimitError = exports.UnlockTokenInvalidError = exports.AccountLockedError = exports.TokenRevokedError = exports.SessionExpiredError = exports.WeakPasswordError = exports.UserDisabledError = exports.EmailAlreadyInUseError = exports.InvalidCredentialsError = exports.KoolbaseAuthError = void 0;
|
|
4
|
+
exports.ClaimsVersionConflictError = exports.MemberExistsError = exports.RoleValueNotAllowedError = exports.RoleNotAssignableError = exports.AppAdminTargetIsAdminError = exports.AppAdminSelfError = exports.NotAppAdminError = void 0;
|
|
4
5
|
exports.retryAfterOf = retryAfterOf;
|
|
5
6
|
const errors_js_1 = require("./errors.js");
|
|
6
7
|
/**
|
|
@@ -556,3 +557,71 @@ class MfaNotEnabledError extends KoolbaseAuthError {
|
|
|
556
557
|
}
|
|
557
558
|
}
|
|
558
559
|
exports.MfaNotEnabledError = MfaNotEnabledError;
|
|
560
|
+
// ─── In-app admin ──────────────────────────────────────────────────────────
|
|
561
|
+
/** The signed-in person is not an app admin. The project owner gives the `app_admin` claim in the dashboard. */
|
|
562
|
+
class NotAppAdminError extends KoolbaseAuthError {
|
|
563
|
+
constructor() {
|
|
564
|
+
super('Only app admins can do this', 'app_admin_required');
|
|
565
|
+
this.name = 'NotAppAdminError';
|
|
566
|
+
Object.setPrototypeOf(this, NotAppAdminError.prototype);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
exports.NotAppAdminError = NotAppAdminError;
|
|
570
|
+
/** An admin tried to disable themselves or change their own roles. */
|
|
571
|
+
class AppAdminSelfError extends KoolbaseAuthError {
|
|
572
|
+
constructor() {
|
|
573
|
+
super('You cannot change your own account here', 'app_admin_self');
|
|
574
|
+
this.name = 'AppAdminSelfError';
|
|
575
|
+
Object.setPrototypeOf(this, AppAdminSelfError.prototype);
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
exports.AppAdminSelfError = AppAdminSelfError;
|
|
579
|
+
/** The person is an app admin too; only the project owner can change them. */
|
|
580
|
+
class AppAdminTargetIsAdminError extends KoolbaseAuthError {
|
|
581
|
+
constructor() {
|
|
582
|
+
super('Another app admin can only be changed by the project owner', 'app_admin_target_is_admin');
|
|
583
|
+
this.name = 'AppAdminTargetIsAdminError';
|
|
584
|
+
Object.setPrototypeOf(this, AppAdminTargetIsAdminError.prototype);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
exports.AppAdminTargetIsAdminError = AppAdminTargetIsAdminError;
|
|
588
|
+
/** The project has not let app admins give this role. */
|
|
589
|
+
class RoleNotAssignableError extends KoolbaseAuthError {
|
|
590
|
+
constructor(message) {
|
|
591
|
+
super(message ?? 'App admins cannot give this role', 'role_not_assignable');
|
|
592
|
+
this.name = 'RoleNotAssignableError';
|
|
593
|
+
Object.setPrototypeOf(this, RoleNotAssignableError.prototype);
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
exports.RoleNotAssignableError = RoleNotAssignableError;
|
|
597
|
+
/** The role exists, but not with this value. */
|
|
598
|
+
class RoleValueNotAllowedError extends KoolbaseAuthError {
|
|
599
|
+
constructor(message) {
|
|
600
|
+
super(message ?? 'That value is not allowed for this role', 'role_value_not_allowed');
|
|
601
|
+
this.name = 'RoleValueNotAllowedError';
|
|
602
|
+
Object.setPrototypeOf(this, RoleValueNotAllowedError.prototype);
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
exports.RoleValueNotAllowedError = RoleValueNotAllowedError;
|
|
606
|
+
/** The invited email already has an account in this app. */
|
|
607
|
+
class MemberExistsError extends KoolbaseAuthError {
|
|
608
|
+
constructor() {
|
|
609
|
+
super('Someone with this email is already in the app', 'member_exists');
|
|
610
|
+
this.name = 'MemberExistsError';
|
|
611
|
+
Object.setPrototypeOf(this, MemberExistsError.prototype);
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
exports.MemberExistsError = MemberExistsError;
|
|
615
|
+
/**
|
|
616
|
+
* The person's roles changed since they were read; nothing was changed. Read
|
|
617
|
+
* them again, or retry with `currentVersion`.
|
|
618
|
+
*/
|
|
619
|
+
class ClaimsVersionConflictError extends KoolbaseAuthError {
|
|
620
|
+
constructor(currentVersion) {
|
|
621
|
+
super("This person's roles changed since you opened them", 'claims_version_conflict');
|
|
622
|
+
this.currentVersion = currentVersion;
|
|
623
|
+
this.name = 'ClaimsVersionConflictError';
|
|
624
|
+
Object.setPrototypeOf(this, ClaimsVersionConflictError.prototype);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
exports.ClaimsVersionConflictError = ClaimsVersionConflictError;
|
package/dist/cjs/auth.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AuthStateListener, KoolbaseConfig, KoolbaseSession, KoolbaseUser, LinkPhoneParams, LoginParams, OtpSendResult, PhoneVerifyResult, RegisterParams, RestoreResult, KoolbaseAuditPage, KoolbaseSessionInfo, ResendVerificationResult, SendOtpParams, SignUpResult, SignInWithAppleParams, VerifyOtpParams } from './types.js';
|
|
2
|
+
import { KoolbaseAppAdmin } from './app-admin.js';
|
|
2
3
|
import type { KoolbaseAvatarFile, SignInWithGoogleParams, SignOutReason } from './types.js';
|
|
3
4
|
export declare class KoolbaseAuth {
|
|
4
5
|
private config;
|
|
@@ -71,6 +72,19 @@ export declare class KoolbaseAuth {
|
|
|
71
72
|
* the caller's own request would have failed first.
|
|
72
73
|
*/
|
|
73
74
|
private updateUserAndPersist;
|
|
75
|
+
/**
|
|
76
|
+
* Remember me: the choice of the sign-in under way, held until its session
|
|
77
|
+
* is saved -- through a second step (MFA) too. Null: none under way.
|
|
78
|
+
*/
|
|
79
|
+
private pendingRemember;
|
|
80
|
+
/** A sign-in starts: its Remember me, omitted meaning remembered. */
|
|
81
|
+
private beginSignIn;
|
|
82
|
+
/**
|
|
83
|
+
* A sign-in's session: told to the storage where it lives first (Remember
|
|
84
|
+
* me), then saved. A refresh or a profile change is not a sign-in and
|
|
85
|
+
* keeps the place the session already has.
|
|
86
|
+
*/
|
|
87
|
+
private setSignedInSession;
|
|
74
88
|
private setSessionInternal;
|
|
75
89
|
/**
|
|
76
90
|
* Discards the stored session without contacting the server.
|
|
@@ -327,6 +341,13 @@ export declare class KoolbaseAuth {
|
|
|
327
341
|
}): Promise<KoolbaseAuditPage>;
|
|
328
342
|
unlock(token: string): Promise<void>;
|
|
329
343
|
get currentUser(): KoolbaseUser | null;
|
|
344
|
+
private _admin?;
|
|
345
|
+
/**
|
|
346
|
+
* Managing the app's people, for the app's own admins (the `app_admin`
|
|
347
|
+
* claim). See {@link KoolbaseAppAdmin}.
|
|
348
|
+
*/
|
|
349
|
+
get admin(): KoolbaseAppAdmin;
|
|
350
|
+
private adminRequest;
|
|
330
351
|
get accessToken(): string | null;
|
|
331
352
|
/**
|
|
332
353
|
* Currently-valid access token for data-plane requests, refreshing
|
|
@@ -378,6 +399,7 @@ export declare class KoolbaseAuth {
|
|
|
378
399
|
signInWithEmailCode(params: {
|
|
379
400
|
email: string;
|
|
380
401
|
code: string;
|
|
402
|
+
remember?: boolean;
|
|
381
403
|
}): Promise<KoolbaseSession>;
|
|
382
404
|
private mfaRequired;
|
|
383
405
|
/** Finishes sign-in with a code from the person's authenticator app. */
|
package/dist/cjs/auth.js
CHANGED
|
@@ -5,6 +5,7 @@ const errors_1 = require("./errors");
|
|
|
5
5
|
const network_js_1 = require("./network.js");
|
|
6
6
|
const types_js_1 = require("./types.js");
|
|
7
7
|
const auth_errors_js_1 = require("./auth-errors.js");
|
|
8
|
+
const app_admin_js_1 = require("./app-admin.js");
|
|
8
9
|
const errors_js_1 = require("./errors.js");
|
|
9
10
|
const platform_js_1 = require("./platform.js");
|
|
10
11
|
const device_metadata_js_1 = require("./device-metadata.js");
|
|
@@ -26,6 +27,11 @@ class KoolbaseAuth {
|
|
|
26
27
|
this.signOutReason = null;
|
|
27
28
|
/** The last renewal failed for want of a connection, not because the server refused it (#96). */
|
|
28
29
|
this.renewalUnavailable = false;
|
|
30
|
+
/**
|
|
31
|
+
* Remember me: the choice of the sign-in under way, held until its session
|
|
32
|
+
* is saved -- through a second step (MFA) too. Null: none under way.
|
|
33
|
+
*/
|
|
34
|
+
this.pendingRemember = null;
|
|
29
35
|
this.config = config;
|
|
30
36
|
this.metadata = new device_metadata_js_1.DeviceMetadata(config.appVersion);
|
|
31
37
|
this.fetchFn = config.fetch ?? ((url, init) => (0, network_js_1.koolbaseFetch)(url, init));
|
|
@@ -165,6 +171,29 @@ class KoolbaseAuth {
|
|
|
165
171
|
return;
|
|
166
172
|
await this.setSessionInternal({ ...this.session, user });
|
|
167
173
|
}
|
|
174
|
+
/** A sign-in starts: its Remember me, omitted meaning remembered. */
|
|
175
|
+
beginSignIn(remember) {
|
|
176
|
+
this.pendingRemember = remember ?? true;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* A sign-in's session: told to the storage where it lives first (Remember
|
|
180
|
+
* me), then saved. A refresh or a profile change is not a sign-in and
|
|
181
|
+
* keeps the place the session already has.
|
|
182
|
+
*/
|
|
183
|
+
async setSignedInSession(session) {
|
|
184
|
+
const remember = this.pendingRemember ?? true;
|
|
185
|
+
this.pendingRemember = null;
|
|
186
|
+
if (this.storage?.setRemember) {
|
|
187
|
+
try {
|
|
188
|
+
await this.storage.setRemember(remember);
|
|
189
|
+
}
|
|
190
|
+
catch (err) {
|
|
191
|
+
// eslint-disable-next-line no-console
|
|
192
|
+
console.warn('[Koolbase] Could not apply Remember me:', err);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
await this.setSessionInternal(session);
|
|
196
|
+
}
|
|
168
197
|
async setSessionInternal(session) {
|
|
169
198
|
this.session = session;
|
|
170
199
|
this.signOutReason = null;
|
|
@@ -348,16 +377,20 @@ class KoolbaseAuth {
|
|
|
348
377
|
};
|
|
349
378
|
}
|
|
350
379
|
const session = this.sessionFromBody(data);
|
|
351
|
-
|
|
380
|
+
this.beginSignIn(undefined);
|
|
381
|
+
await this.setSignedInSession(session);
|
|
352
382
|
return { status: 'authenticated', user: session.user, session };
|
|
353
383
|
}
|
|
354
384
|
async login(params) {
|
|
385
|
+
// Remember me is this device's, never the server's.
|
|
386
|
+
const { remember, ...body } = params;
|
|
387
|
+
this.beginSignIn(remember);
|
|
355
388
|
const res = await this.authRequest('/v1/sdk/auth/login', {
|
|
356
389
|
method: 'POST',
|
|
357
|
-
body
|
|
390
|
+
body,
|
|
358
391
|
});
|
|
359
392
|
const session = await this.parseSessionResponse(res, false);
|
|
360
|
-
await this.
|
|
393
|
+
await this.setSignedInSession(session);
|
|
361
394
|
return session;
|
|
362
395
|
}
|
|
363
396
|
/**
|
|
@@ -386,6 +419,7 @@ class KoolbaseAuth {
|
|
|
386
419
|
* but auto-link rule blocked (409).
|
|
387
420
|
*/
|
|
388
421
|
async signInWithApple(params) {
|
|
422
|
+
this.beginSignIn(params.remember);
|
|
389
423
|
const body = {
|
|
390
424
|
identity_token: params.identityToken,
|
|
391
425
|
};
|
|
@@ -415,7 +449,7 @@ class KoolbaseAuth {
|
|
|
415
449
|
body,
|
|
416
450
|
});
|
|
417
451
|
const session = await this.parseAppleSessionResponse(res);
|
|
418
|
-
await this.
|
|
452
|
+
await this.setSignedInSession(session);
|
|
419
453
|
return session;
|
|
420
454
|
}
|
|
421
455
|
/**
|
|
@@ -441,6 +475,7 @@ class KoolbaseAuth {
|
|
|
441
475
|
* but auto-link rule blocked (409).
|
|
442
476
|
*/
|
|
443
477
|
async signInWithGoogle(params) {
|
|
478
|
+
this.beginSignIn(params.remember);
|
|
444
479
|
const body = {
|
|
445
480
|
identity_token: params.idToken,
|
|
446
481
|
};
|
|
@@ -452,7 +487,7 @@ class KoolbaseAuth {
|
|
|
452
487
|
body,
|
|
453
488
|
});
|
|
454
489
|
const session = await this.parseGoogleSessionResponse(res);
|
|
455
|
-
await this.
|
|
490
|
+
await this.setSignedInSession(session);
|
|
456
491
|
return session;
|
|
457
492
|
}
|
|
458
493
|
/**
|
|
@@ -968,6 +1003,66 @@ class KoolbaseAuth {
|
|
|
968
1003
|
get currentUser() {
|
|
969
1004
|
return this.session?.user ?? null;
|
|
970
1005
|
}
|
|
1006
|
+
/**
|
|
1007
|
+
* Managing the app's people, for the app's own admins (the `app_admin`
|
|
1008
|
+
* claim). See {@link KoolbaseAppAdmin}.
|
|
1009
|
+
*/
|
|
1010
|
+
get admin() {
|
|
1011
|
+
if (!this._admin) {
|
|
1012
|
+
this._admin = new app_admin_js_1.KoolbaseAppAdmin((path, options) => this.adminRequest(path, options));
|
|
1013
|
+
}
|
|
1014
|
+
return this._admin;
|
|
1015
|
+
}
|
|
1016
|
+
async adminRequest(path, options = {}) {
|
|
1017
|
+
const res = await this.authedRequest(path, options);
|
|
1018
|
+
if (res.ok)
|
|
1019
|
+
return res.json().catch(() => ({}));
|
|
1020
|
+
let body = {};
|
|
1021
|
+
try {
|
|
1022
|
+
body = await res.json();
|
|
1023
|
+
}
|
|
1024
|
+
catch {
|
|
1025
|
+
// ignore
|
|
1026
|
+
}
|
|
1027
|
+
const code = body.code ?? '';
|
|
1028
|
+
const msg = body.error ?? '';
|
|
1029
|
+
// The admin routes' own codes first. Their plain failures (a member not
|
|
1030
|
+
// found, a conflict, a refusal) keep the server's words: the general
|
|
1031
|
+
// status fallback would call a 409 an email in use and a 403 a disabled
|
|
1032
|
+
// account, neither of which an admin route means.
|
|
1033
|
+
switch (code) {
|
|
1034
|
+
// Never sent by these routes; mapped so every parser here handles the
|
|
1035
|
+
// challenge the same way (mfa.test checks each switch).
|
|
1036
|
+
case 'mfa_required':
|
|
1037
|
+
throw this.mfaRequired(body);
|
|
1038
|
+
case 'app_admin_required':
|
|
1039
|
+
throw new auth_errors_js_1.NotAppAdminError();
|
|
1040
|
+
case 'app_admin_self':
|
|
1041
|
+
throw new auth_errors_js_1.AppAdminSelfError();
|
|
1042
|
+
case 'app_admin_target_is_admin':
|
|
1043
|
+
throw new auth_errors_js_1.AppAdminTargetIsAdminError();
|
|
1044
|
+
case 'role_not_assignable':
|
|
1045
|
+
throw new auth_errors_js_1.RoleNotAssignableError(msg || undefined);
|
|
1046
|
+
case 'role_value_not_allowed':
|
|
1047
|
+
throw new auth_errors_js_1.RoleValueNotAllowedError(msg || undefined);
|
|
1048
|
+
case 'member_exists':
|
|
1049
|
+
throw new auth_errors_js_1.MemberExistsError();
|
|
1050
|
+
case 'claims_version_conflict':
|
|
1051
|
+
throw new auth_errors_js_1.ClaimsVersionConflictError(typeof body.version === 'number' ? body.version : null);
|
|
1052
|
+
case 'not_found':
|
|
1053
|
+
case 'conflict':
|
|
1054
|
+
case 'permission_denied':
|
|
1055
|
+
case 'validation_error':
|
|
1056
|
+
throw new auth_errors_js_1.KoolbaseAuthError(msg || 'The request was refused.', code);
|
|
1057
|
+
}
|
|
1058
|
+
// The body is read; hand the general parser what it read.
|
|
1059
|
+
return this.throwTypedError({
|
|
1060
|
+
ok: false,
|
|
1061
|
+
status: res.status,
|
|
1062
|
+
headers: res.headers,
|
|
1063
|
+
json: async () => body,
|
|
1064
|
+
});
|
|
1065
|
+
}
|
|
971
1066
|
get accessToken() {
|
|
972
1067
|
return this.session?.accessToken ?? null;
|
|
973
1068
|
}
|
|
@@ -1045,12 +1140,13 @@ class KoolbaseAuth {
|
|
|
1045
1140
|
* @throws EmailCodeDisabledError if the project has switched this off
|
|
1046
1141
|
*/
|
|
1047
1142
|
async signInWithEmailCode(params) {
|
|
1143
|
+
this.beginSignIn(params.remember);
|
|
1048
1144
|
const res = await this.authRequest('/v1/sdk/auth/email/code/verify', {
|
|
1049
1145
|
method: 'POST',
|
|
1050
1146
|
body: { email: params.email, code: params.code },
|
|
1051
1147
|
});
|
|
1052
1148
|
const session = await this.parseSessionResponse(res, false);
|
|
1053
|
-
await this.
|
|
1149
|
+
await this.setSignedInSession(session);
|
|
1054
1150
|
return session;
|
|
1055
1151
|
}
|
|
1056
1152
|
// ─── Two-step sign-in (MFA) ─────────────────────────────────────────────
|
|
@@ -1070,7 +1166,8 @@ class KoolbaseAuth {
|
|
|
1070
1166
|
body: { challenge_token: params.challengeToken, code: params.code },
|
|
1071
1167
|
});
|
|
1072
1168
|
const session = await this.parseSessionResponse(res, false);
|
|
1073
|
-
|
|
1169
|
+
// The second step of the sign-in that asked for it: its Remember me.
|
|
1170
|
+
await this.setSignedInSession(session);
|
|
1074
1171
|
return session;
|
|
1075
1172
|
}
|
|
1076
1173
|
/**
|
|
@@ -1096,7 +1193,7 @@ class KoolbaseAuth {
|
|
|
1096
1193
|
expiresAt: data.expires_at,
|
|
1097
1194
|
user: this.mapUser(data.user),
|
|
1098
1195
|
};
|
|
1099
|
-
await this.
|
|
1196
|
+
await this.setSignedInSession(session);
|
|
1100
1197
|
return { session, recoveryCodesRemaining: Number(data.recovery_codes_remaining ?? 0) };
|
|
1101
1198
|
}
|
|
1102
1199
|
/**
|
|
@@ -1161,6 +1258,7 @@ class KoolbaseAuth {
|
|
|
1161
1258
|
}
|
|
1162
1259
|
async verifyOtp(params) {
|
|
1163
1260
|
this.validatePhone(params.phoneNumber);
|
|
1261
|
+
this.beginSignIn(params.remember);
|
|
1164
1262
|
const res = await this.authRequest('/v1/sdk/auth/phone/verify-otp', {
|
|
1165
1263
|
method: 'POST',
|
|
1166
1264
|
body: {
|
|
@@ -1175,7 +1273,7 @@ class KoolbaseAuth {
|
|
|
1175
1273
|
expiresAt: data.expires_at,
|
|
1176
1274
|
user: this.mapUser(data.user),
|
|
1177
1275
|
};
|
|
1178
|
-
await this.
|
|
1276
|
+
await this.setSignedInSession(session);
|
|
1179
1277
|
return { session, isNewUser: data.is_new_user ?? false };
|
|
1180
1278
|
}
|
|
1181
1279
|
async linkPhone(params) {
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export * from './auth-errors.js';
|
|
|
10
10
|
export * from './database-errors.js';
|
|
11
11
|
export * from './storage-errors.js';
|
|
12
12
|
export { KoolbaseAuth } from './auth.js';
|
|
13
|
+
export { KoolbaseAppAdmin } from './app-admin.js';
|
|
13
14
|
export { KoolbaseDatabase } from './database.js';
|
|
14
15
|
export { KoolbaseCollectionController, collectionQueryKey, initialCollectionState, } from './collection-controller.js';
|
|
15
16
|
export type { CollectionQuery, CollectionState, CollectionStatus } from './collection-controller.js';
|
package/dist/cjs/index.js
CHANGED
|
@@ -19,7 +19,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
19
19
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
20
20
|
};
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.memoryPlatform = exports.getPlatform = exports.setPlatform = exports.RestoreResult = exports.koolbaseSdkVersion = exports.getOrCreateDeviceId = exports.KoolbaseMessaging = exports.isPending = exports.isFiscalized = exports.decodeFiscalResponse = exports.KoolbaseFiscalError = exports.KoolbaseFiscal = exports.disabledAnalytics = exports.KoolbaseAnalytics = exports.initialUploadQueueState = exports.KoolbaseUploadQueue = exports.initialFileState = exports.KoolbaseFileController = exports.filesQueryKey = exports.initialFilesState = exports.KoolbaseFilesController = exports.KoolbaseUploadTask = exports.KoolbaseStorage = exports.KoolbaseConnectivity = exports.KoolbaseRealtime = exports.KoolbaseFunctions = exports.KoolbaseFlags = exports.initialRecordState = exports.KoolbaseRecordController = exports.initialCollectionState = exports.collectionQueryKey = exports.KoolbaseCollectionController = exports.KoolbaseDatabase = exports.KoolbaseAuth = void 0;
|
|
22
|
+
exports.memoryPlatform = exports.getPlatform = exports.setPlatform = exports.RestoreResult = exports.koolbaseSdkVersion = exports.getOrCreateDeviceId = exports.KoolbaseMessaging = exports.isPending = exports.isFiscalized = exports.decodeFiscalResponse = exports.KoolbaseFiscalError = exports.KoolbaseFiscal = exports.disabledAnalytics = exports.KoolbaseAnalytics = exports.initialUploadQueueState = exports.KoolbaseUploadQueue = exports.initialFileState = exports.KoolbaseFileController = exports.filesQueryKey = exports.initialFilesState = exports.KoolbaseFilesController = exports.KoolbaseUploadTask = exports.KoolbaseStorage = exports.KoolbaseConnectivity = exports.KoolbaseRealtime = exports.KoolbaseFunctions = exports.KoolbaseFlags = exports.initialRecordState = exports.KoolbaseRecordController = exports.initialCollectionState = exports.collectionQueryKey = exports.KoolbaseCollectionController = exports.KoolbaseDatabase = exports.KoolbaseAppAdmin = exports.KoolbaseAuth = void 0;
|
|
23
23
|
__exportStar(require("./types.js"), exports);
|
|
24
24
|
__exportStar(require("./errors.js"), exports);
|
|
25
25
|
__exportStar(require("./network.js"), exports);
|
|
@@ -33,6 +33,8 @@ __exportStar(require("./database-errors.js"), exports);
|
|
|
33
33
|
__exportStar(require("./storage-errors.js"), exports);
|
|
34
34
|
var auth_js_1 = require("./auth.js");
|
|
35
35
|
Object.defineProperty(exports, "KoolbaseAuth", { enumerable: true, get: function () { return auth_js_1.KoolbaseAuth; } });
|
|
36
|
+
var app_admin_js_1 = require("./app-admin.js");
|
|
37
|
+
Object.defineProperty(exports, "KoolbaseAppAdmin", { enumerable: true, get: function () { return app_admin_js_1.KoolbaseAppAdmin; } });
|
|
36
38
|
var database_js_1 = require("./database.js");
|
|
37
39
|
Object.defineProperty(exports, "KoolbaseDatabase", { enumerable: true, get: function () { return database_js_1.KoolbaseDatabase; } });
|
|
38
40
|
var collection_controller_js_1 = require("./collection-controller.js");
|