diff --git a/.changeset/moody-clocks-carry.md b/.changeset/moody-clocks-carry.md new file mode 100644 index 000000000..a265cf6c2 --- /dev/null +++ b/.changeset/moody-clocks-carry.md @@ -0,0 +1,5 @@ +--- +"@turnkey/sdk-browser": minor +--- + +Fix readWrite session to use credentialBundle and add loginWithAuthBundle to create a session when you already have a credentialBundle diff --git a/packages/sdk-browser/src/sdk-client.ts b/packages/sdk-browser/src/sdk-client.ts index 239f30977..e94ad9320 100644 --- a/packages/sdk-browser/src/sdk-client.ts +++ b/packages/sdk-browser/src/sdk-client.ts @@ -279,9 +279,9 @@ export class TurnkeyBrowserClient extends TurnkeySDKClientBase { * To be used in conjunction with an `iframeStamper`: the resulting session's credential bundle can be * injected into an iframeStamper to create a session that enables both read and write requests. * - * @param email * @param targetEmbeddedKey * @param expirationSeconds + * @param userId * @returns {Promise} */ loginWithReadWriteSession = async ( @@ -298,7 +298,7 @@ export class TurnkeyBrowserClient extends TurnkeySDKClientBase { // Ensure session and sessionExpiry are included in the object const readWriteSessionResultWithSession = { ...readWriteSessionResult, - session: readWriteSessionResult.credentialBundle, + credentialBundle: readWriteSessionResult.credentialBundle, sessionExpiry: Date.now() + Number(expirationSeconds) * 1000, }; @@ -307,6 +307,32 @@ export class TurnkeyBrowserClient extends TurnkeySDKClientBase { return readWriteSessionResultWithSession; }; + + /** + * Logs in with an existing auth bundle. this bundle enables both read and write requests. + * + * @param credentialBundle + * @param expirationSeconds + * @returns {Promise} + */ + loginWithAuthBundle = async ( + credentialBundle: string, + expirationSeconds: string = DEFAULT_SESSION_EXPIRATION + ): Promise => { + try { + const whoAmIResult = await this.getWhoami(); + + const readWriteSessionResultWithSession = { + ...whoAmIResult, + credentialBundle: credentialBundle, + sessionExpiry: Date.now() + Number(expirationSeconds) * 1000, + }; + await saveSession(readWriteSessionResultWithSession, this.authClient); + return true; + } catch { + return false; + } + }; } export class TurnkeyPasskeyClient extends TurnkeyBrowserClient {