UNPKG

@privy-io/js-sdk-core

Version:

Vanilla JS client for the Privy Auth API

2 lines (1 loc) 2.93 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@privy-io/public-api"),t=require("../../Error.js"),r=require("../../pkce.js"),i=require("./maybeCreateWalletOnLogin.js");require("jose"),require("../../utils/getUserEmbeddedEthereumWallet.js"),require("../../utils/getAllUserEmbeddedEthereumWallets.js"),require("../../utils/embedded-wallets.js"),require("../../utils/getUserEmbeddedSolanaWallet.js"),require("../../utils/getAllUserEmbeddedSolanaWallets.js"),require("../../utils/shouldCreateEmbeddedEthWallet.js"),require("../../utils/shouldCreateEmbeddedSolWallet.js");exports.default=class{async generateURL(t,i){let s=r.createCodeVerifier(),a=r.createStateCode(),o=await r.deriveCodeChallengeFromCodeVerifier({codeVerifier:s,digest:this._crypto?.digest});return await Promise.all([this._storage.put(r.CODE_VERIFIER_KEY,s),this._storage.put(r.STATE_CODE_KEY,a)]),this._privyInternal.fetch(e.OAuthInit,{body:{redirect_to:i,provider:t,code_challenge:o,state_code:a}})}async loginWithCode(s,a,o,l,d,n){let[_,h]=await Promise.all([this._storage.get(r.CODE_VERIFIER_KEY),this._storage.get(r.STATE_CODE_KEY)]);if(h!==a)throw this._privyInternal.createAnalyticsEvent("possible_phishing_attempt",{flow:"oauth",provider:o,storedStateCode:h??"",returnedStateCode:a??""}),new t.PrivyClientError({code:"pkce_state_code_mismatch",error:"Unexpected auth flow. This may be a phishing attempt."});let u=await this._privyInternal.fetch(e.OAuthAuthenticate,{body:{authorization_code:s,code_type:l,state_code:h,code_verifier:_,mode:d}});await this._privyInternal.session.updateWithTokensResponse(u);let c=await i.maybeCreateWalletOnLogin(this._embedded,u,n?.embedded);return await Promise.all([this._storage.del(r.CODE_VERIFIER_KEY),this._storage.del(r.STATE_CODE_KEY)]),this._privyInternal.callbacks?.setUser?.(c.user),c}async linkWithCode(i,s,a,o){let[l,d]=await Promise.all([this._storage.get(r.CODE_VERIFIER_KEY),this._storage.get(r.STATE_CODE_KEY)]);if(d!==s)throw this._privyInternal.createAnalyticsEvent("possible_phishing_attempt",{flow:"oauth",provider:a,storedStateCode:d??"",returnedStateCode:s??""}),new t.PrivyClientError({code:"pkce_state_code_mismatch",error:"Unexpected auth flow. This may be a phishing attempt."});let n=await this._privyInternal.fetch(e.OAuthLink,{body:{authorization_code:i,code_type:o,state_code:d,code_verifier:l}});await this._privyInternal.session.processOAuthTokens(n.oauth_tokens);let _=await this._privyInternal.refreshSession();return await Promise.all([this._storage.del(r.CODE_VERIFIER_KEY),this._storage.del(r.STATE_CODE_KEY)]),{user:_.user,identity_token:_.identity_token}}async unlink(t,r){await this._privyInternal.fetch(e.OAuthUnlink,{body:{provider:t,subject:r}});let i=await this._privyInternal.refreshSession();return{user:i.user,identity_token:i.identity_token}}constructor(e,t,r,i){this._privyInternal=e,this._embedded=t,this._storage=r,this._crypto=i}};