Skip to content

Commit

Permalink
Merge pull-request #503
Browse files Browse the repository at this point in the history
  • Loading branch information
moe-dev committed Feb 13, 2025
2 parents 0d2e286 + faa757c commit 7e44349
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-glasses-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@turnkey/sdk-react": patch
---

Patch EWK custom session lengths - previously not working as intended (defaulted to 15 minute sessions only) and fix the following useLocalStorage issue when compiling: [Error: useLocalStorage is a client-only hook]
1 change: 1 addition & 0 deletions packages/sdk-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ function AuthPage() {
googleEnabled: true,
appleEnabled: false,
facebookEnabled: false,
sessionLengthSeconds: 3600, //1 hour r/w session
};

const configOrder = ["socials", "email", "phone", "passkey"];
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@
"@turnkey/sdk-browser": "workspace:*",
"@turnkey/sdk-server": "workspace:*",
"@turnkey/wallet-stamper": "workspace:*",
"@uidotdev/usehooks": "^2.4.1",
"libphonenumber-js": "^1.11.14",
"next": "^15.0.2",
"react-apple-login": "^1.1.6",
"react-international-phone": "^4.3.0"
"react-international-phone": "^4.3.0",
"usehooks-ts": "^3.1.1"
},
"peerDependencies": {
"@types/react": "^18.2.75",
Expand Down
31 changes: 26 additions & 5 deletions packages/sdk-react/src/components/auth/Auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,16 @@ const Auth: React.FC<AuthProps> = ({
return suborgId;
};

const handleAuthSuccess = async (credentialBundle: any) => {
const handleAuthSuccess = async (
credentialBundle: any,
expirationSeconds?: string,
) => {
if (credentialBundle) {
await authIframeClient!.injectCredentialBundle(credentialBundle);
await authIframeClient!.loginWithAuthBundle(credentialBundle);
await authIframeClient!.loginWithAuthBundle(
credentialBundle,
expirationSeconds,
);
await onAuthSuccess();
}
};
Expand Down Expand Up @@ -223,12 +229,18 @@ const Auth: React.FC<AuthProps> = ({

const sessionResponse = await passkeyClient?.createReadWriteSession({
targetPublicKey: authIframeClient?.iframePublicKey!,
...(authConfig.sessionLengthSeconds !== undefined && {
expirationSeconds: authConfig.sessionLengthSeconds.toString(),
}),
organizationId:
turnkey?.config.defaultOrganizationId ??
process.env.NEXT_PUBLIC_ORGANIZATION_ID!,
});
if (sessionResponse?.credentialBundle) {
await handleAuthSuccess(sessionResponse.credentialBundle);
await handleAuthSuccess(
sessionResponse.credentialBundle,
authConfig.sessionLengthSeconds?.toString(),
);
} else {
setPasskeySignupError(authErrors.passkey.loginFailed);
}
Expand All @@ -241,13 +253,19 @@ const Auth: React.FC<AuthProps> = ({
try {
const sessionResponse = await passkeyClient?.createReadWriteSession({
targetPublicKey: authIframeClient?.iframePublicKey!,
...(authConfig.sessionLengthSeconds !== undefined && {
expirationSeconds: authConfig.sessionLengthSeconds.toString(),
}),
organizationId:
turnkey?.config.defaultOrganizationId ??
process.env.NEXT_PUBLIC_ORGANIZATION_ID!,
});

if (sessionResponse?.credentialBundle) {
await handleAuthSuccess(sessionResponse.credentialBundle);
await handleAuthSuccess(
sessionResponse.credentialBundle,
authConfig.sessionLengthSeconds?.toString(),
);
} else {
authErrors.passkey.loginFailed;
}
Expand Down Expand Up @@ -294,7 +312,10 @@ const Auth: React.FC<AuthProps> = ({
sessionLengthSeconds: authConfig.sessionLengthSeconds,
});
if (oauthResponse && oauthResponse.token) {
await handleAuthSuccess(oauthResponse!.token);
await handleAuthSuccess(
oauthResponse!.token,
authConfig.sessionLengthSeconds?.toString(),
);
} else {
onError(authErrors.oauth.loginFailed);
}
Expand Down
10 changes: 8 additions & 2 deletions packages/sdk-react/src/components/auth/OtpVerification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ interface OtpVerificationProps {
otpId: string;
authIframeClient: any;
sessionLengthSeconds?: number | undefined;
onValidateSuccess: (credentialBundle: any) => Promise<void>;
onValidateSuccess: (
credentialBundle: any,
expirationSeconds?: string,
) => Promise<void>;
onResendCode: (
type: FilterType.Email | FilterType.PhoneNumber,
value: string,
Expand Down Expand Up @@ -53,7 +56,10 @@ const OtpVerification: React.FC<OtpVerificationProps> = ({
});

if (authResponse?.token) {
await onValidateSuccess(authResponse.token);
await onValidateSuccess(
authResponse.token,
sessionLengthSeconds?.toString(),
);
} else {
setOtpError("Invalid code. Please try again.");
}
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk-react/src/hooks/use-session.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use client";

import { StorageKeys, User } from "@turnkey/sdk-browser";
import { useLocalStorage } from "@uidotdev/usehooks";
import { useLocalStorage } from "usehooks-ts";

interface UserSession {
user?: Omit<User, "session"> | undefined;
Expand Down
2 changes: 2 additions & 0 deletions packages/sdk-react/src/hooks/use-turnkey.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use client";

import { useContext } from "react";
import { TurnkeyContext } from "../contexts/TurnkeyContext";

Expand Down
27 changes: 13 additions & 14 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7e44349

Please sign in to comment.