@privy-io/js-sdk-core
Version:
Vanilla JS client for the Privy Auth API
2 lines (1 loc) • 2.75 kB
JavaScript
import{OAuthInit as e,OAuthAuthenticate as t,OAuthLink as i,OAuthUnlink as s}from"@privy-io/routes";import{PrivyClientError as r}from"../../Error.mjs";import{createCodeVerifier as a,createStateCode as o,deriveCodeChallengeFromCodeVerifier as l,CODE_VERIFIER_KEY as d,STATE_CODE_KEY as n}from"../../pkce.mjs";import{maybeCreateWalletOnLogin as h}from"./maybeCreateWalletOnLogin.mjs";import"jose";import"../../utils/getUserEmbeddedEthereumWallet.mjs";import"../../utils/getAllUserEmbeddedEthereumWallets.mjs";import"../../utils/embedded-wallets.mjs";import"../../utils/getUserEmbeddedSolanaWallet.mjs";import"../../utils/getAllUserEmbeddedSolanaWallets.mjs";import"../../utils/shouldCreateEmbeddedEthWallet.mjs";import"../../utils/shouldCreateEmbeddedSolWallet.mjs";class m{async generateURL(t,i){let s=a(),r=o(),h=await l({codeVerifier:s,digest:this._crypto?.digest});return await Promise.all([this._storage.put(d,s),this._storage.put(n,r)]),this._privyInternal.fetch(e,{body:{redirect_to:i,provider:t,code_challenge:h,state_code:r}})}async loginWithCode(e,i,s,a,o,l){let[m,p]=await Promise.all([this._storage.get(d),this._storage.get(n)]);if(p!==i)throw this._privyInternal.createAnalyticsEvent("possible_phishing_attempt",{flow:"oauth",provider:s,storedStateCode:p??"",returnedStateCode:i??""}),new r({code:"pkce_state_code_mismatch",error:"Unexpected auth flow. This may be a phishing attempt."});let _=await this._privyInternal.fetch(t,{body:{authorization_code:e,code_type:a,state_code:p,code_verifier:m,mode:o}});await this._privyInternal.session.updateWithTokensResponse(_);let c=await h(this._embedded,_,l?.embedded);return await Promise.all([this._storage.del(d),this._storage.del(n)]),this._privyInternal.callbacks?.setUser?.(c.user),c}async linkWithCode(e,t,s,a){let[o,l]=await Promise.all([this._storage.get(d),this._storage.get(n)]);if(l!==t)throw this._privyInternal.createAnalyticsEvent("possible_phishing_attempt",{flow:"oauth",provider:s,storedStateCode:l??"",returnedStateCode:t??""}),new r({code:"pkce_state_code_mismatch",error:"Unexpected auth flow. This may be a phishing attempt."});let h=await this._privyInternal.fetch(i,{body:{authorization_code:e,code_type:a,state_code:l,code_verifier:o}});await this._privyInternal.session.processOAuthTokens(h.oauth_tokens);let m=await this._privyInternal.refreshSession();return await Promise.all([this._storage.del(d),this._storage.del(n)]),{user:m.user,identity_token:m.identity_token}}async unlink(e,t){await this._privyInternal.fetch(s,{body:{provider:e,subject:t}});let i=await this._privyInternal.refreshSession();return{user:i.user,identity_token:i.identity_token}}constructor(e,t,i,s){this._privyInternal=e,this._embedded=t,this._storage=i,this._crypto=s}}export{m as default};