-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(clerk-js): Revalidate environment on window focus for Keyless
- Loading branch information
1 parent
4fecdac
commit c61fa3a
Showing
4 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@clerk/clerk-js': patch | ||
--- | ||
|
||
Revalidate environment on window focus for Keyless. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
packages/clerk-js/src/ui/components/KeylessPrompt/use-revalidate-environment.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { useClerk } from '@clerk/shared/react'; | ||
import { useEffect, useReducer } from 'react'; | ||
|
||
import type { Clerk } from '../../../core/clerk'; | ||
import type { Environment } from '../../../core/resources'; | ||
import { useEnvironment } from '../../contexts'; | ||
|
||
/** | ||
* Revalidates environment on focus, highly optimized for Keyless mode. | ||
* Attention: this is not a generic solution, and should not be used for revalidating environment inside UI components that are end-user facing (e.g. SignIn) | ||
*/ | ||
function useRevalidateEnvironment() { | ||
const clerk = useClerk(); | ||
const [, forceUpdate] = useReducer(v => v + 1, 0); | ||
|
||
useEffect(() => { | ||
const controller = new AbortController(); | ||
window.addEventListener( | ||
'focus', | ||
|
||
async () => { | ||
const environment = (clerk as Clerk).__unstable__environment as Environment | undefined; | ||
|
||
if (!environment) { | ||
return; | ||
} | ||
|
||
if (environment.authConfig.claimedAt !== null) { | ||
return controller.abort(); | ||
} | ||
|
||
if (document.visibilityState !== 'visible') { | ||
return; | ||
} | ||
|
||
const maxRetries = 2; | ||
|
||
for (let i = 0; i < maxRetries; i++) { | ||
const { | ||
authConfig: { claimedAt }, | ||
} = await environment.fetch(); | ||
|
||
if (claimedAt !== null) { | ||
forceUpdate(); | ||
break; | ||
} | ||
} | ||
}, | ||
{ | ||
signal: controller.signal, | ||
}, | ||
); | ||
|
||
return () => { | ||
controller.abort(); | ||
}; | ||
}, []); | ||
|
||
return useEnvironment(); | ||
} | ||
|
||
export { useRevalidateEnvironment }; |