-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(astro): Introduce
<AuthenticateWithRedirectCallback/>
for cust…
…om oath flows (#3719)
- Loading branch information
1 parent
1453383
commit c92104e
Showing
8 changed files
with
90 additions
and
1 deletion.
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/astro": patch | ||
--- | ||
|
||
Introduce `<AuthenticateWithRedirectCallback/>` as an Astro and as a React component |
42 changes: 42 additions & 0 deletions
42
packages/astro/src/astro-components/control/AuthenticateWithRedirectCallback.astro
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,42 @@ | ||
--- | ||
import type { HandleOAuthCallbackParams } from '@clerk/types'; | ||
type Props = HandleOAuthCallbackParams | ||
import { generateSafeId } from '@clerk/astro/internal'; | ||
const safeId = generateSafeId(); | ||
// TODO: Move this to a prop when we implement more control components. | ||
const functionName = 'handleRedirectCallback'; | ||
--- | ||
|
||
<div data-clerk-function-id={`clerk-${functionName}-${safeId}`}></div> | ||
|
||
<script is:inline define:vars={{ props: Astro.props, functionName, safeId }}> | ||
/** | ||
* Store the id and the props for the Astro component in order to invoice the correct Clerk function once clerk is loaded. | ||
* The above is handled by `invokeClerkAstroJSFunctions`. | ||
* | ||
* TODO: This should be moved to a separate file once we implement more control components. | ||
*/ | ||
const setOrCreatePropMap = ({ functionName, id, props }) => { | ||
if (!window.__astro_clerk_function_props) { | ||
window.__astro_clerk_function_props = new Map(); | ||
} | ||
|
||
if (!window.__astro_clerk_function_props.has(functionName)) { | ||
const _ = new Map(); | ||
_.set(id, props); | ||
window.__astro_clerk_function_props.set(functionName, _); | ||
} | ||
|
||
window.__astro_clerk_function_props.get(functionName)?.set(id, props); | ||
}; | ||
|
||
setOrCreatePropMap({ | ||
functionName, | ||
id: safeId, | ||
props, | ||
}); | ||
</script> |
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
19 changes: 19 additions & 0 deletions
19
packages/astro/src/internal/invoke-clerk-astro-js-functions.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,19 @@ | ||
import { $clerk } from '../stores/internal'; | ||
|
||
/** | ||
* Loop through any Astro component that has requested to invoke a function and invoke it with its respective props. | ||
*/ | ||
const invokeClerkAstroJSFunctions = () => { | ||
const functionNames = ['handleRedirectCallback'] as const; | ||
|
||
functionNames.forEach(fnName => { | ||
const elementsOfCategory = document.querySelectorAll(`[data-clerk-function-id^="clerk-${fnName}"]`); | ||
elementsOfCategory.forEach(el => { | ||
const id = el.getAttribute('data-clerk-function-id'); | ||
const props = window.__astro_clerk_function_props?.get(fnName)?.get(id!) ?? {}; | ||
void $clerk.get()?.[fnName]?.(props); | ||
}); | ||
}); | ||
}; | ||
|
||
export { invokeClerkAstroJSFunctions }; |
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
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