apps/docs/content/troubleshooting/pkce-flow-errors-cannot-parse-response-or-zgotmplz-in-magic-link-emails-433665.mdx
When setting up authentication with magic links and mobile deep linking, you might encounter specific errors like #ZgotmplZ in your email templates or a 'cannot parse response' error during the login flow. This guide explains the underlying causes and provides a robust solution.
PKCE (Proof Key for Code Exchange) is a security extension to the OAuth 2.0 Authorization Code Flow, specifically designed for public clients like mobile apps. It prevents interception attacks by requiring the client to generate a secret (a "code verifier") which is hashed and sent to the authorization server during the initial authorization request. This hash (the "code challenge") is later compared to the actual code verifier when the client exchanges the authorization code for an access token. It acts as a secure "handshake" between your mobile application and the authentication service.
Magic links are a passwordless authentication method where users receive a unique, time-sensitive link via email. Clicking this link directly logs them into the application without needing a password.
{{ .SiteURL }}?The email templating system often uses a language like Go, which has built-in security features to prevent cross-site scripting (XSS) and other vulnerabilities. When a variable like {{ .SiteURL }} (intended to represent your application's primary URL) is used in an email template, Go's security model automatically sanitizes the output. If the URL provided in {{ .SiteURL }} does not start with a recognized safe scheme like http:// or https:// (e.g., your-app-scheme://), Go considers it potentially malicious. To protect users, it replaces the unsafe link with a placeholder, typically #ZgotmplZ.
You may observe two distinct issues:
The #ZgotmplZ Error:
your-app-scheme:// for a mobile app deep link) directly within a template variable like {{ .SiteURL }} in your email templates. Go's security features sanitize this perceived "unsafe" URL, replacing it with the #ZgotmplZ placeholder. This prevents the link from being rendered correctly and thus from working.The 'Cannot Parse Response' Error:
To achieve a reliable and secure authentication experience that accommodates both Go's security model and the complexities of mobile deep linking and PKCE, the recommended approach is a multi-step redirection: Email → Website → Mobile App.
Here’s how to implement this solution:
Ensure your authentication service (e.g., Supabase) is correctly configured to use your website as the primary callback destination.
SITE_URL: Update this setting to your website's primary domain. This should be a standard web URL (e.g., https://example.com). This ensures that {{ .SiteURL }} and {{ .ConfirmationURL }} will generate safe, web-standard links.Additional Redirect URLs: Include your mobile app's deep link scheme with a wildcard (e.g., your-app-scheme://*) in the list of authorized redirect URLs. This tells the authentication service that your app's scheme is a valid destination after the initial web redirect.Now that your SITE_URL is correctly set to a web domain, you can safely use the built-in variables.
{{ .ConfirmationURL }} for Magic Links: In your email template, use {{ .ConfirmationURL }}. This variable will generate a secure web link pointing to your configured SITE_URL, typically https://example.com/auth/callback or a similar path. This link is safe from Go's sanitization as it uses a standard https:// scheme.This is the most crucial step to resolve both the broken PKCE handshake and the email link scanner issues.
SITE_URL (e.g., https://example.com/auth/callback). This page will be the initial destination when the user clicks the magic link in the email.window.location.href = "your-app-scheme://login-callback?code=..." where code would contain any necessary authentication parameters received by the web callback page.By following this Email → Website → Mobile App flow with a user-initiated action on the web page, you establish a robust and secure authentication process that sidesteps common pitfalls associated with email clients and link scanners, ensuring a consistent user experience.