Skip to content

Commit

Permalink
Refactor implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
LauraBeatris committed Jan 14, 2025
1 parent 0cfdb3a commit 57062cf
Showing 1 changed file with 30 additions and 29 deletions.
59 changes: 30 additions & 29 deletions packages/expo/src/hooks/useSso.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,33 @@ export type StartSSOParams = {
};

export type StartSSOFlowReturnType = {
createdSessionId: string;
/**
* Session ID created upon sign-in completion, or null if incomplete.
* If incomplete, use signIn or signUp for next steps like MFA.
*/
createdSessionId: string | null;
setActive?: SetActive;
signIn?: SignInResource;
signUp?: SignUpResource;
authSessionResult?: WebBrowser.WebBrowserAuthSessionResult;
};

export function useSSO(useSSOParams: UseSSOParams) {
const { strategy } = useSSOParams || {};
if (!strategy) {
return errorThrower.throw('Missing strategy');
}

const { signIn, setActive, isLoaded: isSignInLoaded } = useSignIn();
const { signUp, isLoaded: isSignUpLoaded } = useSignUp();

async function startFlow(startSSOFlowParams?: StartSSOParams): Promise<StartSSOFlowReturnType> {
if (!isSignInLoaded || !isSignUpLoaded) {
return {
createdSessionId: '',
createdSessionId: null,
signIn,
signUp,
setActive,
};
}

let createdSessionId = signIn.createdSessionId;

// Create a redirect url for the current platform and environment.
//
// This redirect URL needs to be whitelisted for your Clerk production instance via
Expand All @@ -58,53 +59,53 @@ export function useSSO(useSSOParams: UseSSOParams) {
path: 'sso-native-callback',
});

await signIn.create({ strategy, redirectUrl: oauthRedirectUrl, identifier: startSSOFlowParams?.identifier });
await signIn.create({
strategy: useSSOParams.strategy,
redirectUrl: oauthRedirectUrl,
identifier: startSSOFlowParams?.identifier,
});

const { externalVerificationRedirectURL } = signIn.firstFactorVerification;

if (!externalVerificationRedirectURL) {
return errorThrower.throw('Missing external verification redirect URL for SSO flow');
return errorThrower.throw(
'Missing external verification redirect URL for SSO flow. This indicates an API issue - please contact support for assistance.',
);
}

// TODO - Add unit tests to handle different results
const authSessionResult = await WebBrowser.openAuthSessionAsync(
externalVerificationRedirectURL.toString(),
oauthRedirectUrl,
);

// @ts-expect-error
const { type, url } = authSessionResult || {};

// TODO: Check all the possible AuthSession results
// https://docs.expo.dev/versions/latest/sdk/auth-session/#returns-7
if (type !== 'success') {
if (authSessionResult.type !== 'success' || !authSessionResult.url) {
return {
authSessionResult,
createdSessionId: '',
createdSessionId,
setActive,
signIn,
signUp,
};
}

const params = new URL(url).searchParams;
const params = new URL(authSessionResult.url).searchParams;
const rotatingTokenNonce = params.get('rotating_token_nonce');
if (!rotatingTokenNonce) {
return errorThrower.throw(
'Missing rotating_token_nonce in SSO callback. This indicates an API issue - please contact support for assistance.',
);
}

const rotatingTokenNonce = params.get('rotating_token_nonce') || '';
// TODO - Add unit tests for transferable flow
await signIn.reload({ rotatingTokenNonce });

const { status, firstFactorVerification } = signIn;

let createdSessionId = '';

if (status === 'complete') {
createdSessionId = signIn.createdSessionId!;
} else if (firstFactorVerification.status === 'transferable') {
if (signIn.firstFactorVerification.status === 'transferable') {
await signUp.create({
transfer: true,
unsafeMetadata: startSSOFlowParams?.unsafeMetadata || useSSOParams.unsafeMetadata,
});
createdSessionId = signUp.createdSessionId || '';
createdSessionId = signUp.createdSessionId;
}

// TODO - Add unit tests for when createdSessionId is null -> missing factor cases
return {
authSessionResult,
createdSessionId,
Expand Down

0 comments on commit 57062cf

Please sign in to comment.