Skip to content

Commit f81fa58

Browse files
Add mocks for webauthn sign in and in react 16 examples
1 parent 099ae3b commit f81fa58

File tree

6 files changed

+148
-0
lines changed

6 files changed

+148
-0
lines changed

examples/for-tests-react-16/src/App.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import UserRoles from "supertokens-auth-react/recipe/userroles";
1515
import Multitenancy from "supertokens-auth-react/recipe/multitenancy";
1616
import MultiFactorAuth from "supertokens-auth-react/recipe/multifactorauth";
1717
import TOTP from "supertokens-auth-react/recipe/totp";
18+
import STGeneralError from "supertokens-web-js/lib/build/error";
19+
import Webauthn from "supertokens-auth-react/recipe/webauthn";
1820

1921
import axios from "axios";
2022
import { useSessionContext } from "supertokens-auth-react/recipe/session";
@@ -260,6 +262,9 @@ if (enabledRecipes.includes("emailpassword") || enabledRecipes.includes("thirdpa
260262
if (enabledRecipes.includes("passwordless") || enabledRecipes.includes("thirdpartypasswordless")) {
261263
recipeList = [getPasswordlessConfigs(testContext), ...recipeList];
262264
}
265+
if (enabledRecipes.includes("webauthn")) {
266+
recipeList = [getWebauthnConfigs(testContext), ...recipeList];
267+
}
263268
if (emailVerificationMode !== "OFF") {
264269
recipeList.push(getEmailVerificationConfigs(testContext));
265270
}
@@ -885,6 +890,106 @@ function getThirdPartyConfigs({ staticProviderList, disableDefaultUI, thirdParty
885890
});
886891
}
887892

893+
function getWebauthnConfigs({ throwWebauthnError, webauthnErrorStatus }) {
894+
return Webauthn.init({
895+
style: `
896+
[data-supertokens~=container] {
897+
font-family: cursive;
898+
}
899+
`,
900+
override: {
901+
functions: (implementation) => {
902+
const log = logWithPrefix(`ST_LOGS WEBAUTHN OVERRIDE`);
903+
904+
return {
905+
...implementation,
906+
getRegisterOptions(...args) {
907+
log(`GET REGISTER OPTIONS`);
908+
return implementation.getRegisterOptions(...args);
909+
},
910+
getSignInOptions(...args) {
911+
log(`GET SIGN IN OPTIONS`);
912+
return implementation.getSignInOptions(...args);
913+
},
914+
signIn(...args) {
915+
log(`SIGN IN`);
916+
return implementation.signIn(...args);
917+
},
918+
registerCredentialWithSignUp(...args) {
919+
log(`GET REGISTER OPTIONS WITH SIGN UP`);
920+
921+
// We will throw an error if it is asked for.
922+
if (throwWebauthnError) {
923+
throw new STGeneralError("TEST ERROR");
924+
}
925+
926+
// Return error status if the user passed that.
927+
if (webauthnErrorStatus) {
928+
return {
929+
status: webauthnErrorStatus,
930+
};
931+
}
932+
933+
// We are mocking the popup since it's not possible to
934+
// test the webauthn popup.
935+
return {
936+
status: "OK",
937+
user: {},
938+
fetchResponse: {},
939+
};
940+
},
941+
authenticateCredentialWithSignIn(...args) {
942+
log(`AUTHENTICATE CREDENTIAL WITH SIGN IN`);
943+
944+
// We will throw an error if it is asked for.
945+
if (throwWebauthnError) {
946+
throw new STGeneralError("TEST ERROR");
947+
}
948+
949+
// Return error status if the user passed that.
950+
if (webauthnErrorStatus) {
951+
return {
952+
status: webauthnErrorStatus,
953+
};
954+
}
955+
956+
return {
957+
status: "OK",
958+
user: {},
959+
fetchResponse: {},
960+
};
961+
},
962+
};
963+
},
964+
},
965+
preAPIHook: async (context) => {
966+
if (localStorage.getItem(`SHOW_GENERAL_ERROR`)?.includes(context.action)) {
967+
let errorFromStorage = localStorage.getItem("TRANSLATED_GENERAL_ERROR");
968+
969+
if (context.action === "EMAIL_EXISTS") {
970+
context.url += "&generalError=true";
971+
} else {
972+
let jsonBody = JSON.parse(context.requestInit.body);
973+
jsonBody = {
974+
...jsonBody,
975+
generalError: true,
976+
generalErrorMessage: errorFromStorage === null ? undefined : errorFromStorage,
977+
};
978+
context.requestInit.body = JSON.stringify(jsonBody);
979+
}
980+
}
981+
console.log(`ST_LOGS WEBAUTHN PRE_API_HOOKS ${context.action}`);
982+
return context;
983+
},
984+
getRedirectionURL: async (context) => {
985+
console.log(`ST_LOGS WEBAUTHN GET_REDIRECTION_URL ${context.action}`);
986+
},
987+
onHandleEvent: async (context) => {
988+
console.log(`ST_LOGS WEBAUTHN ON_HANDLE_EVENT ${context.action}`);
989+
},
990+
});
991+
}
992+
888993
function setIsNewUserToStorage(recipeName, isNewRecipeUser) {
889994
localStorage.setItem("isNewUserCheck", `${recipeName}-${isNewRecipeUser}`);
890995
}

examples/for-tests-react-16/src/AppWithReactDomRouter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { MultiFactorAuthPreBuiltUI } from "supertokens-auth-react/recipe/multifa
1212
import { TOTPPreBuiltUI } from "supertokens-auth-react/recipe/totp/prebuiltui";
1313
import { AccessDeniedScreen } from "supertokens-auth-react/recipe/session/prebuiltui";
1414
import { getEnabledRecipes, getTestContext } from "./testContext";
15+
import { WebauthnPreBuiltUI } from "supertokens-auth-react/recipe/webauthn/prebuiltui";
1516

1617
function AppWithReactDomRouter(props) {
1718
const context = getTestContext();
@@ -28,6 +29,9 @@ function AppWithReactDomRouter(props) {
2829
if (enabledRecipes.some((r) => r.endsWith("passwordless"))) {
2930
recipePreBuiltUIList.push(PasswordlessPreBuiltUI);
3031
}
32+
if (enabledRecipes.some((r) => r.endsWith("webauthn"))) {
33+
recipePreBuiltUIList.push(WebauthnPreBuiltUI);
34+
}
3135

3236
if (emailVerificationMode !== "OFF") {
3337
recipePreBuiltUIList.push(EmailVerificationPreBuiltUI);

examples/for-tests-react-16/src/AppWithReactDomRouterV5.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { AccessDeniedScreen } from "supertokens-auth-react/recipe/session/prebui
1111
import { MultiFactorAuthPreBuiltUI } from "supertokens-auth-react/recipe/multifactorauth/prebuiltui";
1212
import { TOTPPreBuiltUI } from "supertokens-auth-react/recipe/totp/prebuiltui";
1313
import { getEnabledRecipes, getTestContext } from "./testContext";
14+
import { WebauthnPreBuiltUI } from "supertokens-auth-react/recipe/webauthn/prebuiltui";
1415

1516
function AppWithReactDomRouter(props) {
1617
const context = getTestContext();
@@ -27,6 +28,9 @@ function AppWithReactDomRouter(props) {
2728
if (enabledRecipes.some((r) => r.endsWith("passwordless"))) {
2829
recipePreBuiltUIList.push(PasswordlessPreBuiltUI);
2930
}
31+
if (enabledRecipes.some((r) => r.endsWith("webauthn"))) {
32+
recipePreBuiltUIList.push(WebauthnPreBuiltUI);
33+
}
3034
if (emailVerificationMode !== "OFF") {
3135
recipePreBuiltUIList.push(EmailVerificationPreBuiltUI);
3236
}

examples/for-tests-react-16/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-react-16/src/testContext.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ export function getTestContext() {
2424
signoutOnSessionNotExists: localStorage.getItem("signoutOnSessionNotExists") === "true",
2525
disableRedirectionAfterSuccessfulSignInUp:
2626
localStorage.getItem("disableRedirectionAfterSuccessfulSignInUp") === "true",
27+
throwWebauthnError: localStorage.getItem("throwWebauthnError") === "true",
28+
webauthnErrorStatus: localStorage.getItem("webauthnErrorStatus") || undefined,
2729
};
2830
return ret;
2931
}

examples/for-tests/src/App.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,14 @@ function getWebauthnConfigs({ throwWebauthnError, webauthnErrorStatus }) {
11691169
log(`GET REGISTER OPTIONS`);
11701170
return implementation.getRegisterOptions(...args);
11711171
},
1172+
getSignInOptions(...args) {
1173+
log(`GET SIGN IN OPTIONS`);
1174+
return implementation.getSignInOptions(...args);
1175+
},
1176+
signIn(...args) {
1177+
log(`SIGN IN`);
1178+
return implementation.signIn(...args);
1179+
},
11721180
registerCredentialWithSignUp(...args) {
11731181
log(`GET REGISTER OPTIONS WITH SIGN UP`);
11741182

@@ -1192,6 +1200,27 @@ function getWebauthnConfigs({ throwWebauthnError, webauthnErrorStatus }) {
11921200
fetchResponse: {},
11931201
};
11941202
},
1203+
authenticateCredentialWithSignIn(...args) {
1204+
log(`AUTHENTICATE CREDENTIAL WITH SIGN IN`);
1205+
1206+
// We will throw an error if it is asked for.
1207+
if (throwWebauthnError) {
1208+
throw new STGeneralError("TEST ERROR");
1209+
}
1210+
1211+
// Return error status if the user passed that.
1212+
if (webauthnErrorStatus) {
1213+
return {
1214+
status: webauthnErrorStatus,
1215+
};
1216+
}
1217+
1218+
return {
1219+
status: "OK",
1220+
user: {},
1221+
fetchResponse: {},
1222+
};
1223+
},
11951224
};
11961225
},
11971226
},

0 commit comments

Comments
 (0)