@imtbl/auth 2.12.5 → 2.12.6-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +163 -0
- package/dist/browser/index.js +80 -28
- package/dist/node/index.cjs +99 -40
- package/dist/node/index.js +79 -27
- package/dist/types/index.d.ts +3 -1
- package/dist/types/login/standalone.d.ts +223 -0
- package/dist/types/logout/index.d.ts +27 -0
- package/dist/types/types.d.ts +32 -3
- package/package.json +6 -6
- package/src/Auth.test.ts +225 -0
- package/src/Auth.ts +31 -25
- package/src/index.ts +27 -0
- package/src/login/standalone.ts +906 -0
- package/src/logout/index.ts +52 -0
- package/src/types.ts +36 -2
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal logout utilities shared between Auth class and standalone functions.
|
|
3
|
+
* NOT exported from package index - only used internally.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const LOGOUT_ENDPOINT = '/v2/logout';
|
|
7
|
+
const CROSS_SDK_BRIDGE_LOGOUT_ENDPOINT = '/im-logged-out';
|
|
8
|
+
const DEFAULT_AUTH_DOMAIN = 'https://auth.immutable.com';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Internal config for building logout URLs.
|
|
12
|
+
* Includes crossSdkBridgeEnabled for Auth class (Passport SDK).
|
|
13
|
+
* NOT exported publicly from the package.
|
|
14
|
+
*/
|
|
15
|
+
export interface InternalLogoutConfig {
|
|
16
|
+
/** The client ID for the application */
|
|
17
|
+
clientId: string;
|
|
18
|
+
/** The authentication domain (defaults to https://auth.immutable.com) */
|
|
19
|
+
authenticationDomain?: string;
|
|
20
|
+
/** The URI to redirect to after logout */
|
|
21
|
+
logoutRedirectUri?: string;
|
|
22
|
+
/** Whether cross-SDK bridge is enabled (internal to Auth class) */
|
|
23
|
+
crossSdkBridgeEnabled?: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Normalize authentication domain to ensure https:// prefix.
|
|
28
|
+
*/
|
|
29
|
+
function normalizeAuthDomain(domain: string): string {
|
|
30
|
+
return domain.replace(/^(?:https?:\/\/)?(.*)/, 'https://$1');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Build logout URL for federated logout.
|
|
35
|
+
* Used by both Auth class (with crossSdkBridgeEnabled) and standalone functions.
|
|
36
|
+
*
|
|
37
|
+
* @param config - Configuration for building the logout URL
|
|
38
|
+
* @returns The complete logout URL string
|
|
39
|
+
*/
|
|
40
|
+
export function buildLogoutUrl(config: InternalLogoutConfig): string {
|
|
41
|
+
const authDomain = normalizeAuthDomain(config.authenticationDomain || DEFAULT_AUTH_DOMAIN);
|
|
42
|
+
const endpoint = config.crossSdkBridgeEnabled
|
|
43
|
+
? CROSS_SDK_BRIDGE_LOGOUT_ENDPOINT
|
|
44
|
+
: LOGOUT_ENDPOINT;
|
|
45
|
+
|
|
46
|
+
const url = new URL(endpoint, authDomain);
|
|
47
|
+
url.searchParams.set('client_id', config.clientId);
|
|
48
|
+
if (config.logoutRedirectUri) {
|
|
49
|
+
url.searchParams.set('returnTo', config.logoutRedirectUri);
|
|
50
|
+
}
|
|
51
|
+
return url.toString();
|
|
52
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -26,10 +26,12 @@ export enum EvmChain {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export type ChainAddress = {
|
|
29
|
-
ethAddress: string
|
|
30
|
-
userAdminAddress: string
|
|
29
|
+
ethAddress: `0x${string}`;
|
|
30
|
+
userAdminAddress: `0x${string}`;
|
|
31
31
|
};
|
|
32
32
|
|
|
33
|
+
export type ZkEvmInfo = ChainAddress;
|
|
34
|
+
|
|
33
35
|
export type User = {
|
|
34
36
|
idToken?: string;
|
|
35
37
|
accessToken: string;
|
|
@@ -163,12 +165,44 @@ export type LoginOptions = {
|
|
|
163
165
|
export enum AuthEvents {
|
|
164
166
|
LOGGED_OUT = 'loggedOut',
|
|
165
167
|
LOGGED_IN = 'loggedIn',
|
|
168
|
+
/**
|
|
169
|
+
* Emitted when tokens are refreshed via signinSilent().
|
|
170
|
+
* This is critical for refresh token rotation - when client-side refresh happens,
|
|
171
|
+
* the new tokens must be synced to server-side session to prevent race conditions.
|
|
172
|
+
*/
|
|
173
|
+
TOKEN_REFRESHED = 'tokenRefreshed',
|
|
174
|
+
/**
|
|
175
|
+
* Emitted when the user is removed from local storage due to a permanent auth error.
|
|
176
|
+
* Only emitted for errors where the refresh token is truly invalid:
|
|
177
|
+
* - invalid_grant: refresh token expired, revoked, or already used
|
|
178
|
+
* - login_required: user must re-authenticate
|
|
179
|
+
* - consent_required / interaction_required: user must interact with auth server
|
|
180
|
+
*
|
|
181
|
+
* NOT emitted for transient errors (network, timeout, server errors) - user stays logged in.
|
|
182
|
+
* Consumers should sync this state by clearing their session (e.g., NextAuth signOut).
|
|
183
|
+
*/
|
|
184
|
+
USER_REMOVED = 'userRemoved',
|
|
166
185
|
}
|
|
167
186
|
|
|
187
|
+
/**
|
|
188
|
+
* Error reason for USER_REMOVED event.
|
|
189
|
+
* Note: Network/timeout errors do NOT emit USER_REMOVED (user stays logged in),
|
|
190
|
+
* so 'network_error' is not a valid reason.
|
|
191
|
+
*/
|
|
192
|
+
export type UserRemovedReason =
|
|
193
|
+
// OAuth permanent errors (invalid_grant, login_required, etc.)
|
|
194
|
+
| 'refresh_token_invalid'
|
|
195
|
+
// Unknown non-OAuth errors
|
|
196
|
+
| 'refresh_failed'
|
|
197
|
+
// Fallback for truly unknown error types
|
|
198
|
+
| 'unknown';
|
|
199
|
+
|
|
168
200
|
/**
|
|
169
201
|
* Event map for typed event emitter
|
|
170
202
|
*/
|
|
171
203
|
export interface AuthEventMap extends Record<string, any> {
|
|
172
204
|
[AuthEvents.LOGGED_OUT]: [];
|
|
173
205
|
[AuthEvents.LOGGED_IN]: [User];
|
|
206
|
+
[AuthEvents.TOKEN_REFRESHED]: [User];
|
|
207
|
+
[AuthEvents.USER_REMOVED]: [{ reason: UserRemovedReason; error?: string }];
|
|
174
208
|
}
|