Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .changeset/flat-horses-battle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
'@clerk/clerk-expo': patch
---

Accept custom `redirectURL` for SSO callback via `startSSOFlow`

Usage:

```ts
await startSSOFlow({
strategy: "oauth_google",
redirectUrl: AuthSession.makeRedirectUri({
path: "dashboard"
}),
});
```
19 changes: 14 additions & 5 deletions packages/expo/src/hooks/useSSO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as WebBrowser from 'expo-web-browser';
import { errorThrower } from '../utils/errors';

export type StartSSOFlowParams = {
redirectUrl?: string;
unsafeMetadata?: SignUpUnsafeMetadata;
} & (
| {
Expand All @@ -19,7 +20,7 @@ export type StartSSOFlowParams = {

export type StartSSOFlowReturnType = {
createdSessionId: string | null;
authSessionResult?: WebBrowser.WebBrowserAuthSessionResult;
authSessionResult: WebBrowser.WebBrowserAuthSessionResult | null;
setActive?: SetActive;
signIn?: SignInResource;
signUp?: SignUpResource;
Expand All @@ -33,6 +34,7 @@ export function useSSO() {
if (!isSignInLoaded || !isSignUpLoaded) {
return {
createdSessionId: null,
authSessionResult: null,
signIn,
signUp,
setActive,
Expand All @@ -47,9 +49,11 @@ export function useSSO() {
* to include the `rotating_token_nonce` on SSO callback
* @ref https://clerk.com/docs/reference/backend-api/tag/Redirect-URLs#operation/CreateRedirectURL
*/
const redirectUrl = AuthSession.makeRedirectUri({
path: 'sso-callback',
});
const redirectUrl =
startSSOFlowParams.redirectUrl ??
AuthSession.makeRedirectUri({
path: 'sso-callback',
});
Comment on lines +52 to +56

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❓ Should redirectUrl be any string ? Should we be more strict and do

Suggested change
const redirectUrl =
startSSOFlowParams.redirectUrl ??
AuthSession.makeRedirectUri({
path: 'sso-callback',
});
const redirectUrl =
AuthSession.makeRedirectUri({
path: startSSOFlowParams.redirectUrl ?? 'sso-callback',
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are cases where developers might want to have more flexibility here, such as:

Directly passing the scheme as a property, even tho Expo inherits from the current build:

startSSOFlow({ 
  redirectUrl: AuthSession.makeRedirectUri({
    scheme: "myapp",
  });
})

Deep-link to the current route, without passing a path:

startSSOFlow({ 
  redirectUrl:
    // Defaults to the current route
   AuthSession.makeRedirectUri();
})

Or using the expo-linking package:

startSSOFlow({ 
  redirectUrl: Linking.createURL("dashboard")
})


await signIn.create({
strategy,
Expand All @@ -62,13 +66,17 @@ export function useSSO() {
return errorThrower.throw('Missing external verification redirect URL for SSO flow');
}

const authSessionResult = await WebBrowser.openAuthSessionAsync(externalVerificationRedirectURL.toString());
const authSessionResult = await WebBrowser.openAuthSessionAsync(
externalVerificationRedirectURL.toString(),
redirectUrl,
);
if (authSessionResult.type !== 'success' || !authSessionResult.url) {
return {
createdSessionId: null,
setActive,
signIn,
signUp,
authSessionResult,
};
}

Expand All @@ -89,6 +97,7 @@ export function useSSO() {
setActive,
signIn,
signUp,
authSessionResult,
};
}

Expand Down