Skip to content

Commit e1eab89

Browse files
Add basic tests, mocks and helpers for webauthn signup
1 parent caddefe commit e1eab89

24 files changed

+3908
-1073
lines changed

examples/for-tests/src/App.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Footer from "./Footer";
88
import SuperTokens from "supertokens-auth-react";
99
import EmailVerification from "supertokens-auth-react/recipe/emailverification";
1010
import EmailPassword from "supertokens-auth-react/recipe/emailpassword";
11+
import Webauthn from "supertokens-auth-react/recipe/webauthn";
1112
import Passwordless from "supertokens-auth-react/recipe/passwordless";
1213
import ThirdParty from "supertokens-auth-react/recipe/thirdparty";
1314
import Multitenancy from "supertokens-auth-react/recipe/multitenancy";
@@ -427,6 +428,9 @@ if (enabledRecipes.includes("emailpassword") || enabledRecipes.includes("thirdpa
427428
if (enabledRecipes.includes("passwordless") || enabledRecipes.includes("thirdpartypasswordless")) {
428429
recipeList = [getPasswordlessConfigs(testContext), ...recipeList];
429430
}
431+
if (enabledRecipes.includes("webauthn")) {
432+
recipeList = [getWebauthnConfigs(), ...recipeList];
433+
}
430434

431435
if (emailVerificationMode !== "OFF") {
432436
recipeList.push(getEmailVerificationConfigs(testContext));
@@ -1147,6 +1151,65 @@ function setIsNewUserToStorage(recipeName, isNewRecipeUser) {
11471151
localStorage.setItem("isNewUserCheck", `${recipeName}-${isNewRecipeUser}`);
11481152
}
11491153

1154+
function getWebauthnConfigs() {
1155+
return Webauthn.init({
1156+
style: `
1157+
[data-supertokens~=container] {
1158+
font-family: cursive;
1159+
}
1160+
`,
1161+
override: {
1162+
functions: (implementation) => {
1163+
const log = logWithPrefix(`ST_LOGS WEBAUTHN OVERRIDE`);
1164+
1165+
return {
1166+
...implementation,
1167+
getRegisterOptions(...args) {
1168+
log(`GET REGISTER OPTIONS`);
1169+
return implementation.getRegisterOptions(...args);
1170+
},
1171+
registerCredentialWithSignUp(...args) {
1172+
log(`GET REGISTER OPTIONS WITH SIGN UP`);
1173+
1174+
// We are mocking the popup since it's not possible to
1175+
// test the webauthn popup.
1176+
return {
1177+
status: "OK",
1178+
user: {},
1179+
fetchResponse: {},
1180+
};
1181+
},
1182+
};
1183+
},
1184+
},
1185+
preAPIHook: async (context) => {
1186+
if (localStorage.getItem(`SHOW_GENERAL_ERROR`)?.includes(context.action)) {
1187+
let errorFromStorage = localStorage.getItem("TRANSLATED_GENERAL_ERROR");
1188+
1189+
if (context.action === "EMAIL_EXISTS") {
1190+
context.url += "&generalError=true";
1191+
} else {
1192+
let jsonBody = JSON.parse(context.requestInit.body);
1193+
jsonBody = {
1194+
...jsonBody,
1195+
generalError: true,
1196+
generalErrorMessage: errorFromStorage === null ? undefined : errorFromStorage,
1197+
};
1198+
context.requestInit.body = JSON.stringify(jsonBody);
1199+
}
1200+
}
1201+
console.log(`ST_LOGS WEBAUTHN PRE_API_HOOKS ${context.action}`);
1202+
return context;
1203+
},
1204+
getRedirectionURL: async (context) => {
1205+
console.log(`ST_LOGS WEBAUTHN GET_REDIRECTION_URL ${context.action}`);
1206+
},
1207+
onHandleEvent: async (context) => {
1208+
console.log(`ST_LOGS WEBAUTHN ON_HANDLE_EVENT ${context.action}`);
1209+
},
1210+
});
1211+
}
1212+
11501213
window.SuperTokens = SuperTokens;
11511214
window.Session = Session;
11521215
window.UserRoleClaim = UserRoles.UserRoleClaim;

examples/for-tests/src/AppWithReactDomRouter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ResetPasswordUsingToken } from "supertokens-auth-react/recipe/emailpass
44
import { SessionAuth } from "supertokens-auth-react/recipe/session";
55
import { getSuperTokensRoutesForReactRouterDom, AuthPage } from "supertokens-auth-react/ui";
66
import { EmailPasswordPreBuiltUI } from "supertokens-auth-react/recipe/emailpassword/prebuiltui";
7+
import { WebauthnPreBuiltUI } from "supertokens-auth-react/recipe/webauthn/prebuiltui";
78
import { PasswordlessPreBuiltUI } from "supertokens-auth-react/recipe/passwordless/prebuiltui";
89
import { EmailVerificationPreBuiltUI } from "supertokens-auth-react/recipe/emailverification/prebuiltui";
910
import { ThirdPartyPreBuiltUI, SignInAndUpCallback } from "supertokens-auth-react/recipe/thirdparty/prebuiltui";
@@ -42,6 +43,9 @@ function AppWithReactDomRouter(props) {
4243
if (enabledRecipes.some((r) => r.endsWith("passwordless"))) {
4344
recipePreBuiltUIList.push(PasswordlessPreBuiltUI);
4445
}
46+
if (enabledRecipes.some((r) => r.endsWith("webauthn"))) {
47+
recipePreBuiltUIList.push(WebauthnPreBuiltUI);
48+
}
4549
if (emailVerificationMode !== "OFF") {
4650
recipePreBuiltUIList.push(EmailVerificationPreBuiltUI);
4751
}

examples/for-tests/src/AppWithoutRouter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { EmailPasswordPreBuiltUI } from "supertokens-auth-react/recipe/emailpass
55
import { PasswordlessPreBuiltUI } from "supertokens-auth-react/recipe/passwordless/prebuiltui";
66
import { ThirdPartyPreBuiltUI } from "supertokens-auth-react/recipe/thirdparty/prebuiltui";
77
import { EmailVerificationPreBuiltUI } from "supertokens-auth-react/recipe/emailverification/prebuiltui";
8+
import { WebauthnPreBuiltUI } from "supertokens-auth-react/recipe/webauthn/prebuiltui";
89
import { getEnabledRecipes } from "./testContext";
910

1011
function AppWithoutRouter() {
@@ -32,6 +33,9 @@ function Routing() {
3233
if (enabledRecipes.some((r) => r.endsWith("passwordless"))) {
3334
recipePreBuiltUIList.push(PasswordlessPreBuiltUI);
3435
}
36+
if (enabledRecipes.some((r) => r.endsWith("webauthn"))) {
37+
recipePreBuiltUIList.push(WebauthnPreBuiltUI);
38+
}
3539

3640
if (emailVerificationMode !== "OFF") {
3741
recipePreBuiltUIList.push(EmailVerificationPreBuiltUI);

examples/for-tests/src/testContext.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function getEnabledRecipes() {
4141
"thirdpartyemailpassword",
4242
"passwordless",
4343
"thirdpartypasswordless",
44+
"webauthn",
4445
];
4546
} else if (testContext.clientRecipeListForDynamicLogin) {
4647
enabledRecipes = JSON.parse(testContext.clientRecipeListForDynamicLogin);
@@ -51,6 +52,7 @@ export function getEnabledRecipes() {
5152
"thirdpartyemailpassword",
5253
"passwordless",
5354
"thirdpartypasswordless",
55+
"webauthn",
5456
];
5557
} else {
5658
if (testContext.authRecipe === "both") {

lib/build/emailpassword-shared3.js

Lines changed: 5 additions & 99 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)