Skip to content

Commit

Permalink
Allow overriding Session ID for Guest mode (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xFirekeeper authored Jan 17, 2025
1 parent b50e1ed commit 40e1d56
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
6 changes: 5 additions & 1 deletion Thirdweb/Thirdweb.Wallets/IThirdwebWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ public Task<string> RecoverAddressFromTypedDataV4<T, TDomain>(T data, TypedData<
/// <param name="chainId">The chain ID if linking an external wallet (SIWE).</param>
/// <param name="jwt">The JWT token if linking custom JWT auth.</param>
/// <param name="payload">The login payload if linking custom AuthEndpoint auth.</param>
/// <param name="defaultSessionIdOverride">The default session ID override if linking Guest auth.</param>
/// <param name="forceWalletIds">The wallet IDs to force display if linking using SiweExternal auth.</param>
/// <returns>A list of <see cref="LinkedAccount"/> objects.</returns>
public Task<List<LinkedAccount>> LinkAccount(
IThirdwebWallet walletToLink,
Expand All @@ -153,7 +155,9 @@ public Task<List<LinkedAccount>> LinkAccount(
IThirdwebBrowser browser = null,
BigInteger? chainId = null,
string jwt = null,
string payload = null
string payload = null,
string defaultSessionIdOverride = null,
List<string> forceWalletIds = null
);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,9 @@ public async Task<List<LinkedAccount>> LinkAccount(
IThirdwebBrowser browser = null,
BigInteger? chainId = null,
string jwt = null,
string payload = null
string payload = null,
string defaultSessionIdOverride = null,
List<string> forceWalletIds = null
)
{
if (!await this.IsConnected().ConfigureAwait(false))
Expand Down Expand Up @@ -496,11 +498,10 @@ public async Task<List<LinkedAccount>> LinkAccount(
serverRes = await ecosystemWallet.PreAuth_AuthEndpoint(payload).ConfigureAwait(false);
break;
case "Guest":
serverRes = await ecosystemWallet.PreAuth_Guest().ConfigureAwait(false);
serverRes = await ecosystemWallet.PreAuth_Guest(defaultSessionIdOverride).ConfigureAwait(false);
break;
case "SiweExternal":
// TODO: Allow enforcing wallet ids in linking flow?
serverRes = await ecosystemWallet.PreAuth_SiweExternal(isMobile ?? false, browserOpenAction, null, mobileRedirectScheme, browser).ConfigureAwait(false);
serverRes = await ecosystemWallet.PreAuth_SiweExternal(isMobile ?? false, browserOpenAction, forceWalletIds, mobileRedirectScheme, browser).ConfigureAwait(false);
break;
case "Google":
case "Apple":
Expand Down Expand Up @@ -827,7 +828,7 @@ public async Task<string> LoginWithBackend()

#region Guest

private async Task<Server.VerifyResult> PreAuth_Guest()
private async Task<Server.VerifyResult> PreAuth_Guest(string defaultSessionIdOverride = null)
{
var sessionData = this.EmbeddedWallet.GetSessionData();
string sessionId;
Expand All @@ -837,15 +838,15 @@ public async Task<string> LoginWithBackend()
}
else
{
sessionId = Guid.NewGuid().ToString();
sessionId = defaultSessionIdOverride ?? Guid.NewGuid().ToString();
}
var serverRes = await this.EmbeddedWallet.SignInWithGuestAsync(sessionId).ConfigureAwait(false);
return serverRes;
}

public async Task<string> LoginWithGuest()
public async Task<string> LoginWithGuest(string defaultSessionIdOverride = null)
{
var serverRes = await this.PreAuth_Guest().ConfigureAwait(false);
var serverRes = await this.PreAuth_Guest(defaultSessionIdOverride).ConfigureAwait(false);
return await this.PostAuth(serverRes).ConfigureAwait(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,9 @@ public virtual Task<List<LinkedAccount>> LinkAccount(
IThirdwebBrowser browser = null,
BigInteger? chainId = null,
string jwt = null,
string payload = null
string payload = null,
string defaultSessionIdOverride = null,
List<string> forceWalletIds = null
)
{
throw new InvalidOperationException("LinkAccount is not supported for private key wallets.");
Expand Down
8 changes: 6 additions & 2 deletions Thirdweb/Thirdweb.Wallets/SmartWallet/SmartWallet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,9 @@ public async Task<List<LinkedAccount>> LinkAccount(
IThirdwebBrowser browser = null,
BigInteger? chainId = null,
string jwt = null,
string payload = null
string payload = null,
string defaultSessionIdOverride = null,
List<string> forceWalletIds = null
)
{
var personalWallet = await this.GetPersonalWallet().ConfigureAwait(false);
Expand All @@ -1180,7 +1182,9 @@ public async Task<List<LinkedAccount>> LinkAccount(
}
else
{
return await personalWallet.LinkAccount(walletToLink, otp, isMobile, browserOpenAction, mobileRedirectScheme, browser, chainId, jwt, payload).ConfigureAwait(false);
return await personalWallet
.LinkAccount(walletToLink, otp, isMobile, browserOpenAction, mobileRedirectScheme, browser, chainId, jwt, payload, defaultSessionIdOverride, forceWalletIds)
.ConfigureAwait(false);
}
}

Expand Down

0 comments on commit 40e1d56

Please sign in to comment.