UNPKG

@privy-io/js-sdk-core

Version:

Vanilla JS client for the Privy Auth API

2 lines (1 loc) 3.96 kB
"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",g="privy-refresh-token",d="privy:id-token",u="privy-id-token",k="privy-session";class c 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(d);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}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(d),this._storage.del(this.GUEST_CREDENTIAL_STORAGE_KEY)]),a.default.remove(_),a.default.remove(g),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(g,e,{sameSite:"Strict",secure:!0,expires:30})),this.emit("refresh_token_stored",{cookiesEnabled:this._isUsingServerCookies})):(await this._storage.del(h),a.default.remove(g),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.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;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(d);await this._storage.del(d),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}constructor(e){super(),this._isUsingServerCookies=!1,this._storage=e.storage,this.GUEST_CREDENTIAL_STORAGE_KEY=`privy:guest:${e.appId}`}}c.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=c;