Skip to content
Merged
Prev Previous commit
Next Next commit
custom jwt working
  • Loading branch information
ikethirdweb committed Oct 19, 2023
commit 6aefb6aa0e450d9c0d04875ec3e66b803ba5b05b
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export function ChooseWallet({

const guestWallet = wallets.find((w) => w.id === walletIds.localWallet);
const emailWallet = wallets.find(
(w) => w.id === walletIds.magicLink || w.id === walletIds.embeddedWallet,
(w) =>
w.id === walletIds.magicLink ||
(w.id === walletIds.embeddedWallet && w.selectUI),
);
const connectionWallets = wallets
.filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import {
cognitoEmailSignIn,
cognitoEmailSignUp,
} from "./helpers/auth/cognitoAuth";
import { postPaperAuth, prePaperAuth } from "./helpers/auth/middleware";
import {
postPaperAuth,
postPaperAuthUserManaged,
prePaperAuth,
} from "./helpers/auth/middleware";
import { isDeviceSharePresentForUser } from "./helpers/storage/local";
import { getCognitoUser, setCognitoUser } from "./helpers/storage/state";
import { SendEmailOtpReturnType } from "@thirdweb-dev/wallets";
Expand Down Expand Up @@ -201,7 +205,7 @@ export async function socialLogin(oauthOptions: OauthOption, clientId: string) {
}

export async function customJwt(authOptions: AuthOptions, clientId: string) {
const { jwtToken } = authOptions;
const { jwtToken, encryptionKey } = authOptions;

const resp = await fetch(ROUTE_AUTH_JWT_CALLBACK, {
method: "POST",
Expand Down Expand Up @@ -233,7 +237,7 @@ export async function customJwt(authOptions: AuthOptions, clientId: string) {
isNewUser: verifiedToken.isNewUser,
};

await postPaperAuth(toStoreToken, clientId);
await postPaperAuthUserManaged(toStoreToken, clientId, encryptionKey);

return { verifiedToken, email: verifiedToken.authDetails.email };
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,45 @@ export async function postPaperAuth(

return storedToken;
}

export async function postPaperAuthUserManaged(
storedToken: AuthStoredTokenWithCookieReturnType["storedToken"],
clientId: string,
encryptionKey: string,
) {
if (storedToken.shouldStoreCookieString) {
await setAuthTokenClient(storedToken.cookieString, clientId);
}

await setWallerUserDetails({
clientId,
userId: storedToken.authDetails.userWalletId,
email: storedToken.authDetails.email,
});

if (storedToken.isNewUser) {
console.log("========== New User ==========");
await setUpNewUserWallet(encryptionKey, clientId);
} else {
try {
// existing device share
await getDeviceShare(clientId);
console.log("========== Existing user with device share ==========");
} catch (e) {
// trying to recreate device share from recovery code to derive wallet
console.log("========== Existing user on new device ==========");

try {
await setUpShareForNewDevice({
clientId,
recoveryCode: encryptionKey,
});
} catch (error) {
console.error("Error setting up wallet on device", error);
throw error;
}
}
}

return storedToken;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ import type {
AuthProvider,
} from "@paperxyz/embedded-wallet-service-sdk";

export type CustomAuth = {
jwtToken: string;
encryptionKey: string;
};

export type OauthOptions = {
providers: AuthProvider[];
redirectUrl: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ export const EmailSelectionUI: React.FC<
SelectUIProps<EmbeddedWallet> & {
oauthOptions?: OauthOptions;
email?: boolean;
custom_auth?: boolean;
}
> = ({ onSelect, walletConfig, oauthOptions, email }) => {
> = ({ onSelect, walletConfig, oauthOptions, email, custom_auth }) => {
const theme = useGlobalTheme();
const [emailInput, setEmailInput] = useState<string>("");
const [errorMessage, setErrorMessage] = useState<string>("");
Expand All @@ -41,6 +42,11 @@ export const EmailSelectionUI: React.FC<
setEmailWallet(emailWalletInstance);
}, [createWalletInstance, walletConfig]);

if (custom_auth) {
// No UI for custom auth
return null;
}

const validateEmail = (emailToValidate: string) => {
const pattern = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
return pattern.test(emailToValidate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export type EmbeddedWalletConfig = {
redirectUrl: string;
}
| false;

custom_auth?: boolean;
};

export const embeddedWallet = (
Expand All @@ -36,6 +38,7 @@ export const embeddedWallet = (
}
: undefined
}
custom_auth={config?.custom_auth}
// you cannot disable both email and oauth
email={!config?.oauthOptions && !config?.email ? true : config?.email}
/>
Expand All @@ -50,7 +53,7 @@ export const embeddedWallet = (
clientId: options.clientId || "",
});
},
selectUI: selectUI,
selectUI: config?.custom_auth ? undefined : selectUI,
connectUI: EmbeddedConnectionUI,
};
};