@privy-io/js-sdk-core
Version:
Vanilla JS client for the Privy Auth API
2 lines (1 loc) • 4.38 kB
JavaScript
"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 a=/*#__PURE__*/o(e),n=/*#__PURE__*/o(s);let l="privy:token",_="privy-token",h="privy:pat",g="privy:refresh_token",u="privy-refresh-token",d="privy:id-token",c="privy-id-token",k="privy-session";class y extends a.default{set isUsingServerCookies(e){this._isUsingServerCookies=e}async getCustomerAccessToken(){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 getPrivyAccessToken(){let e=await this._storage.get(h);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(g);return"string"==typeof e?e:null}async getIdentityToken(){let e=await this._storage.get(d);return"string"==typeof e?e:null}get mightHaveServerCookies(){try{let e=n.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}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(g),this._storage.del(d),this._storage.del(this.GUEST_CREDENTIAL_STORAGE_KEY)]),n.default.remove(_),n.default.remove(u),n.default.remove(c),n.default.remove(k),e?.reason&&this.emit("storage_cleared",{reason:e.reason})}async storeCustomerAccessToken(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;n.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),n.default.remove(_),null!==e&&this.emit("token_cleared",{reason:"set_with_non_string_value"})}}async storePrivyAccessToken(e){"string"==typeof e?await this._storage.put(h,e):await this._storage.del(h)}async storeRefreshToken(e){"string"==typeof e?(await this._storage.put(g,e),this._isUsingServerCookies||(n.default.set(k,"t",{sameSite:"Strict",secure:!0,expires:30}),n.default.set(u,e,{sameSite:"Strict",secure:!0,expires:30})),this.emit("refresh_token_stored",{cookiesEnabled:this._isUsingServerCookies})):(await this._storage.del(g),n.default.remove(u),n.default.remove(k),this.emit("refresh_token_cleared",{reason:"set_with_non_string_value"}))}async updateWithTokensResponse(e){let t=(await i.allSettled([this.storeCustomerAccessToken(e.token),this.storePrivyAccessToken(e.privy_access_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.reason))).join(", "))}async processOAuthTokens(e){e&&this.emit("oauth_tokens_granted",e)}async storeIdentityToken(e){if("string"==typeof e){let t=await this._storage.get(d);if(await this._storage.put(d,e),!this._isUsingServerCookies){let t=r.Token.parse(e)?.expiration;n.default.set(c,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(d);await this._storage.del(d),n.default.remove(c),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}constructor(e){super(),this._isUsingServerCookies=!1,this._storage=e.storage,this.GUEST_CREDENTIAL_STORAGE_KEY=`privy:guest:${e.appId}`}}y.events=["storage_cleared","token_cleared","refresh_token_cleared","identity_token_cleared","token_stored","refresh_token_stored","identity_token_stored","oauth_tokens_granted","error_storing_tokens"],exports.Session=y;