UNPKG

@privy-io/js-sdk-core

Version:

Vanilla JS client for the Privy Auth API

2 lines (1 loc) 3.71 kB
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";class k 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}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}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)]),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.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(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}constructor(e){super(),this._isUsingServerCookies=!1,this._storage=e.storage,this.GUEST_CREDENTIAL_STORAGE_KEY=`privy:guest:${e.appId}`}}k.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"];export{k as Session};