Skip to content

fix(clerk-js,types): Use intent parameter to reload resource #5553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 8, 2025

Conversation

dstaley
Copy link
Member

@dstaley dstaley commented Apr 7, 2025

Description

This PR introduces a new URL parameter, intent, used when performing SSO login via popup to indicate which resource needs to be refreshed before handling the redirect callback. The intent parameter is appended to the redirectUrl passed to FAPI such that the popup flow redirects to the redirectUrl with the intent parameter set to the resource that needs to be reloaded. This is passed to a new reloadResource optional parameter on the handleRedirectCallback method to indicate which resource to reload.

This reduces the instances of a 405 Method Not Allowed response from FAPI which is returned when we attempt to refresh a resource that we haven't acted upon. For example, if we're attempting to sign in, calling signUp.reload() will cause an HTTP 405 error. By only performing this reload on the signIn resource, we reduce the instance of 405 errors in our logs and within customer applications.

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

Copy link

changeset-bot bot commented Apr 7, 2025

🦋 Changeset detected

Latest commit: e06e243

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 22 packages
Name Type
@clerk/clerk-js Patch
@clerk/types Patch
@clerk/chrome-extension Patch
@clerk/clerk-expo Patch
@clerk/agent-toolkit Patch
@clerk/astro Patch
@clerk/backend Patch
@clerk/elements Patch
@clerk/expo-passkeys Patch
@clerk/express Patch
@clerk/fastify Patch
@clerk/localizations Patch
@clerk/nextjs Patch
@clerk/nuxt Patch
@clerk/react-router Patch
@clerk/clerk-react Patch
@clerk/remix Patch
@clerk/shared Patch
@clerk/tanstack-react-start Patch
@clerk/testing Patch
@clerk/themes Patch
@clerk/vue Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Apr 7, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
clerk-js-sandbox ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 8, 2025 6:00pm

@@ -24,7 +24,9 @@ export const SSOCallbackCard = (props: HandleOAuthCallbackParams | HandleSamlCal
React.useEffect(() => {
let timeoutId: ReturnType<typeof setTimeout>;
if (__internal_setActiveInProgress !== true) {
handleRedirectCallback({ ...props }, navigate).catch(e => {
const intent = new URLSearchParams(window.location.search).get('intent');
Copy link
Member

Choose a reason for hiding this comment

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

Should we maybe use the params from the router instead ?

Copy link
Member Author

Choose a reason for hiding this comment

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

In the hash and virtual routers, the queryString isn't derived from window.location.search, which is where the intent param is going to be. For example, this is the SSO callback route when using the hash router:

http://localhost:4000/sign-in?intent=sign-in#/sso-callback

In this example, window.location.search is ?intent=sign-in, but the value of queryString from useRotuer is "".

Alternatively, we could append the intent parameter to different locations based on the router used, but I felt that was a little too much additional complexity in exchange for basically no additional functionality.

@@ -24,7 +24,9 @@ export const SSOCallbackCard = (props: HandleOAuthCallbackParams | HandleSamlCal
React.useEffect(() => {
let timeoutId: ReturnType<typeof setTimeout>;
if (__internal_setActiveInProgress !== true) {
handleRedirectCallback({ ...props }, navigate).catch(e => {
const intent = new URLSearchParams(window.location.search).get('intent');
const reloadResource = intent === 'signIn' || intent === 'signUp' ? intent : undefined;
Copy link
Member

Choose a reason for hiding this comment

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

Can we do this ? Or is reloadResource:false not proper ?

Suggested change
const reloadResource = intent === 'signIn' || intent === 'signUp' ? intent : undefined;
const reloadResource = ['signIn', 'signUp'].includes(intent)

Copy link
Member Author

@dstaley dstaley Apr 8, 2025

Choose a reason for hiding this comment

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

TypeScript won't narrow from string to 'signIn' | 'signUp' if we use Array.includes, and we still need to make sure we know which resource needs to be reloaded, so a boolean value won't work.

@dstaley dstaley merged commit 5f3cc46 into main Apr 8, 2025
30 checks passed
@dstaley dstaley deleted the ds.fix/redirect-reload-resource branch April 8, 2025 19:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants