Skip to content

Commit

Permalink
Merge pull-request #449
Browse files Browse the repository at this point in the history
  • Loading branch information
moe-dev committed Dec 12, 2024
2 parents 433070d + 7988bc1 commit bd324bb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-clocks-carry.md
Original file line number Diff line number Diff line change
@@ -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
30 changes: 28 additions & 2 deletions packages/sdk-browser/src/sdk-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SdkApiTypes.TCreateReadWriteSessionResponse>}
*/
loginWithReadWriteSession = async (
Expand All @@ -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,
};

Expand All @@ -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<boolean>}
*/
loginWithAuthBundle = async (
credentialBundle: string,
expirationSeconds: string = DEFAULT_SESSION_EXPIRATION
): Promise<any> => {
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 {
Expand Down

0 comments on commit bd324bb

Please sign in to comment.