@privy-io/js-sdk-core 0.44.2 → 0.44.3-beta-20250221224248
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/Session.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("eventemitter3"),t=require("jose"),s=require("js-cookie"),r=require("./Token.js"),i=require("./utils/allSettled.js");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=/*#__PURE__*/o(e),a=/*#__PURE__*/o(s);let l="privy:token",_="privy-token",h="privy:refresh_token",
|
|
1
|
+
"use strict";var e=require("eventemitter3"),t=require("jose"),s=require("js-cookie"),r=require("./Token.js"),i=require("./utils/allSettled.js");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var n=/*#__PURE__*/o(e),a=/*#__PURE__*/o(s);let l="privy:token",_="privy-token",h="privy:refresh_token",d="privy-refresh-token",g="privy:id-token",u="privy-id-token",k="privy-session",c="privy:session_transfer_token";class f extends n.default{set isUsingServerCookies(e){this._isUsingServerCookies=e}async getToken(){let e=await this._storage.get(l);try{return"string"==typeof e?new r.Token(e).value:null}catch(e){return console.error(e),await this.destroyLocalState({reason:"getToken_error"}),null}}async getRefreshToken(){let e=await this._storage.get(h);return"string"==typeof e?e:null}async getIdentityToken(){let e=await this._storage.get(g);return"string"==typeof e?e:null}async getForkedToken(){let e=await this._storage.get(c);return"string"==typeof e?e:null}get mightHaveServerCookies(){try{let e=a.default.get(k);return void 0!==e&&e.length>0}catch(e){console.error(e)}return!1}hasRefreshCredentials(e,t){return this.mightHaveServerCookies||"string"==typeof e&&"string"==typeof t}async hasRecoveryCredentials(){return"string"==typeof await this.getForkedToken()}tokenIsActive(e){if(!e)return!1;let t=r.Token.parse(e);return null!==t&&!t.isExpired(30)}async destroyLocalState(e){await i.allSettled([this._storage.del(l),this._storage.del(h),this._storage.del(g),this._storage.del(this.GUEST_CREDENTIAL_STORAGE_KEY),this.clearForkedToken()]),a.default.remove(_),a.default.remove(d),a.default.remove(u),a.default.remove(k),e?.reason&&this.emit("storage_cleared",{reason:e.reason})}async storeToken(e){if("string"==typeof e){let t=await this._storage.get(l);if(await this._storage.put(l,e),!this._isUsingServerCookies){let t=r.Token.parse(e)?.expiration;a.default.set(_,e,{sameSite:"Strict",secure:!0,expires:t?new Date(1e3*t):void 0})}t!==e&&this.emit("token_stored",{cookiesEnabled:this._isUsingServerCookies})}else{let e=await this._storage.get(l);await this._storage.del(l),a.default.remove(_),null!==e&&this.emit("token_cleared",{reason:"set_with_non_string_value"})}}async storeRefreshToken(e){"string"==typeof e?(await this._storage.put(h,e),this._isUsingServerCookies||(a.default.set(k,"t",{sameSite:"Strict",secure:!0,expires:30}),a.default.set(d,e,{sameSite:"Strict",secure:!0,expires:30})),this.emit("refresh_token_stored",{cookiesEnabled:this._isUsingServerCookies})):(await this._storage.del(h),a.default.remove(d),a.default.remove(k),this.emit("refresh_token_cleared",{reason:"set_with_non_string_value"}))}async updateWithTokensResponse(e){let t=(await i.allSettled([this.storeToken(e.token),this.storeRefreshToken(e.refresh_token),this.storeIdentityToken(e.identity_token),this.processOAuthTokens(e.oauth_tokens)])).filter((e=>"rejected"===e.status));t.length>0&&this.emit("error_storing_tokens",t.map((e=>String(e.status))).join(", "))}async processOAuthTokens(e){e&&this.emit("oauth_tokens_granted",e)}async storeIdentityToken(e){if("string"==typeof e){let t=await this._storage.get(g);if(await this._storage.put(g,e),!this._isUsingServerCookies){let t=r.Token.parse(e)?.expiration;a.default.set(u,e,{sameSite:"Strict",secure:!0,expires:t?new Date(1e3*t):void 0})}t!==e&&this.emit("identity_token_stored",{cookiesEnabled:this._isUsingServerCookies})}else{let e=await this._storage.get(g);await this._storage.del(g),a.default.remove(u),null!==e&&this.emit("identity_token_cleared",{reason:"set_with_non_string_value"})}}async getOrCreateGuestCredential(){let e=this._storage.get(this.GUEST_CREDENTIAL_STORAGE_KEY);if(e&&"string"==typeof e)return e;let s=t.base64url.encode(crypto.getRandomValues(new Uint8Array(32)));return await this._storage.put(this.GUEST_CREDENTIAL_STORAGE_KEY,s),s}async clearForkedToken(){await this._storage.del(c)}constructor(e){super(),this._isUsingServerCookies=!1,this._storage=e.storage,this.GUEST_CREDENTIAL_STORAGE_KEY=`privy:guest:${e.appId}`}}f.events=["storage_cleared","token_cleared","refresh_token_cleared","identity_token_cleared","forked_token_cleared","token_stored","refresh_token_stored","identity_token_stored","oauth_tokens_granted","error_storing_tokens"],exports.Session=f;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var e=require("fetch-retry"),t=require("uuid"),s=require("@privy-io/api-base"),i=require("@privy-io/public-api"),r=require("../Error.js"),a=require("../Session.js"),n=require("../toAbortSignalTimeout.js"),o=require("../utils/toSearchParams.js");function h(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("eventemitter3"),require("jose"),require("js-cookie"),require("../Token.js"),require("../utils/allSettled.js");var l=/*#__PURE__*/h(e);let c="privy:caid";exports.PrivyInternal=class{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!this._config}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new r.PrivyClientError({code:"storage_error",error:"Unable to access storage"});this._config=await this.getAppConfig(),this._config?.custom_api_url&&(this.baseUrl=this._config.custom_api_url,this.session.isUsingServerCookies=!0),this.callbacks?.setIsReady?.(!0),this.createAnalyticsEvent("sdk_initialize",{})}}async fetch(e,{body:t,params:s,query:a,headers:n,options:h={onRequest:this._beforeRequest.bind(this)}}){let l=new Request(`${this.baseUrl}${i.getPathWithParams(e.path,s)}${o.toSearchParams(a)}`,{method:e.method,body:JSON.stringify(t),headers:n}),c=await h.onRequest(l),d=await this._fetch(l,c),u=await d.json();if(d.status>299)throw new r.PrivyApiError(u);return u}async _beforeRequestWithoutInitialize(e){let t=await this.session.getToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");let i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:n.default(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessToken(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(i.AppConfig,{params:{app_id:this.appId},options:{onRequest:this._beforeRequestWithoutInitialize.bind(this)}})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{let e=await this._storage.get(c);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){console.error("Unable to load clientId",e)}try{this._analyticsId=t.v4()}catch(e){console.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(c,this._analyticsId)}catch(e){console.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(c)}catch(e){console.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(i.AnalyticsEvent,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}})}catch(e){}}async refreshSession(e=!1){let t=await this.session.getRefreshToken()??void 0,s=t??"key",i=this._cache.get(s);if(i)return console.debug("Found in-flight session refresh request, deduping"),await i;let r=this._refreshSession(t,e);this._cache.set(s,r);try{return await r}finally{this._cache.delete(s)}}async _refreshSession(e,t){let a=await this.session.getToken();if(!this.session.hasRefreshCredentials(a,e??null))throw this.debug("[privy:refresh] missing tokens, skipping request"),await this._initialize(),new r.PrivyApiError({code:s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{this.debug(`[privy:refresh] fetching: ${i.RefreshSession.path}`);let s=await this.fetch(i.RefreshSession,{body:{refresh_token:e},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}}),r=s.session_update_action;return this.debug(`[privy:refresh] response: ${r}`),t||this.callbacks?.setUser?.(s.user),"set"===r&&(await this.session.updateWithTokensResponse(s),this.debug("[privy:refresh] tokens stored")),"clear"===r&&(await this.session.destroyLocalState(),this.debug("[privy:refresh] tokens cleared"),t||this.callbacks?.setUser?.(null)),"ignore"===r&&s.token&&(await this.session.storeToken(s.token),this.debug("[privy:refresh] access token stored"),s.identity_token&&(this.debug("[privy:refresh] identity token stored"),await this.session.storeIdentityToken(s.identity_token))),this.debug("[privy:refresh] returning response"),s}catch(e){throw this.debug(`[privy:refresh] error: ${e.message??"unknown error"}`),e instanceof r.PrivyApiError&&e.code===s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),t||this.callbacks?.setUser?.(null)),e}}async getAccessToken(){let[e,t]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);return!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)?(await this.refreshSession(),await this.session.getToken()):e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{let e=`privy:__storage__test-${t.v4()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return console.error(e),!1}}debug(e){"DEBUG"===this._logLevel&&console.debug(e)}constructor(e){this._sdkVersion="js-sdk-core:0.44.
|
|
1
|
+
"use strict";var e=require("fetch-retry"),t=require("uuid"),s=require("@privy-io/api-base"),i=require("@privy-io/public-api"),r=require("../Error.js"),a=require("../Session.js"),n=require("../toAbortSignalTimeout.js"),o=require("../utils/toSearchParams.js");function h(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}require("eventemitter3"),require("jose"),require("js-cookie"),require("../Token.js"),require("../utils/allSettled.js");var l=/*#__PURE__*/h(e);let c="privy:caid";exports.PrivyInternal=class{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!this._config}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new r.PrivyClientError({code:"storage_error",error:"Unable to access storage"});this._config=await this.getAppConfig(),this._config?.custom_api_url&&(this.baseUrl=this._config.custom_api_url,this.session.isUsingServerCookies=!0),this.callbacks?.setIsReady?.(!0),this.createAnalyticsEvent("sdk_initialize",{})}}async fetch(e,{body:t,params:s,query:a,headers:n,options:h={onRequest:this._beforeRequest.bind(this)}}){let l=new Request(`${this.baseUrl}${i.getPathWithParams(e.path,s)}${o.toSearchParams(a)}`,{method:e.method,body:JSON.stringify(t),headers:n}),c=await h.onRequest(l),d=await this._fetch(l,c),u=await d.json();if(d.status>299)throw new r.PrivyApiError(u);return u}async _beforeRequestWithoutInitialize(e){let t=await this.session.getToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");let i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:n.default(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessToken(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(i.AppConfig,{params:{app_id:this.appId},options:{onRequest:this._beforeRequestWithoutInitialize.bind(this)}})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{let e=await this._storage.get(c);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){console.error("Unable to load clientId",e)}try{this._analyticsId=t.v4()}catch(e){console.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(c,this._analyticsId)}catch(e){console.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(c)}catch(e){console.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(i.AnalyticsEvent,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}})}catch(e){}}async refreshSession(e=!1){let t=await this.session.getRefreshToken()??void 0,s=t??"key",i=this._cache.get(s);if(i)return console.debug("Found in-flight session refresh request, deduping"),await i;let r=this._refreshSession(t,e);this._cache.set(s,r);try{return await r}finally{this._cache.delete(s)}}async _refreshSession(e,t){let a=await this.session.getToken();if(!this.session.hasRefreshCredentials(a,e??null))throw this.debug("[privy:refresh] missing tokens, skipping request"),await this._initialize(),new r.PrivyApiError({code:s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{this.debug(`[privy:refresh] fetching: ${i.RefreshSession.path}`);let s=await this.fetch(i.RefreshSession,{body:{refresh_token:e},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}}),r=s.session_update_action;return this.debug(`[privy:refresh] response: ${r}`),t||this.callbacks?.setUser?.(s.user),"set"===r&&(await this.session.updateWithTokensResponse(s),this.debug("[privy:refresh] tokens stored")),"clear"===r&&(await this.session.destroyLocalState(),this.debug("[privy:refresh] tokens cleared"),t||this.callbacks?.setUser?.(null)),"ignore"===r&&s.token&&(await this.session.storeToken(s.token),this.debug("[privy:refresh] access token stored"),s.identity_token&&(this.debug("[privy:refresh] identity token stored"),await this.session.storeIdentityToken(s.identity_token))),this.debug("[privy:refresh] returning response"),s}catch(e){throw this.debug(`[privy:refresh] error: ${e.message??"unknown error"}`),e instanceof r.PrivyApiError&&e.code===s.PrivyErrorCode.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),t||this.callbacks?.setUser?.(null)),e}}async getAccessToken(){let[e,t]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);return!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)?(await this.refreshSession(),await this.session.getToken()):e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{let e=`privy:__storage__test-${t.v4()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return console.error(e),!1}}debug(e){"DEBUG"===this._logLevel&&console.debug(e)}constructor(e){this._sdkVersion="js-sdk-core:0.44.3-beta-20250221224248",this._cache=new Map,this._storage=e.storage,this._analyticsId=null,this._getOrGenerateClientAnalyticsId(),this.baseUrl=e.baseUrl??"https://auth.privy.io",this.appId=e.appId,this.appClientId=e.appClientId,this._sdkVersion=e.sdkVersion??this._sdkVersion,this._logLevel=e.logLevel,this.callbacks=e.callbacks,this.nativeAppIdentifier=e.nativeAppIdentifier,this.session=new a.Session({storage:this._storage,isUsingServerCookies:!1,appId:e.appId}),this._fetch=l.default(fetch,{retries:3,retryDelay:500}),this.session.on("error_storing_tokens",(e=>{this.createAnalyticsEvent("error_updating_tokens_in_storage",{reason:e})}))}};
|
package/dist/dts/index.d.mts
CHANGED
|
@@ -1302,6 +1302,12 @@ declare class EmbeddedWalletApi {
|
|
|
1302
1302
|
smart_wallets: {
|
|
1303
1303
|
address: string;
|
|
1304
1304
|
}[];
|
|
1305
|
+
} | {
|
|
1306
|
+
type: "signer";
|
|
1307
|
+
public_key: string;
|
|
1308
|
+
verified_at: number;
|
|
1309
|
+
first_verified_at: number | null;
|
|
1310
|
+
latest_verified_at: number | null;
|
|
1305
1311
|
})[];
|
|
1306
1312
|
created_at: number;
|
|
1307
1313
|
has_accepted_terms: boolean;
|
|
@@ -1693,6 +1699,12 @@ declare class UserApi {
|
|
|
1693
1699
|
smart_wallets: {
|
|
1694
1700
|
address: string;
|
|
1695
1701
|
}[];
|
|
1702
|
+
} | {
|
|
1703
|
+
type: "signer";
|
|
1704
|
+
public_key: string;
|
|
1705
|
+
verified_at: number;
|
|
1706
|
+
first_verified_at: number | null;
|
|
1707
|
+
latest_verified_at: number | null;
|
|
1696
1708
|
})[];
|
|
1697
1709
|
created_at: number;
|
|
1698
1710
|
has_accepted_terms: boolean;
|
|
@@ -1995,6 +2007,12 @@ declare class EmailApi {
|
|
|
1995
2007
|
smart_wallets: {
|
|
1996
2008
|
address: string;
|
|
1997
2009
|
}[];
|
|
2010
|
+
} | {
|
|
2011
|
+
type: "signer";
|
|
2012
|
+
public_key: string;
|
|
2013
|
+
verified_at: number;
|
|
2014
|
+
first_verified_at: number | null;
|
|
2015
|
+
latest_verified_at: number | null;
|
|
1998
2016
|
})[];
|
|
1999
2017
|
created_at: number;
|
|
2000
2018
|
has_accepted_terms: boolean;
|
|
@@ -2249,6 +2267,12 @@ declare class EmailApi {
|
|
|
2249
2267
|
smart_wallets: {
|
|
2250
2268
|
address: string;
|
|
2251
2269
|
}[];
|
|
2270
|
+
} | {
|
|
2271
|
+
type: "signer";
|
|
2272
|
+
public_key: string;
|
|
2273
|
+
verified_at: number;
|
|
2274
|
+
first_verified_at: number | null;
|
|
2275
|
+
latest_verified_at: number | null;
|
|
2252
2276
|
})[];
|
|
2253
2277
|
created_at: number;
|
|
2254
2278
|
has_accepted_terms: boolean;
|
|
@@ -2538,6 +2562,12 @@ declare class FarcasterApi {
|
|
|
2538
2562
|
smart_wallets: {
|
|
2539
2563
|
address: string;
|
|
2540
2564
|
}[];
|
|
2565
|
+
} | {
|
|
2566
|
+
type: "signer";
|
|
2567
|
+
public_key: string;
|
|
2568
|
+
verified_at: number;
|
|
2569
|
+
first_verified_at: number | null;
|
|
2570
|
+
latest_verified_at: number | null;
|
|
2541
2571
|
})[];
|
|
2542
2572
|
created_at: number;
|
|
2543
2573
|
has_accepted_terms: boolean;
|
|
@@ -2797,6 +2827,12 @@ declare class FarcasterApi {
|
|
|
2797
2827
|
smart_wallets: {
|
|
2798
2828
|
address: string;
|
|
2799
2829
|
}[];
|
|
2830
|
+
} | {
|
|
2831
|
+
type: "signer";
|
|
2832
|
+
public_key: string;
|
|
2833
|
+
verified_at: number;
|
|
2834
|
+
first_verified_at: number | null;
|
|
2835
|
+
latest_verified_at: number | null;
|
|
2800
2836
|
})[];
|
|
2801
2837
|
created_at: number;
|
|
2802
2838
|
has_accepted_terms: boolean;
|
|
@@ -3104,6 +3140,12 @@ declare class OAuthApi {
|
|
|
3104
3140
|
smart_wallets: {
|
|
3105
3141
|
address: string;
|
|
3106
3142
|
}[];
|
|
3143
|
+
} | {
|
|
3144
|
+
type: "signer";
|
|
3145
|
+
public_key: string;
|
|
3146
|
+
verified_at: number;
|
|
3147
|
+
first_verified_at: number | null;
|
|
3148
|
+
latest_verified_at: number | null;
|
|
3107
3149
|
})[];
|
|
3108
3150
|
created_at: number;
|
|
3109
3151
|
has_accepted_terms: boolean;
|
|
@@ -3364,6 +3406,12 @@ declare class OAuthApi {
|
|
|
3364
3406
|
smart_wallets: {
|
|
3365
3407
|
address: string;
|
|
3366
3408
|
}[];
|
|
3409
|
+
} | {
|
|
3410
|
+
type: "signer";
|
|
3411
|
+
public_key: string;
|
|
3412
|
+
verified_at: number;
|
|
3413
|
+
first_verified_at: number | null;
|
|
3414
|
+
latest_verified_at: number | null;
|
|
3367
3415
|
})[];
|
|
3368
3416
|
created_at: number;
|
|
3369
3417
|
has_accepted_terms: boolean;
|
|
@@ -3771,6 +3819,12 @@ declare class PhoneApi {
|
|
|
3771
3819
|
smart_wallets: {
|
|
3772
3820
|
address: string;
|
|
3773
3821
|
}[];
|
|
3822
|
+
} | {
|
|
3823
|
+
type: "signer";
|
|
3824
|
+
public_key: string;
|
|
3825
|
+
verified_at: number;
|
|
3826
|
+
first_verified_at: number | null;
|
|
3827
|
+
latest_verified_at: number | null;
|
|
3774
3828
|
})[];
|
|
3775
3829
|
created_at: number;
|
|
3776
3830
|
has_accepted_terms: boolean;
|
|
@@ -4025,6 +4079,12 @@ declare class PhoneApi {
|
|
|
4025
4079
|
smart_wallets: {
|
|
4026
4080
|
address: string;
|
|
4027
4081
|
}[];
|
|
4082
|
+
} | {
|
|
4083
|
+
type: "signer";
|
|
4084
|
+
public_key: string;
|
|
4085
|
+
verified_at: number;
|
|
4086
|
+
first_verified_at: number | null;
|
|
4087
|
+
latest_verified_at: number | null;
|
|
4028
4088
|
})[];
|
|
4029
4089
|
created_at: number;
|
|
4030
4090
|
has_accepted_terms: boolean;
|
|
@@ -5246,6 +5306,12 @@ declare const delegateWallet: (client: Privy) => ({ address, chainType }: Delega
|
|
|
5246
5306
|
smart_wallets: {
|
|
5247
5307
|
address: string;
|
|
5248
5308
|
}[];
|
|
5309
|
+
} | {
|
|
5310
|
+
type: "signer";
|
|
5311
|
+
public_key: string;
|
|
5312
|
+
verified_at: number;
|
|
5313
|
+
first_verified_at: number | null;
|
|
5314
|
+
latest_verified_at: number | null;
|
|
5249
5315
|
})[];
|
|
5250
5316
|
created_at: number;
|
|
5251
5317
|
has_accepted_terms: boolean;
|
|
@@ -5511,6 +5577,12 @@ declare const revokeWallets: (client: Privy) => () => Promise<{
|
|
|
5511
5577
|
smart_wallets: {
|
|
5512
5578
|
address: string;
|
|
5513
5579
|
}[];
|
|
5580
|
+
} | {
|
|
5581
|
+
type: "signer";
|
|
5582
|
+
public_key: string;
|
|
5583
|
+
verified_at: number;
|
|
5584
|
+
first_verified_at: number | null;
|
|
5585
|
+
latest_verified_at: number | null;
|
|
5514
5586
|
})[];
|
|
5515
5587
|
created_at: number;
|
|
5516
5588
|
has_accepted_terms: boolean;
|
package/dist/dts/index.d.ts
CHANGED
|
@@ -1302,6 +1302,12 @@ declare class EmbeddedWalletApi {
|
|
|
1302
1302
|
smart_wallets: {
|
|
1303
1303
|
address: string;
|
|
1304
1304
|
}[];
|
|
1305
|
+
} | {
|
|
1306
|
+
type: "signer";
|
|
1307
|
+
public_key: string;
|
|
1308
|
+
verified_at: number;
|
|
1309
|
+
first_verified_at: number | null;
|
|
1310
|
+
latest_verified_at: number | null;
|
|
1305
1311
|
})[];
|
|
1306
1312
|
created_at: number;
|
|
1307
1313
|
has_accepted_terms: boolean;
|
|
@@ -1693,6 +1699,12 @@ declare class UserApi {
|
|
|
1693
1699
|
smart_wallets: {
|
|
1694
1700
|
address: string;
|
|
1695
1701
|
}[];
|
|
1702
|
+
} | {
|
|
1703
|
+
type: "signer";
|
|
1704
|
+
public_key: string;
|
|
1705
|
+
verified_at: number;
|
|
1706
|
+
first_verified_at: number | null;
|
|
1707
|
+
latest_verified_at: number | null;
|
|
1696
1708
|
})[];
|
|
1697
1709
|
created_at: number;
|
|
1698
1710
|
has_accepted_terms: boolean;
|
|
@@ -1995,6 +2007,12 @@ declare class EmailApi {
|
|
|
1995
2007
|
smart_wallets: {
|
|
1996
2008
|
address: string;
|
|
1997
2009
|
}[];
|
|
2010
|
+
} | {
|
|
2011
|
+
type: "signer";
|
|
2012
|
+
public_key: string;
|
|
2013
|
+
verified_at: number;
|
|
2014
|
+
first_verified_at: number | null;
|
|
2015
|
+
latest_verified_at: number | null;
|
|
1998
2016
|
})[];
|
|
1999
2017
|
created_at: number;
|
|
2000
2018
|
has_accepted_terms: boolean;
|
|
@@ -2249,6 +2267,12 @@ declare class EmailApi {
|
|
|
2249
2267
|
smart_wallets: {
|
|
2250
2268
|
address: string;
|
|
2251
2269
|
}[];
|
|
2270
|
+
} | {
|
|
2271
|
+
type: "signer";
|
|
2272
|
+
public_key: string;
|
|
2273
|
+
verified_at: number;
|
|
2274
|
+
first_verified_at: number | null;
|
|
2275
|
+
latest_verified_at: number | null;
|
|
2252
2276
|
})[];
|
|
2253
2277
|
created_at: number;
|
|
2254
2278
|
has_accepted_terms: boolean;
|
|
@@ -2538,6 +2562,12 @@ declare class FarcasterApi {
|
|
|
2538
2562
|
smart_wallets: {
|
|
2539
2563
|
address: string;
|
|
2540
2564
|
}[];
|
|
2565
|
+
} | {
|
|
2566
|
+
type: "signer";
|
|
2567
|
+
public_key: string;
|
|
2568
|
+
verified_at: number;
|
|
2569
|
+
first_verified_at: number | null;
|
|
2570
|
+
latest_verified_at: number | null;
|
|
2541
2571
|
})[];
|
|
2542
2572
|
created_at: number;
|
|
2543
2573
|
has_accepted_terms: boolean;
|
|
@@ -2797,6 +2827,12 @@ declare class FarcasterApi {
|
|
|
2797
2827
|
smart_wallets: {
|
|
2798
2828
|
address: string;
|
|
2799
2829
|
}[];
|
|
2830
|
+
} | {
|
|
2831
|
+
type: "signer";
|
|
2832
|
+
public_key: string;
|
|
2833
|
+
verified_at: number;
|
|
2834
|
+
first_verified_at: number | null;
|
|
2835
|
+
latest_verified_at: number | null;
|
|
2800
2836
|
})[];
|
|
2801
2837
|
created_at: number;
|
|
2802
2838
|
has_accepted_terms: boolean;
|
|
@@ -3104,6 +3140,12 @@ declare class OAuthApi {
|
|
|
3104
3140
|
smart_wallets: {
|
|
3105
3141
|
address: string;
|
|
3106
3142
|
}[];
|
|
3143
|
+
} | {
|
|
3144
|
+
type: "signer";
|
|
3145
|
+
public_key: string;
|
|
3146
|
+
verified_at: number;
|
|
3147
|
+
first_verified_at: number | null;
|
|
3148
|
+
latest_verified_at: number | null;
|
|
3107
3149
|
})[];
|
|
3108
3150
|
created_at: number;
|
|
3109
3151
|
has_accepted_terms: boolean;
|
|
@@ -3364,6 +3406,12 @@ declare class OAuthApi {
|
|
|
3364
3406
|
smart_wallets: {
|
|
3365
3407
|
address: string;
|
|
3366
3408
|
}[];
|
|
3409
|
+
} | {
|
|
3410
|
+
type: "signer";
|
|
3411
|
+
public_key: string;
|
|
3412
|
+
verified_at: number;
|
|
3413
|
+
first_verified_at: number | null;
|
|
3414
|
+
latest_verified_at: number | null;
|
|
3367
3415
|
})[];
|
|
3368
3416
|
created_at: number;
|
|
3369
3417
|
has_accepted_terms: boolean;
|
|
@@ -3771,6 +3819,12 @@ declare class PhoneApi {
|
|
|
3771
3819
|
smart_wallets: {
|
|
3772
3820
|
address: string;
|
|
3773
3821
|
}[];
|
|
3822
|
+
} | {
|
|
3823
|
+
type: "signer";
|
|
3824
|
+
public_key: string;
|
|
3825
|
+
verified_at: number;
|
|
3826
|
+
first_verified_at: number | null;
|
|
3827
|
+
latest_verified_at: number | null;
|
|
3774
3828
|
})[];
|
|
3775
3829
|
created_at: number;
|
|
3776
3830
|
has_accepted_terms: boolean;
|
|
@@ -4025,6 +4079,12 @@ declare class PhoneApi {
|
|
|
4025
4079
|
smart_wallets: {
|
|
4026
4080
|
address: string;
|
|
4027
4081
|
}[];
|
|
4082
|
+
} | {
|
|
4083
|
+
type: "signer";
|
|
4084
|
+
public_key: string;
|
|
4085
|
+
verified_at: number;
|
|
4086
|
+
first_verified_at: number | null;
|
|
4087
|
+
latest_verified_at: number | null;
|
|
4028
4088
|
})[];
|
|
4029
4089
|
created_at: number;
|
|
4030
4090
|
has_accepted_terms: boolean;
|
|
@@ -5246,6 +5306,12 @@ declare const delegateWallet: (client: Privy) => ({ address, chainType }: Delega
|
|
|
5246
5306
|
smart_wallets: {
|
|
5247
5307
|
address: string;
|
|
5248
5308
|
}[];
|
|
5309
|
+
} | {
|
|
5310
|
+
type: "signer";
|
|
5311
|
+
public_key: string;
|
|
5312
|
+
verified_at: number;
|
|
5313
|
+
first_verified_at: number | null;
|
|
5314
|
+
latest_verified_at: number | null;
|
|
5249
5315
|
})[];
|
|
5250
5316
|
created_at: number;
|
|
5251
5317
|
has_accepted_terms: boolean;
|
|
@@ -5511,6 +5577,12 @@ declare const revokeWallets: (client: Privy) => () => Promise<{
|
|
|
5511
5577
|
smart_wallets: {
|
|
5512
5578
|
address: string;
|
|
5513
5579
|
}[];
|
|
5580
|
+
} | {
|
|
5581
|
+
type: "signer";
|
|
5582
|
+
public_key: string;
|
|
5583
|
+
verified_at: number;
|
|
5584
|
+
first_verified_at: number | null;
|
|
5585
|
+
latest_verified_at: number | null;
|
|
5514
5586
|
})[];
|
|
5515
5587
|
created_at: number;
|
|
5516
5588
|
has_accepted_terms: boolean;
|
package/dist/esm/Session.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import e from"eventemitter3";import{base64url as t}from"jose";import s from"js-cookie";import{Token as r}from"./Token.mjs";import{allSettled as i}from"./utils/allSettled.mjs";let o="privy:token",n="privy-token",a="privy:refresh_token",_="privy-refresh-token",h="privy:id-token",
|
|
1
|
+
import e from"eventemitter3";import{base64url as t}from"jose";import s from"js-cookie";import{Token as r}from"./Token.mjs";import{allSettled as i}from"./utils/allSettled.mjs";let o="privy:token",n="privy-token",a="privy:refresh_token",_="privy-refresh-token",h="privy:id-token",g="privy-id-token",l="privy-session",k="privy:session_transfer_token";class d extends e{set isUsingServerCookies(e){this._isUsingServerCookies=e}async getToken(){let e=await this._storage.get(o);try{return"string"==typeof e?new r(e).value:null}catch(e){return console.error(e),await this.destroyLocalState({reason:"getToken_error"}),null}}async getRefreshToken(){let e=await this._storage.get(a);return"string"==typeof e?e:null}async getIdentityToken(){let e=await this._storage.get(h);return"string"==typeof e?e:null}async getForkedToken(){let e=await this._storage.get(k);return"string"==typeof e?e:null}get mightHaveServerCookies(){try{let e=s.get(l);return void 0!==e&&e.length>0}catch(e){console.error(e)}return!1}hasRefreshCredentials(e,t){return this.mightHaveServerCookies||"string"==typeof e&&"string"==typeof t}async hasRecoveryCredentials(){return"string"==typeof await this.getForkedToken()}tokenIsActive(e){if(!e)return!1;let t=r.parse(e);return null!==t&&!t.isExpired(30)}async destroyLocalState(e){await i([this._storage.del(o),this._storage.del(a),this._storage.del(h),this._storage.del(this.GUEST_CREDENTIAL_STORAGE_KEY),this.clearForkedToken()]),s.remove(n),s.remove(_),s.remove(g),s.remove(l),e?.reason&&this.emit("storage_cleared",{reason:e.reason})}async storeToken(e){if("string"==typeof e){let t=await this._storage.get(o);if(await this._storage.put(o,e),!this._isUsingServerCookies){let t=r.parse(e)?.expiration;s.set(n,e,{sameSite:"Strict",secure:!0,expires:t?new Date(1e3*t):void 0})}t!==e&&this.emit("token_stored",{cookiesEnabled:this._isUsingServerCookies})}else{let e=await this._storage.get(o);await this._storage.del(o),s.remove(n),null!==e&&this.emit("token_cleared",{reason:"set_with_non_string_value"})}}async storeRefreshToken(e){"string"==typeof e?(await this._storage.put(a,e),this._isUsingServerCookies||(s.set(l,"t",{sameSite:"Strict",secure:!0,expires:30}),s.set(_,e,{sameSite:"Strict",secure:!0,expires:30})),this.emit("refresh_token_stored",{cookiesEnabled:this._isUsingServerCookies})):(await this._storage.del(a),s.remove(_),s.remove(l),this.emit("refresh_token_cleared",{reason:"set_with_non_string_value"}))}async updateWithTokensResponse(e){let t=(await i([this.storeToken(e.token),this.storeRefreshToken(e.refresh_token),this.storeIdentityToken(e.identity_token),this.processOAuthTokens(e.oauth_tokens)])).filter((e=>"rejected"===e.status));t.length>0&&this.emit("error_storing_tokens",t.map((e=>String(e.status))).join(", "))}async processOAuthTokens(e){e&&this.emit("oauth_tokens_granted",e)}async storeIdentityToken(e){if("string"==typeof e){let t=await this._storage.get(h);if(await this._storage.put(h,e),!this._isUsingServerCookies){let t=r.parse(e)?.expiration;s.set(g,e,{sameSite:"Strict",secure:!0,expires:t?new Date(1e3*t):void 0})}t!==e&&this.emit("identity_token_stored",{cookiesEnabled:this._isUsingServerCookies})}else{let e=await this._storage.get(h);await this._storage.del(h),s.remove(g),null!==e&&this.emit("identity_token_cleared",{reason:"set_with_non_string_value"})}}async getOrCreateGuestCredential(){let e=this._storage.get(this.GUEST_CREDENTIAL_STORAGE_KEY);if(e&&"string"==typeof e)return e;let s=t.encode(crypto.getRandomValues(new Uint8Array(32)));return await this._storage.put(this.GUEST_CREDENTIAL_STORAGE_KEY,s),s}async clearForkedToken(){await this._storage.del(k)}constructor(e){super(),this._isUsingServerCookies=!1,this._storage=e.storage,this.GUEST_CREDENTIAL_STORAGE_KEY=`privy:guest:${e.appId}`}}d.events=["storage_cleared","token_cleared","refresh_token_cleared","identity_token_cleared","forked_token_cleared","token_stored","refresh_token_stored","identity_token_stored","oauth_tokens_granted","error_storing_tokens"];export{d as Session};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import e from"fetch-retry";import{v4 as t}from"uuid";import{PrivyErrorCode as s}from"@privy-io/api-base";import{getPathWithParams as i,AppConfig as r,AnalyticsEvent as a,RefreshSession as n}from"@privy-io/public-api";import{PrivyClientError as o,PrivyApiError as h}from"../Error.mjs";import{Session as c}from"../Session.mjs";import l from"../toAbortSignalTimeout.mjs";import{toSearchParams as d}from"../utils/toSearchParams.mjs";import"eventemitter3";import"jose";import"js-cookie";import"../Token.mjs";import"../utils/allSettled.mjs";let p="privy:caid";class u{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!this._config}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new o({code:"storage_error",error:"Unable to access storage"});this._config=await this.getAppConfig(),this._config?.custom_api_url&&(this.baseUrl=this._config.custom_api_url,this.session.isUsingServerCookies=!0),this.callbacks?.setIsReady?.(!0),this.createAnalyticsEvent("sdk_initialize",{})}}async fetch(e,{body:t,params:s,query:r,headers:a,options:n={onRequest:this._beforeRequest.bind(this)}}){let o=new Request(`${this.baseUrl}${i(e.path,s)}${d(r)}`,{method:e.method,body:JSON.stringify(t),headers:a}),c=await n.onRequest(o),l=await this._fetch(o,c),p=await l.json();if(l.status>299)throw new h(p);return p}async _beforeRequestWithoutInitialize(e){let t=await this.session.getToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");let i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:l(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessToken(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(r,{params:{app_id:this.appId},options:{onRequest:this._beforeRequestWithoutInitialize.bind(this)}})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{let e=await this._storage.get(p);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){console.error("Unable to load clientId",e)}try{this._analyticsId=t()}catch(e){console.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(p,this._analyticsId)}catch(e){console.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(p)}catch(e){console.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(a,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}})}catch(e){}}async refreshSession(e=!1){let t=await this.session.getRefreshToken()??void 0,s=t??"key",i=this._cache.get(s);if(i)return console.debug("Found in-flight session refresh request, deduping"),await i;let r=this._refreshSession(t,e);this._cache.set(s,r);try{return await r}finally{this._cache.delete(s)}}async _refreshSession(e,t){let i=await this.session.getToken();if(!this.session.hasRefreshCredentials(i,e??null))throw this.debug("[privy:refresh] missing tokens, skipping request"),await this._initialize(),new h({code:s.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{this.debug(`[privy:refresh] fetching: ${n.path}`);let s=await this.fetch(n,{body:{refresh_token:e},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}}),i=s.session_update_action;return this.debug(`[privy:refresh] response: ${i}`),t||this.callbacks?.setUser?.(s.user),"set"===i&&(await this.session.updateWithTokensResponse(s),this.debug("[privy:refresh] tokens stored")),"clear"===i&&(await this.session.destroyLocalState(),this.debug("[privy:refresh] tokens cleared"),t||this.callbacks?.setUser?.(null)),"ignore"===i&&s.token&&(await this.session.storeToken(s.token),this.debug("[privy:refresh] access token stored"),s.identity_token&&(this.debug("[privy:refresh] identity token stored"),await this.session.storeIdentityToken(s.identity_token))),this.debug("[privy:refresh] returning response"),s}catch(e){throw this.debug(`[privy:refresh] error: ${e.message??"unknown error"}`),e instanceof h&&e.code===s.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),t||this.callbacks?.setUser?.(null)),e}}async getAccessToken(){let[e,t]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);return!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)?(await this.refreshSession(),await this.session.getToken()):e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{let e=`privy:__storage__test-${t()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return console.error(e),!1}}debug(e){"DEBUG"===this._logLevel&&console.debug(e)}constructor(t){this._sdkVersion="js-sdk-core:0.44.
|
|
1
|
+
import e from"fetch-retry";import{v4 as t}from"uuid";import{PrivyErrorCode as s}from"@privy-io/api-base";import{getPathWithParams as i,AppConfig as r,AnalyticsEvent as a,RefreshSession as n}from"@privy-io/public-api";import{PrivyClientError as o,PrivyApiError as h}from"../Error.mjs";import{Session as c}from"../Session.mjs";import l from"../toAbortSignalTimeout.mjs";import{toSearchParams as d}from"../utils/toSearchParams.mjs";import"eventemitter3";import"jose";import"js-cookie";import"../Token.mjs";import"../utils/allSettled.mjs";let p="privy:caid";class u{setCallbacks(e){this.callbacks={...this.callbacks,...e}}get isReady(){return!!this._config}get config(){return this._config}get caid(){return this._analyticsId}async _initialize(){if(this.isReady)this.callbacks?.setIsReady?.(!0);else{if(!await this.isStorageAccessible())throw new o({code:"storage_error",error:"Unable to access storage"});this._config=await this.getAppConfig(),this._config?.custom_api_url&&(this.baseUrl=this._config.custom_api_url,this.session.isUsingServerCookies=!0),this.callbacks?.setIsReady?.(!0),this.createAnalyticsEvent("sdk_initialize",{})}}async fetch(e,{body:t,params:s,query:r,headers:a,options:n={onRequest:this._beforeRequest.bind(this)}}){let o=new Request(`${this.baseUrl}${i(e.path,s)}${d(r)}`,{method:e.method,body:JSON.stringify(t),headers:a}),c=await n.onRequest(o),l=await this._fetch(o,c),p=await l.json();if(l.status>299)throw new h(p);return p}async _beforeRequestWithoutInitialize(e){let t=await this.session.getToken(),s=new Headers(e.headers);s.set("privy-app-id",this.appId),this.appClientId&&s.set("privy-client-id",this.appClientId),s.set("privy-client",this._sdkVersion),t&&s.set("Authorization",`Bearer ${t}`),s.set("Content-Type","application/json"),s.set("Accept","application/json");let i=await this._getOrGenerateClientAnalyticsId();return i&&s.set("privy-ca-id",i),this.nativeAppIdentifier&&s.set("x-native-app-identifier",this.nativeAppIdentifier),{signal:l(2e4),headers:s,credentials:"include"}}async beforeRequestWithoutRefresh(e){return await this._initialize(),this._beforeRequestWithoutInitialize(e)}async _beforeRequest(e){return await this._initialize(),await this.getAccessToken(),this.beforeRequestWithoutRefresh(e)}async getAppConfig(){return await this.fetch(r,{params:{app_id:this.appId},options:{onRequest:this._beforeRequestWithoutInitialize.bind(this)}})}async _getOrGenerateClientAnalyticsId(){if(this._analyticsId)return this._analyticsId;try{let e=await this._storage.get(p);if("string"==typeof e&&e.length>0)return this._analyticsId=e,e}catch(e){console.error("Unable to load clientId",e)}try{this._analyticsId=t()}catch(e){console.error("Unable to generate uuidv4",e)}if(!this._analyticsId)return null;try{await this._storage.put(p,this._analyticsId)}catch(e){console.error(`Unable to store clientId: ${this._analyticsId}`,e)}return this._analyticsId}async destroyClientAnalyticsId(){try{return await this._storage.del(p)}catch(e){console.error("Unable to delete clientId",e)}}async createAnalyticsEvent(e,t){try{await this.fetch(a,{body:{event_name:e,client_id:await this._getOrGenerateClientAnalyticsId(),payload:t},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}})}catch(e){}}async refreshSession(e=!1){let t=await this.session.getRefreshToken()??void 0,s=t??"key",i=this._cache.get(s);if(i)return console.debug("Found in-flight session refresh request, deduping"),await i;let r=this._refreshSession(t,e);this._cache.set(s,r);try{return await r}finally{this._cache.delete(s)}}async _refreshSession(e,t){let i=await this.session.getToken();if(!this.session.hasRefreshCredentials(i,e??null))throw this.debug("[privy:refresh] missing tokens, skipping request"),await this._initialize(),new h({code:s.MISSING_OR_INVALID_TOKEN,error:"No tokens found in storage"});try{this.debug(`[privy:refresh] fetching: ${n.path}`);let s=await this.fetch(n,{body:{refresh_token:e},options:{onRequest:this.beforeRequestWithoutRefresh.bind(this)}}),i=s.session_update_action;return this.debug(`[privy:refresh] response: ${i}`),t||this.callbacks?.setUser?.(s.user),"set"===i&&(await this.session.updateWithTokensResponse(s),this.debug("[privy:refresh] tokens stored")),"clear"===i&&(await this.session.destroyLocalState(),this.debug("[privy:refresh] tokens cleared"),t||this.callbacks?.setUser?.(null)),"ignore"===i&&s.token&&(await this.session.storeToken(s.token),this.debug("[privy:refresh] access token stored"),s.identity_token&&(this.debug("[privy:refresh] identity token stored"),await this.session.storeIdentityToken(s.identity_token))),this.debug("[privy:refresh] returning response"),s}catch(e){throw this.debug(`[privy:refresh] error: ${e.message??"unknown error"}`),e instanceof h&&e.code===s.MISSING_OR_INVALID_TOKEN&&(await this.session.destroyLocalState(),t||this.callbacks?.setUser?.(null)),e}}async getAccessToken(){let[e,t]=await Promise.all([this.session.getToken(),this.session.getRefreshToken()]);return!this.session.tokenIsActive(e)&&this.session.hasRefreshCredentials(e,t)?(await this.refreshSession(),await this.session.getToken()):e}async getIdentityToken(){return await this.session.getIdentityToken()}async isStorageAccessible(){try{let e=`privy:__storage__test-${t()}`,s="blobby";await this._storage.put(e,s);let i=await this._storage.get(e);return await this._storage.del(e),i===s}catch(e){return console.error(e),!1}}debug(e){"DEBUG"===this._logLevel&&console.debug(e)}constructor(t){this._sdkVersion="js-sdk-core:0.44.3-beta-20250221224248",this._cache=new Map,this._storage=t.storage,this._analyticsId=null,this._getOrGenerateClientAnalyticsId(),this.baseUrl=t.baseUrl??"https://auth.privy.io",this.appId=t.appId,this.appClientId=t.appClientId,this._sdkVersion=t.sdkVersion??this._sdkVersion,this._logLevel=t.logLevel,this.callbacks=t.callbacks,this.nativeAppIdentifier=t.nativeAppIdentifier,this.session=new c({storage:this._storage,isUsingServerCookies:!1,appId:t.appId}),this._fetch=e(fetch,{retries:3,retryDelay:500}),this.session.on("error_storing_tokens",(e=>{this.createAnalyticsEvent("error_updating_tokens_in_storage",{reason:e})}))}}export{u as PrivyInternal};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@privy-io/js-sdk-core",
|
|
3
|
-
"version": "0.44.
|
|
3
|
+
"version": "0.44.3-beta-20250221224248",
|
|
4
4
|
"description": "Vanilla JS client for the Privy Auth API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"authentication",
|
|
@@ -69,7 +69,7 @@
|
|
|
69
69
|
"@ethersproject/transactions": "^5.7.0",
|
|
70
70
|
"@ethersproject/units": "^5.7.0",
|
|
71
71
|
"@privy-io/api-base": "^1.4.3",
|
|
72
|
-
"@privy-io/public-api": "2.18.
|
|
72
|
+
"@privy-io/public-api": "2.18.10-beta-20250221224248",
|
|
73
73
|
"eventemitter3": "^5.0.1",
|
|
74
74
|
"fetch-retry": "^5.0.6",
|
|
75
75
|
"jose": "^4.15.5",
|