Skip to content

Commit

Permalink
feat: handle not initialized OAuth2Provider recipe more gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed Sep 28, 2024
1 parent fb841f1 commit 6c7ff10
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 64 deletions.
79 changes: 50 additions & 29 deletions lib/build/index2.js

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

21 changes: 13 additions & 8 deletions lib/build/thirdpartyprebuiltui.js

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

59 changes: 36 additions & 23 deletions lib/ts/recipe/authRecipe/components/feature/authPage/authPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
getTenantIdFromQueryParams,
mergeObjects,
updateQueryParam,
useOnMountAPICall,
useRethrowInRender,
} from "../../../../../utils";
import MultiFactorAuth from "../../../../multifactorauth/recipe";
Expand Down Expand Up @@ -174,17 +175,30 @@ const AuthPageInner: React.FC<AuthPageProps> = (props) => {
);
}, [loadedDynamicLoginMethods, setLoadedDynamicLoginMethods]);

useEffect(() => {
if (oauth2ClientInfo) {
return;
}
const oauth2Recipe = OAuth2Provider.getInstance();
if (oauth2Recipe !== undefined && loginChallenge !== null) {
void OAuth2Provider.getInstanceOrThrow()
.webJSRecipe.getLoginChallengeInfo({ loginChallenge, userContext })
.then(({ info }) => setOAuth2ClientInfo(info));
useOnMountAPICall(
async () => {
if (oauth2ClientInfo) {
return;
}
const oauth2Recipe = OAuth2Provider.getInstance();
if (oauth2Recipe !== undefined && loginChallenge !== null) {
return oauth2Recipe.webJSRecipe.getLoginChallengeInfo({ loginChallenge, userContext });
}
return undefined;
},
async (info) => {
if (info !== undefined) {
setOAuth2ClientInfo(info.info);
}
},
() => {
return SuperTokens.getInstanceOrThrow().redirectToAuth({
navigate: props.navigate,
redirectBack: false,
userContext,
});
}
}, [setOAuth2ClientInfo, loginChallenge, oauth2ClientInfo]);
);

useEffect(() => {
if (sessionLoadedAndNotRedirecting) {
Expand All @@ -202,15 +216,14 @@ const AuthPageInner: React.FC<AuthPageProps> = (props) => {
Session.getInstanceOrThrow().config.onHandleEvent({
action: "SESSION_ALREADY_EXISTS",
});
if (loginChallenge !== null) {
const oauth2Recipe = OAuth2Provider.getInstance();
if (loginChallenge !== null && oauth2Recipe !== undefined) {
(async function () {
const { frontendRedirectTo } =
await OAuth2Provider.getInstanceOrThrow().webJSRecipe.getRedirectURLToContinueOAuthFlow(
{
loginChallenge,
userContext,
}
);
await oauth2Recipe.webJSRecipe.getRedirectURLToContinueOAuthFlow({
loginChallenge,
userContext,
});
return Session.getInstanceOrThrow().validateGlobalClaimsAndHandleSuccessRedirection(
{
action: "SUCCESS_OAUTH2",
Expand Down Expand Up @@ -299,7 +312,8 @@ const AuthPageInner: React.FC<AuthPageProps> = (props) => {

const onAuthSuccess = useCallback(
async (ctx: AuthSuccessContext) => {
if (loginChallenge === null) {
const oauth2Recipe = OAuth2Provider.getInstance();
if (loginChallenge === null || oauth2Recipe === undefined) {
return Session.getInstanceOrThrow().validateGlobalClaimsAndHandleSuccessRedirection(
{
...ctx,
Expand All @@ -313,11 +327,10 @@ const AuthPageInner: React.FC<AuthPageProps> = (props) => {
props.navigate
);
}
const { frontendRedirectTo } =
await OAuth2Provider.getInstanceOrThrow().webJSRecipe.getRedirectURLToContinueOAuthFlow({
loginChallenge,
userContext,
});
const { frontendRedirectTo } = await oauth2Recipe.webJSRecipe.getRedirectURLToContinueOAuthFlow({
loginChallenge,
userContext,
});
return Session.getInstanceOrThrow().validateGlobalClaimsAndHandleSuccessRedirection(
{
...ctx,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@ const SignInAndUpCallback: React.FC<PropType> = (props) => {
const redirectToPath = stateResponse === undefined ? undefined : stateResponse.redirectToPath;
const loginChallenge = stateResponse?.oauth2LoginChallenge;

if (loginChallenge !== undefined) {
const oauth2Recipe = OAuth2Provider.getInstance();
if (loginChallenge !== undefined && oauth2Recipe !== undefined) {
try {
const { frontendRedirectTo } =
await OAuth2Provider.getInstanceOrThrow().webJSRecipe.getRedirectURLToContinueOAuthFlow({
const { frontendRedirectTo } = await oauth2Recipe.webJSRecipe.getRedirectURLToContinueOAuthFlow(
{
loginChallenge,
userContext,
});
}
);
return Session.getInstanceOrThrow().validateGlobalClaimsAndHandleSuccessRedirection(
{
action: "SUCCESS_OAUTH2",
Expand Down

0 comments on commit 6c7ff10

Please sign in to comment.