AMP项目是否支持Google One Tap登录功能?
Yes, you absolutely can integrate Google One Tap for login/registration into your AMP pages (including registration, landing, static, and blog pages). AMP supports this through the amp-auto-lightbox component and Google's Identity Services script, which is tailored to work within AMP's performance and security constraints. Here's a practical, step-by-step breakdown of how to implement it:
Implementation Steps
Add the Required AMP Component
First, include theamp-auto-lightboxscript in your page's<head>section. This component lets you launch the One Tap flow in an AMP-compliant lightbox:<script async custom-element="amp-auto-lightbox" src="https://cdn.ampproject.org/v0/amp-auto-lightbox-0.1.js"></script>Set Up the One Tap Trigger
Create a clickable element (like a button) to trigger the flow, then wrap the Google Identity Services script and configuration inside anamp-auto-lightboxcontainer. Here's a working example:<amp-auto-lightbox id="google-one-tap-container" layout="nodisplay"> <script async src="https://accounts.google.com/gsi/client" onload="initOneTap()"></script> <div id="g_id_onload" data-client_id="YOUR_GOOGLE_CLIENT_ID" data-login_uri="https://your-domain.com/handle-google-login" data-auto_prompt="false"> </div> <div id="g_id_signin"></div> </amp-auto-lightbox> <button on="tap:google-one-tap-container.open">Sign in with Google</button>- Replace
YOUR_GOOGLE_CLIENT_IDwith the client ID from your Google Cloud project. data-login_urishould point to your backend endpoint that will verify the Google ID token and create a user session.
- Replace
Validate the Token on Your Backend
After the user completes the One Tap flow, Google will send an ID token to your specifiedlogin_uri. Use Google's official libraries (like thegoogle-auth-libraryfor Node.js) to validate this token, confirm its legitimacy, and then create a session for the user.
Key Considerations
- AMP Compliance: Always use the latest versions of AMP components and the Google Identity Services script to avoid compatibility issues—AMP enforces strict rules for third-party scripts to maintain performance.
- User Experience: The
amp-auto-lightboxensures the One Tap flow doesn't break AMP's fast-loading promise, providing a smooth, native-feeling experience for users. - Privacy & Terms: Make sure you're following Google's terms of service and relevant privacy laws (like GDPR) when handling user data from One Tap.
If you hit specific snags (like console errors or callback issues), share your code snippets and details, and we can troubleshoot further!
内容的提问来源于stack exchange,提问作者Steven Quinn

