@koolbase/core 10.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 +19 -0
- package/dist/cjs/analytics.d.ts +24 -0
- package/dist/cjs/analytics.js +110 -0
- package/dist/cjs/auth-errors.d.ts +117 -0
- package/dist/cjs/auth-errors.js +250 -0
- package/dist/cjs/auth.d.ts +213 -0
- package/dist/cjs/auth.js +813 -0
- package/dist/cjs/cache-store.d.ts +50 -0
- package/dist/cjs/cache-store.js +194 -0
- package/dist/cjs/conflict.d.ts +80 -0
- package/dist/cjs/conflict.js +84 -0
- package/dist/cjs/database-errors.d.ts +101 -0
- package/dist/cjs/database-errors.js +200 -0
- package/dist/cjs/database.d.ts +298 -0
- package/dist/cjs/database.js +852 -0
- package/dist/cjs/device-id.d.ts +1 -0
- package/dist/cjs/device-id.js +57 -0
- package/dist/cjs/device-metadata.d.ts +36 -0
- package/dist/cjs/device-metadata.js +94 -0
- package/dist/cjs/errors.d.ts +64 -0
- package/dist/cjs/errors.js +85 -0
- package/dist/cjs/flags.d.ts +15 -0
- package/dist/cjs/flags.js +76 -0
- package/dist/cjs/function-errors.d.ts +51 -0
- package/dist/cjs/function-errors.js +103 -0
- package/dist/cjs/functions.d.ts +15 -0
- package/dist/cjs/functions.js +83 -0
- package/dist/cjs/index.d.ts +22 -0
- package/dist/cjs/index.js +56 -0
- package/dist/cjs/messaging.d.ts +13 -0
- package/dist/cjs/messaging.js +36 -0
- package/dist/cjs/offline-state.d.ts +97 -0
- package/dist/cjs/offline-state.js +198 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/pending-write.d.ts +47 -0
- package/dist/cjs/pending-write.js +22 -0
- package/dist/cjs/platform.d.ts +51 -0
- package/dist/cjs/platform.js +41 -0
- package/dist/cjs/realtime.d.ts +44 -0
- package/dist/cjs/realtime.js +195 -0
- package/dist/cjs/record.d.ts +2 -0
- package/dist/cjs/record.js +23 -0
- package/dist/cjs/shared.d.ts +9 -0
- package/dist/cjs/shared.js +43 -0
- package/dist/cjs/storage-errors.d.ts +163 -0
- package/dist/cjs/storage-errors.js +253 -0
- package/dist/cjs/storage.d.ts +198 -0
- package/dist/cjs/storage.js +451 -0
- package/dist/cjs/sync-engine.d.ts +30 -0
- package/dist/cjs/sync-engine.js +286 -0
- package/dist/cjs/types.d.ts +487 -0
- package/dist/cjs/types.js +40 -0
- package/dist/esm/analytics.d.ts +24 -0
- package/dist/esm/analytics.js +106 -0
- package/dist/esm/auth-errors.d.ts +117 -0
- package/dist/esm/auth-errors.js +222 -0
- package/dist/esm/auth.d.ts +213 -0
- package/dist/esm/auth.js +809 -0
- package/dist/esm/cache-store.d.ts +50 -0
- package/dist/esm/cache-store.js +179 -0
- package/dist/esm/conflict.d.ts +80 -0
- package/dist/esm/conflict.js +80 -0
- package/dist/esm/database-errors.d.ts +101 -0
- package/dist/esm/database-errors.js +189 -0
- package/dist/esm/database.d.ts +298 -0
- package/dist/esm/database.js +848 -0
- package/dist/esm/device-id.d.ts +1 -0
- package/dist/esm/device-id.js +54 -0
- package/dist/esm/device-metadata.d.ts +36 -0
- package/dist/esm/device-metadata.js +90 -0
- package/dist/esm/errors.d.ts +64 -0
- package/dist/esm/errors.js +79 -0
- package/dist/esm/flags.d.ts +15 -0
- package/dist/esm/flags.js +72 -0
- package/dist/esm/function-errors.d.ts +51 -0
- package/dist/esm/function-errors.js +93 -0
- package/dist/esm/functions.d.ts +15 -0
- package/dist/esm/functions.js +79 -0
- package/dist/esm/index.d.ts +22 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/messaging.d.ts +13 -0
- package/dist/esm/messaging.js +32 -0
- package/dist/esm/offline-state.d.ts +97 -0
- package/dist/esm/offline-state.js +189 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/pending-write.d.ts +47 -0
- package/dist/esm/pending-write.js +19 -0
- package/dist/esm/platform.d.ts +51 -0
- package/dist/esm/platform.js +36 -0
- package/dist/esm/realtime.d.ts +44 -0
- package/dist/esm/realtime.js +191 -0
- package/dist/esm/record.d.ts +2 -0
- package/dist/esm/record.js +20 -0
- package/dist/esm/shared.d.ts +9 -0
- package/dist/esm/shared.js +39 -0
- package/dist/esm/storage-errors.d.ts +163 -0
- package/dist/esm/storage-errors.js +239 -0
- package/dist/esm/storage.d.ts +198 -0
- package/dist/esm/storage.js +447 -0
- package/dist/esm/sync-engine.d.ts +30 -0
- package/dist/esm/sync-engine.js +282 -0
- package/dist/esm/types.d.ts +487 -0
- package/dist/esm/types.js +37 -0
- package/package.json +45 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { KoolbaseError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Base error type for all Koolbase auth errors. Catchable via
|
|
4
|
+
* `instanceof KoolbaseAuthError` to handle any auth-related failure
|
|
5
|
+
* generically; subclasses let you handle specific cases.
|
|
6
|
+
*/
|
|
7
|
+
export declare class KoolbaseAuthError extends KoolbaseError {
|
|
8
|
+
constructor(message: string, code?: string);
|
|
9
|
+
}
|
|
10
|
+
export declare class InvalidCredentialsError extends KoolbaseAuthError {
|
|
11
|
+
constructor();
|
|
12
|
+
}
|
|
13
|
+
export declare class EmailAlreadyInUseError extends KoolbaseAuthError {
|
|
14
|
+
constructor();
|
|
15
|
+
}
|
|
16
|
+
export declare class UserDisabledError extends KoolbaseAuthError {
|
|
17
|
+
constructor();
|
|
18
|
+
}
|
|
19
|
+
export declare class WeakPasswordError extends KoolbaseAuthError {
|
|
20
|
+
constructor();
|
|
21
|
+
}
|
|
22
|
+
export declare class SessionExpiredError extends KoolbaseAuthError {
|
|
23
|
+
constructor();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Thrown when the access token references a session that has been revoked
|
|
27
|
+
* centrally — either by the user (sessions endpoint) or an administrator.
|
|
28
|
+
* Distinct from {@link SessionExpiredError} which indicates the access
|
|
29
|
+
* token TTL elapsed without a successful refresh.
|
|
30
|
+
*
|
|
31
|
+
* Forward-compatible: matches multiple server message patterns so it stays
|
|
32
|
+
* accurate as the server's revocation signaling evolves.
|
|
33
|
+
*/
|
|
34
|
+
export declare class TokenRevokedError extends KoolbaseAuthError {
|
|
35
|
+
constructor();
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Thrown when the account is temporarily locked due to too many failed
|
|
39
|
+
* login attempts. The server uses progressive 5/10/20-attempt lockouts;
|
|
40
|
+
* if an unlock email was issued (level 2+), the user can clear the lock
|
|
41
|
+
* by passing that token to {@link KoolbaseAuth.unlock}.
|
|
42
|
+
*
|
|
43
|
+
* [lockedUntil] is currently null — the server returns a generic 429 but
|
|
44
|
+
* does not yet include the unlock timestamp in the response body. Field
|
|
45
|
+
* is forward-compatible for when the server adds it.
|
|
46
|
+
*/
|
|
47
|
+
export declare class AccountLockedError extends KoolbaseAuthError {
|
|
48
|
+
lockedUntil?: Date;
|
|
49
|
+
constructor(lockedUntil?: Date);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Thrown when the unlock token from a brute-force unlock email is
|
|
53
|
+
* invalid, expired, or already consumed. Unlock tokens are one-shot.
|
|
54
|
+
*/
|
|
55
|
+
export declare class UnlockTokenInvalidError extends KoolbaseAuthError {
|
|
56
|
+
constructor();
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Thrown when the server rate-limits a non-phone authentication endpoint
|
|
60
|
+
* (HTTP 429 without the "account temporarily locked" marker). Phone OTP
|
|
61
|
+
* endpoints throw {@link OtpRateLimitError} instead — they hit a
|
|
62
|
+
* separate server-side rate-limiter.
|
|
63
|
+
*/
|
|
64
|
+
export declare class RateLimitError extends KoolbaseAuthError {
|
|
65
|
+
constructor(message?: string);
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Generic network error. The SDK does NOT throw this directly — fetch
|
|
69
|
+
* failures (DNS, no connection, timeout) propagate as native TypeErrors.
|
|
70
|
+
* This class exists for consumer code that wants to construct or
|
|
71
|
+
* `instanceof`-check a typed network error from their own retry logic.
|
|
72
|
+
*/
|
|
73
|
+
export declare class NetworkError extends KoolbaseAuthError {
|
|
74
|
+
constructor();
|
|
75
|
+
}
|
|
76
|
+
export declare class InvalidPhoneNumberError extends KoolbaseAuthError {
|
|
77
|
+
constructor();
|
|
78
|
+
}
|
|
79
|
+
export declare class OtpExpiredError extends KoolbaseAuthError {
|
|
80
|
+
constructor();
|
|
81
|
+
}
|
|
82
|
+
export declare class OtpInvalidError extends KoolbaseAuthError {
|
|
83
|
+
constructor();
|
|
84
|
+
}
|
|
85
|
+
export declare class OtpMaxAttemptsError extends KoolbaseAuthError {
|
|
86
|
+
constructor();
|
|
87
|
+
}
|
|
88
|
+
export declare class OtpRateLimitError extends KoolbaseAuthError {
|
|
89
|
+
constructor();
|
|
90
|
+
}
|
|
91
|
+
export declare class PhoneAlreadyLinkedError extends KoolbaseAuthError {
|
|
92
|
+
constructor();
|
|
93
|
+
}
|
|
94
|
+
export declare class SmsConfigMissingError extends KoolbaseAuthError {
|
|
95
|
+
constructor();
|
|
96
|
+
}
|
|
97
|
+
export declare class AppleSignInNotConfiguredError extends KoolbaseAuthError {
|
|
98
|
+
constructor();
|
|
99
|
+
}
|
|
100
|
+
export declare class InvalidAppleTokenError extends KoolbaseAuthError {
|
|
101
|
+
constructor();
|
|
102
|
+
}
|
|
103
|
+
export declare class AppleEmailRequiredError extends KoolbaseAuthError {
|
|
104
|
+
constructor();
|
|
105
|
+
}
|
|
106
|
+
export declare class OAuthEmailConflictError extends KoolbaseAuthError {
|
|
107
|
+
constructor();
|
|
108
|
+
}
|
|
109
|
+
export declare class GoogleSignInNotConfiguredError extends KoolbaseAuthError {
|
|
110
|
+
constructor();
|
|
111
|
+
}
|
|
112
|
+
export declare class InvalidGoogleTokenError extends KoolbaseAuthError {
|
|
113
|
+
constructor();
|
|
114
|
+
}
|
|
115
|
+
export declare class GoogleEmailRequiredError extends KoolbaseAuthError {
|
|
116
|
+
constructor();
|
|
117
|
+
}
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { KoolbaseError } from './errors.js';
|
|
2
|
+
/**
|
|
3
|
+
* Base error type for all Koolbase auth errors. Catchable via
|
|
4
|
+
* `instanceof KoolbaseAuthError` to handle any auth-related failure
|
|
5
|
+
* generically; subclasses let you handle specific cases.
|
|
6
|
+
*/
|
|
7
|
+
export class KoolbaseAuthError extends KoolbaseError {
|
|
8
|
+
constructor(message, code) {
|
|
9
|
+
super(message, code);
|
|
10
|
+
this.name = 'KoolbaseAuthError';
|
|
11
|
+
Object.setPrototypeOf(this, KoolbaseAuthError.prototype);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
// ─── Credentials / Registration ────────────────────────────────────────────
|
|
15
|
+
export class InvalidCredentialsError extends KoolbaseAuthError {
|
|
16
|
+
constructor() {
|
|
17
|
+
super('Invalid email or password', 'invalid_credentials');
|
|
18
|
+
this.name = 'InvalidCredentialsError';
|
|
19
|
+
Object.setPrototypeOf(this, InvalidCredentialsError.prototype);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
export class EmailAlreadyInUseError extends KoolbaseAuthError {
|
|
23
|
+
constructor() {
|
|
24
|
+
super('Email is already in use', 'email_taken');
|
|
25
|
+
this.name = 'EmailAlreadyInUseError';
|
|
26
|
+
Object.setPrototypeOf(this, EmailAlreadyInUseError.prototype);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export class UserDisabledError extends KoolbaseAuthError {
|
|
30
|
+
constructor() {
|
|
31
|
+
super('This account has been disabled', 'user_disabled');
|
|
32
|
+
this.name = 'UserDisabledError';
|
|
33
|
+
Object.setPrototypeOf(this, UserDisabledError.prototype);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
export class WeakPasswordError extends KoolbaseAuthError {
|
|
37
|
+
constructor() {
|
|
38
|
+
super('Password must be at least 8 characters', 'weak_password');
|
|
39
|
+
this.name = 'WeakPasswordError';
|
|
40
|
+
Object.setPrototypeOf(this, WeakPasswordError.prototype);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// ─── Session lifecycle ─────────────────────────────────────────────────────
|
|
44
|
+
export class SessionExpiredError extends KoolbaseAuthError {
|
|
45
|
+
constructor() {
|
|
46
|
+
super('Session expired, please log in again', 'session_expired');
|
|
47
|
+
this.name = 'SessionExpiredError';
|
|
48
|
+
Object.setPrototypeOf(this, SessionExpiredError.prototype);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Thrown when the access token references a session that has been revoked
|
|
53
|
+
* centrally — either by the user (sessions endpoint) or an administrator.
|
|
54
|
+
* Distinct from {@link SessionExpiredError} which indicates the access
|
|
55
|
+
* token TTL elapsed without a successful refresh.
|
|
56
|
+
*
|
|
57
|
+
* Forward-compatible: matches multiple server message patterns so it stays
|
|
58
|
+
* accurate as the server's revocation signaling evolves.
|
|
59
|
+
*/
|
|
60
|
+
export class TokenRevokedError extends KoolbaseAuthError {
|
|
61
|
+
constructor() {
|
|
62
|
+
super('Session has been revoked, please log in again', 'token_revoked');
|
|
63
|
+
this.name = 'TokenRevokedError';
|
|
64
|
+
Object.setPrototypeOf(this, TokenRevokedError.prototype);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// ─── Brute-force protection ────────────────────────────────────────────────
|
|
68
|
+
/**
|
|
69
|
+
* Thrown when the account is temporarily locked due to too many failed
|
|
70
|
+
* login attempts. The server uses progressive 5/10/20-attempt lockouts;
|
|
71
|
+
* if an unlock email was issued (level 2+), the user can clear the lock
|
|
72
|
+
* by passing that token to {@link KoolbaseAuth.unlock}.
|
|
73
|
+
*
|
|
74
|
+
* [lockedUntil] is currently null — the server returns a generic 429 but
|
|
75
|
+
* does not yet include the unlock timestamp in the response body. Field
|
|
76
|
+
* is forward-compatible for when the server adds it.
|
|
77
|
+
*/
|
|
78
|
+
export class AccountLockedError extends KoolbaseAuthError {
|
|
79
|
+
constructor(lockedUntil) {
|
|
80
|
+
super('Account temporarily locked due to too many failed attempts', 'account_locked');
|
|
81
|
+
this.lockedUntil = lockedUntil;
|
|
82
|
+
this.name = 'AccountLockedError';
|
|
83
|
+
Object.setPrototypeOf(this, AccountLockedError.prototype);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Thrown when the unlock token from a brute-force unlock email is
|
|
88
|
+
* invalid, expired, or already consumed. Unlock tokens are one-shot.
|
|
89
|
+
*/
|
|
90
|
+
export class UnlockTokenInvalidError extends KoolbaseAuthError {
|
|
91
|
+
constructor() {
|
|
92
|
+
super('Unlock link is invalid or has expired', 'unlock_token_invalid');
|
|
93
|
+
this.name = 'UnlockTokenInvalidError';
|
|
94
|
+
Object.setPrototypeOf(this, UnlockTokenInvalidError.prototype);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Thrown when the server rate-limits a non-phone authentication endpoint
|
|
99
|
+
* (HTTP 429 without the "account temporarily locked" marker). Phone OTP
|
|
100
|
+
* endpoints throw {@link OtpRateLimitError} instead — they hit a
|
|
101
|
+
* separate server-side rate-limiter.
|
|
102
|
+
*/
|
|
103
|
+
export class RateLimitError extends KoolbaseAuthError {
|
|
104
|
+
constructor(message) {
|
|
105
|
+
super(message ?? 'Too many requests, please wait before trying again', 'rate_limit');
|
|
106
|
+
this.name = 'RateLimitError';
|
|
107
|
+
Object.setPrototypeOf(this, RateLimitError.prototype);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// ─── Network ───────────────────────────────────────────────────────────────
|
|
111
|
+
/**
|
|
112
|
+
* Generic network error. The SDK does NOT throw this directly — fetch
|
|
113
|
+
* failures (DNS, no connection, timeout) propagate as native TypeErrors.
|
|
114
|
+
* This class exists for consumer code that wants to construct or
|
|
115
|
+
* `instanceof`-check a typed network error from their own retry logic.
|
|
116
|
+
*/
|
|
117
|
+
export class NetworkError extends KoolbaseAuthError {
|
|
118
|
+
constructor() {
|
|
119
|
+
super('Network error, please check your connection', 'network_error');
|
|
120
|
+
this.name = 'NetworkError';
|
|
121
|
+
Object.setPrototypeOf(this, NetworkError.prototype);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// ─── Phone OTP (unchanged from earlier releases) ───────────────────────────
|
|
125
|
+
export class InvalidPhoneNumberError extends KoolbaseAuthError {
|
|
126
|
+
constructor() {
|
|
127
|
+
super('Phone number must be in E.164 format (e.g. +233XXXXXXXXX)', 'invalid_phone');
|
|
128
|
+
this.name = 'InvalidPhoneNumberError';
|
|
129
|
+
Object.setPrototypeOf(this, InvalidPhoneNumberError.prototype);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
export class OtpExpiredError extends KoolbaseAuthError {
|
|
133
|
+
constructor() {
|
|
134
|
+
super('OTP has expired, please request a new code', 'otp_expired');
|
|
135
|
+
this.name = 'OtpExpiredError';
|
|
136
|
+
Object.setPrototypeOf(this, OtpExpiredError.prototype);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
export class OtpInvalidError extends KoolbaseAuthError {
|
|
140
|
+
constructor() {
|
|
141
|
+
super('Invalid OTP code', 'otp_invalid');
|
|
142
|
+
this.name = 'OtpInvalidError';
|
|
143
|
+
Object.setPrototypeOf(this, OtpInvalidError.prototype);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
export class OtpMaxAttemptsError extends KoolbaseAuthError {
|
|
147
|
+
constructor() {
|
|
148
|
+
super('Too many incorrect attempts, please request a new code', 'otp_max_attempts');
|
|
149
|
+
this.name = 'OtpMaxAttemptsError';
|
|
150
|
+
Object.setPrototypeOf(this, OtpMaxAttemptsError.prototype);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
export class OtpRateLimitError extends KoolbaseAuthError {
|
|
154
|
+
constructor() {
|
|
155
|
+
super('Too many OTP requests, please wait before trying again', 'otp_rate_limit');
|
|
156
|
+
this.name = 'OtpRateLimitError';
|
|
157
|
+
Object.setPrototypeOf(this, OtpRateLimitError.prototype);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
export class PhoneAlreadyLinkedError extends KoolbaseAuthError {
|
|
161
|
+
constructor() {
|
|
162
|
+
super('Phone number is already associated with another account', 'phone_taken');
|
|
163
|
+
this.name = 'PhoneAlreadyLinkedError';
|
|
164
|
+
Object.setPrototypeOf(this, PhoneAlreadyLinkedError.prototype);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
export class SmsConfigMissingError extends KoolbaseAuthError {
|
|
168
|
+
constructor() {
|
|
169
|
+
super('SMS provider not configured for this project', 'sms_config_missing');
|
|
170
|
+
this.name = 'SmsConfigMissingError';
|
|
171
|
+
Object.setPrototypeOf(this, SmsConfigMissingError.prototype);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
export class AppleSignInNotConfiguredError extends KoolbaseAuthError {
|
|
175
|
+
constructor() {
|
|
176
|
+
super('Apple Sign-In is not configured for this environment', 'apple_not_configured');
|
|
177
|
+
this.name = 'AppleSignInNotConfiguredError';
|
|
178
|
+
Object.setPrototypeOf(this, AppleSignInNotConfiguredError.prototype);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
export class InvalidAppleTokenError extends KoolbaseAuthError {
|
|
182
|
+
constructor() {
|
|
183
|
+
super('Invalid Apple identity token', 'invalid_apple_token');
|
|
184
|
+
this.name = 'InvalidAppleTokenError';
|
|
185
|
+
Object.setPrototypeOf(this, InvalidAppleTokenError.prototype);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
export class AppleEmailRequiredError extends KoolbaseAuthError {
|
|
189
|
+
constructor() {
|
|
190
|
+
super('Apple did not return email for this sign-in. Revoke this app in iOS Settings → Apple ID and retry.', 'apple_email_required');
|
|
191
|
+
this.name = 'AppleEmailRequiredError';
|
|
192
|
+
Object.setPrototypeOf(this, AppleEmailRequiredError.prototype);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
export class OAuthEmailConflictError extends KoolbaseAuthError {
|
|
196
|
+
constructor() {
|
|
197
|
+
super('Email is already in use by another account. Sign in with your existing method and link Apple from settings.', 'oauth_email_conflict');
|
|
198
|
+
this.name = 'OAuthEmailConflictError';
|
|
199
|
+
Object.setPrototypeOf(this, OAuthEmailConflictError.prototype);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
export class GoogleSignInNotConfiguredError extends KoolbaseAuthError {
|
|
203
|
+
constructor() {
|
|
204
|
+
super('Google Sign-In is not configured for this environment', 'google_not_configured');
|
|
205
|
+
this.name = 'GoogleSignInNotConfiguredError';
|
|
206
|
+
Object.setPrototypeOf(this, GoogleSignInNotConfiguredError.prototype);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
export class InvalidGoogleTokenError extends KoolbaseAuthError {
|
|
210
|
+
constructor() {
|
|
211
|
+
super('Invalid Google identity token', 'invalid_google_token');
|
|
212
|
+
this.name = 'InvalidGoogleTokenError';
|
|
213
|
+
Object.setPrototypeOf(this, InvalidGoogleTokenError.prototype);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
export class GoogleEmailRequiredError extends KoolbaseAuthError {
|
|
217
|
+
constructor() {
|
|
218
|
+
super('Google did not return email for this sign-in. Ensure the email scope is requested in the native flow.', 'google_email_required');
|
|
219
|
+
this.name = 'GoogleEmailRequiredError';
|
|
220
|
+
Object.setPrototypeOf(this, GoogleEmailRequiredError.prototype);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { AuthStateListener, KoolbaseConfig, KoolbaseSession, KoolbaseUser, LinkPhoneParams, LoginParams, OtpSendResult, PhoneVerifyResult, RegisterParams, RestoreResult, SendOtpParams, SignInWithAppleParams, VerifyOtpParams } from './types.js';
|
|
2
|
+
import type { SignInWithGoogleParams } from './types.js';
|
|
3
|
+
export declare class KoolbaseAuth {
|
|
4
|
+
private config;
|
|
5
|
+
private storage;
|
|
6
|
+
private session;
|
|
7
|
+
private metadata;
|
|
8
|
+
private fetchFn;
|
|
9
|
+
private timeoutMs;
|
|
10
|
+
private ongoingRefresh;
|
|
11
|
+
private listeners;
|
|
12
|
+
constructor(config: KoolbaseConfig);
|
|
13
|
+
/**
|
|
14
|
+
* Subscribe to authentication state changes. The listener fires:
|
|
15
|
+
* - Immediately on subscribe, with the current user (or null).
|
|
16
|
+
* - On every successful login, register, refresh, session restoration.
|
|
17
|
+
* - On logout / explicit setSession(null).
|
|
18
|
+
* - On linkPhone success (user object updated with phone fields).
|
|
19
|
+
*
|
|
20
|
+
* Returns an unsubscribe function. Call it when the consumer no longer
|
|
21
|
+
* needs updates (e.g. in a React useEffect cleanup).
|
|
22
|
+
*
|
|
23
|
+
* Listener errors are swallowed so a buggy listener can't break auth
|
|
24
|
+
* state propagation to other listeners.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* const unsubscribe = auth.onAuthStateChange((user) => {
|
|
28
|
+
* setCurrentUser(user);
|
|
29
|
+
* });
|
|
30
|
+
* // later:
|
|
31
|
+
* unsubscribe();
|
|
32
|
+
*/
|
|
33
|
+
onAuthStateChange(listener: AuthStateListener): () => void;
|
|
34
|
+
private fireAuthStateChange;
|
|
35
|
+
/**
|
|
36
|
+
* Compose the full header set for an outbound request: base headers,
|
|
37
|
+
* device metadata, and optionally the Authorization bearer token.
|
|
38
|
+
* Async because device metadata's first build may read from keychain.
|
|
39
|
+
*/
|
|
40
|
+
private prepareHeaders;
|
|
41
|
+
/**
|
|
42
|
+
* Low-level request helper used by every endpoint. Wires together:
|
|
43
|
+
* - The injected fetch implementation (config.fetch or global fetch)
|
|
44
|
+
* - Device metadata + x-api-key + auth header in one place
|
|
45
|
+
* - AbortController-based timeout (config.authTimeout, default 10s)
|
|
46
|
+
*
|
|
47
|
+
* On timeout, fetch rejects with an AbortError; callers see this as a
|
|
48
|
+
* non-KoolbaseAuthError exception, which restoreSession() treats as
|
|
49
|
+
* Offline (preserving optimistic state).
|
|
50
|
+
*/
|
|
51
|
+
private authRequest;
|
|
52
|
+
/**
|
|
53
|
+
* Authenticated request wrapper. Refreshes the access token if it's
|
|
54
|
+
* stale (within 1-min buffer of expiry) before issuing the call, then
|
|
55
|
+
* delegates to {@link authRequest} with includeAuth=true.
|
|
56
|
+
*/
|
|
57
|
+
private authedRequest;
|
|
58
|
+
private setSessionInternal;
|
|
59
|
+
/**
|
|
60
|
+
* Discards the stored session without contacting the server.
|
|
61
|
+
*
|
|
62
|
+
* For when the session is already known to be unusable — the server rejected
|
|
63
|
+
* the token, or a build was pointed at a different project and the persisted
|
|
64
|
+
* session belongs to the old one. Unlike `logout()` there is no server call:
|
|
65
|
+
* the token has already been refused, and asking for it to be revoked would
|
|
66
|
+
* only add a round trip that cannot succeed.
|
|
67
|
+
*
|
|
68
|
+
* Safe in any state, including with no session at all. The SDK calls this
|
|
69
|
+
* itself when a request is rejected as unauthenticated, so most apps will not
|
|
70
|
+
* need to.
|
|
71
|
+
*/
|
|
72
|
+
clearStoredSession(): Promise<void>;
|
|
73
|
+
private clearSessionInternal;
|
|
74
|
+
restoreSession(): Promise<RestoreResult>;
|
|
75
|
+
register(params: RegisterParams): Promise<KoolbaseUser>;
|
|
76
|
+
login(params: LoginParams): Promise<KoolbaseSession>;
|
|
77
|
+
/**
|
|
78
|
+
* Sign in with Apple using a credential obtained from a native Apple
|
|
79
|
+
* Sign-In SDK.
|
|
80
|
+
*
|
|
81
|
+
* The SDK is library-agnostic — use any native Apple Sign-In package
|
|
82
|
+
* (`@invertase/react-native-apple-authentication`, etc.) and pass the
|
|
83
|
+
* resulting `identityToken`, optional `nonce`, and optional `fullName`.
|
|
84
|
+
*
|
|
85
|
+
* `fullName` is meaningful only on first sign-in — Apple omits name
|
|
86
|
+
* data on subsequent sign-ins. The server persists at link time and
|
|
87
|
+
* ignores on subsequent sign-ins.
|
|
88
|
+
*
|
|
89
|
+
* On success the session is persisted via the configured storage and
|
|
90
|
+
* `onAuthStateChange` fires with the resolved user.
|
|
91
|
+
*
|
|
92
|
+
* @throws AppleSignInNotConfiguredError when Apple is not enabled in
|
|
93
|
+
* the dashboard OAuth config for this environment (400).
|
|
94
|
+
* @throws InvalidAppleTokenError when the token signature, audience,
|
|
95
|
+
* expiry, replay, or nonce check failed server-side (401).
|
|
96
|
+
* @throws UserDisabledError when the account flag is set to disabled (403).
|
|
97
|
+
* @throws AppleEmailRequiredError when Apple did not return email for
|
|
98
|
+
* a new-account sign-in (400).
|
|
99
|
+
* @throws OAuthEmailConflictError when email matches existing user
|
|
100
|
+
* but auto-link rule blocked (409).
|
|
101
|
+
*/
|
|
102
|
+
signInWithApple(params: SignInWithAppleParams): Promise<KoolbaseSession>;
|
|
103
|
+
/**
|
|
104
|
+
* Sign in with Google using an idToken from a native Google Sign-In SDK.
|
|
105
|
+
*
|
|
106
|
+
* The SDK is library-agnostic — use any native Google Sign-In package
|
|
107
|
+
* (`@react-native-google-signin/google-signin`, etc.) and pass the
|
|
108
|
+
* resulting `idToken`. Google embeds the user's name and email in the
|
|
109
|
+
* idToken itself, so this method does not take a `fullName` parameter
|
|
110
|
+
* (unlike `signInWithApple`).
|
|
111
|
+
*
|
|
112
|
+
* On success the session is persisted via the configured storage and
|
|
113
|
+
* `onAuthStateChange` fires with the resolved user.
|
|
114
|
+
*
|
|
115
|
+
* @throws GoogleSignInNotConfiguredError when Google is not enabled
|
|
116
|
+
* in the OAuth config for this environment (400).
|
|
117
|
+
* @throws InvalidGoogleTokenError when the token signature, audience,
|
|
118
|
+
* expiry, replay, or nonce check failed server-side (401).
|
|
119
|
+
* @throws UserDisabledError when the account flag is set to disabled (403).
|
|
120
|
+
* @throws GoogleEmailRequiredError when Google did not return email
|
|
121
|
+
* for a new-account sign-in (400).
|
|
122
|
+
* @throws OAuthEmailConflictError when email matches existing user
|
|
123
|
+
* but auto-link rule blocked (409).
|
|
124
|
+
*/
|
|
125
|
+
signInWithGoogle(params: SignInWithGoogleParams): Promise<KoolbaseSession>;
|
|
126
|
+
/**
|
|
127
|
+
* Parses a /v1/sdk/auth/oauth/google response. Code-first: the server
|
|
128
|
+
* emits unified OAuth codes (oauth_not_configured, invalid_oauth_token,
|
|
129
|
+
* oauth_email_required, oauth_email_conflict) for both providers; the
|
|
130
|
+
* provider distinction is made here so Google codes map to Google-specific
|
|
131
|
+
* errors. Status + message logic is retained as a fallback for older servers.
|
|
132
|
+
*/
|
|
133
|
+
private parseGoogleSessionResponse;
|
|
134
|
+
/**
|
|
135
|
+
* Parses a /v1/sdk/auth/oauth/apple response. Code-first; the provider
|
|
136
|
+
* distinction is made here so the server's unified OAuth codes map to
|
|
137
|
+
* Apple-specific errors. Status + message logic is retained as a fallback
|
|
138
|
+
* for older servers.
|
|
139
|
+
*/
|
|
140
|
+
private parseAppleSessionResponse;
|
|
141
|
+
refresh(refreshToken?: string): Promise<KoolbaseSession>;
|
|
142
|
+
private _doRefresh;
|
|
143
|
+
logout(): Promise<boolean>;
|
|
144
|
+
forgotPassword(email: string): Promise<void>;
|
|
145
|
+
resetPassword(token: string, password: string): Promise<void>;
|
|
146
|
+
unlock(token: string): Promise<void>;
|
|
147
|
+
get currentUser(): KoolbaseUser | null;
|
|
148
|
+
get accessToken(): string | null;
|
|
149
|
+
/**
|
|
150
|
+
* Currently-valid access token for data-plane requests, refreshing
|
|
151
|
+
* (via refresh()) if the cached one is near expiry. Returns null when no
|
|
152
|
+
* session exists or refresh fails — callers then go api-key-only and the
|
|
153
|
+
* server treats it as having no end-user identity. The db/storage/functions
|
|
154
|
+
* clients pull from this per request so identity follows the live session.
|
|
155
|
+
*/
|
|
156
|
+
validAccessToken(): Promise<string | null>;
|
|
157
|
+
setSession(session: KoolbaseSession | null): Promise<void>;
|
|
158
|
+
/**
|
|
159
|
+
* @deprecated v1.9.0: Server endpoint /v1/sdk/auth/oauth not yet
|
|
160
|
+
* shipped. This method previously routed to /v1/auth/oauth (dashboard
|
|
161
|
+
* developer OAuth) which never created project-scoped end-user
|
|
162
|
+
* sessions. Properly implemented in v1.10.0 with provider-specific
|
|
163
|
+
* server endpoints under /v1/sdk/auth/oauth/{apple,google,github}.
|
|
164
|
+
* Use email/password sign-in for now.
|
|
165
|
+
*
|
|
166
|
+
* @throws Always throws KoolbaseAuthError('not_implemented').
|
|
167
|
+
*/
|
|
168
|
+
oauthLogin(_params: {
|
|
169
|
+
provider: string;
|
|
170
|
+
token: string;
|
|
171
|
+
email?: string;
|
|
172
|
+
name?: string;
|
|
173
|
+
avatarUrl?: string;
|
|
174
|
+
}): Promise<never>;
|
|
175
|
+
sendOtp(params: SendOtpParams): Promise<OtpSendResult>;
|
|
176
|
+
verifyOtp(params: VerifyOtpParams): Promise<PhoneVerifyResult>;
|
|
177
|
+
linkPhone(params: LinkPhoneParams): Promise<void>;
|
|
178
|
+
/**
|
|
179
|
+
* Release resources held by this auth client. Clears the in-memory
|
|
180
|
+
* listener set. Does not invalidate sessions or clear storage — call
|
|
181
|
+
* {@link logout} for that.
|
|
182
|
+
*/
|
|
183
|
+
dispose(): void;
|
|
184
|
+
private validatePhone;
|
|
185
|
+
private _ensureValidToken;
|
|
186
|
+
private mapUser;
|
|
187
|
+
/**
|
|
188
|
+
* Parse a session-returning response (login, register, refresh).
|
|
189
|
+
* Non-2xx is delegated to throwTypedError, which is code-first
|
|
190
|
+
* (reads body.code) with a status/message fallback. isRefresh only
|
|
191
|
+
* affects how a bare 401 (no code, older server) is interpreted.
|
|
192
|
+
*/
|
|
193
|
+
private parseSessionResponse;
|
|
194
|
+
private checkResponse;
|
|
195
|
+
/**
|
|
196
|
+
* Map a non-2xx credential/session response to a typed error.
|
|
197
|
+
*
|
|
198
|
+
* Code-first: the server now emits a stable `code` on every error
|
|
199
|
+
* (contract conformance), so we switch on body.code. The status +
|
|
200
|
+
* message logic is retained as a fallback for older servers or any
|
|
201
|
+
* response that arrives without a code. isRefresh only changes how a
|
|
202
|
+
* bare 401 is interpreted in the fallback path.
|
|
203
|
+
*/
|
|
204
|
+
private throwTypedError;
|
|
205
|
+
/**
|
|
206
|
+
* Parse a phone-auth response. Code-first, with a phone-specific twist:
|
|
207
|
+
* the server emits the generic `rate_limit` code for the phone endpoints
|
|
208
|
+
* (they share the default 429), but phone has a dedicated server-side
|
|
209
|
+
* rate-limiter, so we surface OtpRateLimitError rather than RateLimitError.
|
|
210
|
+
* Status + message logic is retained as a fallback for older servers.
|
|
211
|
+
*/
|
|
212
|
+
private parsePhoneResponse;
|
|
213
|
+
}
|