-
-
Notifications
You must be signed in to change notification settings - Fork 43
feat: add Google One Tap modal #1117
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,5 +114,8 @@ | |
"core-js-pure", | ||
"sharp" | ||
] | ||
}, | ||
"dependencies": { | ||
"zod": "^3.25.13" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import type { WrapperProps } from '@docusaurus/types'; | ||
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; | ||
import type LayoutType from '@theme/Layout'; | ||
import Layout from '@theme-original/Layout'; | ||
import type { ReactNode } from 'react'; | ||
import { useEffect, useState } from 'react'; | ||
import { z } from 'zod'; | ||
|
||
type Props = WrapperProps<typeof LayoutType>; | ||
|
||
const > | ||
.object({ | ||
isEnabled: z.boolean().optional(), | ||
autoSelect: z.boolean().optional(), | ||
closeOnTapOutside: z.boolean().optional(), | ||
itpSupport: z.boolean().optional(), | ||
}) | ||
.optional(); | ||
|
||
const googleOneTapConfigSchema = z.object({ | ||
clientId: z.string(), | ||
oneTap: oneTapSchema, | ||
}); | ||
|
||
type GoogleOneTapConfig = z.infer<typeof googleOneTapConfigSchema>; | ||
|
||
const CACHE_KEY = '_logto_google_one_tap_config'; | ||
const CACHE_EXPIRY_KEY = '_logto_google_one_tap_config_expiry'; | ||
const CACHE_EXPIRY_TIME = 1 * 60 * 60 * 1000; // 1 hour | ||
|
||
// Default API base URL | ||
const DEFAULT_API_BASE_PROD_URL = 'https://auth.logto.io'; | ||
const DEFAULT_API_BASE_DEV_URL = 'https://auth.logto.dev'; | ||
|
||
export default function LayoutWrapper(props: Props): ReactNode { | ||
const [config, setConfig] = useState<GoogleOneTapConfig | undefined>(undefined); | ||
const { siteConfig } = useDocusaurusContext(); | ||
|
||
// Get the API base URL from customFields, or use the default value if it doesn't exist | ||
const logtoApiBaseUrl = siteConfig.customFields?.logtoApiBaseUrl; | ||
const apiBaseUrl = | ||
typeof logtoApiBaseUrl === 'string' | ||
? logtoApiBaseUrl | ||
: siteConfig.customFields?.isDevFeatureEnabled | ||
? DEFAULT_API_BASE_DEV_URL | ||
: DEFAULT_API_BASE_PROD_URL; | ||
|
||
useEffect(() => { | ||
const fetchConfig = async () => { | ||
try { | ||
const cachedConfig = localStorage.getItem(CACHE_KEY); | ||
const cachedExpiry = localStorage.getItem(CACHE_EXPIRY_KEY); | ||
|
||
if (cachedConfig && cachedExpiry && Number(cachedExpiry) > Date.now()) { | ||
try { | ||
const parsedConfig = googleOneTapConfigSchema.parse(JSON.parse(cachedConfig)); | ||
setConfig(parsedConfig); | ||
return; | ||
} catch (parseError) { | ||
console.error('Cached config validation failed:', parseError); | ||
} | ||
} | ||
|
||
const response = await fetch(`${apiBaseUrl}/api/google-one-tap/config`, { | ||
headers: { | ||
Origin: window.location.origin, | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error('Failed to fetch Google One Tap config'); | ||
} | ||
|
||
const data = await response.json(); | ||
|
||
const validatedConfig = googleOneTapConfigSchema.parse(data); | ||
|
||
localStorage.setItem(CACHE_KEY, JSON.stringify(validatedConfig)); | ||
localStorage.setItem(CACHE_EXPIRY_KEY, String(Date.now() + CACHE_EXPIRY_TIME)); | ||
|
||
setConfig(validatedConfig); | ||
} catch (error) { | ||
console.error('Error fetching or validating Google One Tap config:', error); | ||
} | ||
}; | ||
|
||
void fetchConfig(); | ||
}, [apiBaseUrl]); | ||
|
||
return ( | ||
<> | ||
{config && config.oneTap?.isEnabled && ( | ||
<div | ||
data-itp_support={Boolean(config.oneTap.itpSupport)} | ||
data-auto_select={Boolean(config.oneTap.autoSelect)} | ||
data-cancel_on_tap_outside={Boolean(config.oneTap.closeOnTapOutside)} | ||
id="g_id_onload" | ||
data-client_id={config.clientId} | ||
// TODO: implement handleCredentialResponse page | ||
data-callback="handleCredentialResponse" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [nitpick] Consider adding documentation or clarifying the TODO for the 'handleCredentialResponse' callback to guide future implementations. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
data-context="signin" | ||
></div> | ||
)} | ||
<Layout {...props} /> | ||
</> | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
}, | ||
"include": [ | ||
"src", | ||
"src/theme", | ||
"docs", | ||
"i18n", | ||
"plugins", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider implementing a graceful fallback UI or user notification in case the config fetch fails rather than only logging the error.
Copilot uses AI. Check for mistakes.