WebView 应用中的 Google 登录和 Sign in with Apple:Firebase Auth 的 Codex 工作流程
在封装的 Web 应用中,有两件事会让登录失败:Google 拒绝在嵌入式 WebView 中进行 OAuth,而 Apple 会拒绝那些提供了 Google 登录却没有同等隐私友好选项的应用。WebViewGold 以原生方式解决了第一个问题,并在 iOS 上自动识别 Sign in with Apple;Codex 负责让 Web 端与之配合。本指南以托管在 Express 上的 Firebase Auth 应用为例,在这种技术栈中重定向的细节最为关键。
目标是提供三种登录方式,它们在 iOS 和 Android 应用内的表现与网页上完全一致:电子邮件、Google 和 Sign in with Apple,最终都进入 WebView 中同一个 Firebase 会话,并且在关闭再重新打开应用后依然保持登录。
审核人员的测试很简短。他们点按 Google;如果 Google 返回“Error 403: disallowed_useragent”,应用就算是坏的(准则 2.1)。接着他们会在 Google 旁边寻找 Apple 选项;如果没有,准则 4.8 要求提供一种只收集姓名和电子邮件、并允许用户隐藏邮箱地址的登录方式,实际上就是 Sign in with Apple。只使用自有邮箱账号或魔法链接账号的应用不受此限制,这也是一条正当的出路(准则 4.8 详解)。
一个包含您的域名和返回 URL https://app.example.com/__/auth/handler 的 Services ID(因为使用了代理,所以是您自己的域名)、一个 Sign in with Apple 密钥和您的 Team ID,全部填入 Firebase 的 Apple 提供方设置中。
Android:Apple 登录是可选的
Google Play 没有关于 Sign in with Apple 的规定。只有在您的用户期望时才在 Android 上显示这个按钮,并在真机上测试该重定向;WebViewGold 的 Apple 自动处理在文档中是针对 iOS 说明的。
## Authentication (Firebase Auth inside WebViewGold)
- Providers: email/password, Google, Apple. Google and Apple buttons live in one
component (src/components/SocialButtons.tsx) with equal size and prominence.
- Every sign-in call goes through src/lib/auth.ts. Inside the native app
(isNativeApp()) use signInWithRedirect, which Firebase recommends on mobile;
keep the method in one constant so we can switch to popup after device tests.
Desktop browsers may use signInWithPopup.
- authDomain is our own domain (app.example.com). Express proxies /__/auth/* to
PROJECT_ID.firebaseapp.com as a transparent reverse proxy. Never point
authDomain back to firebaseapp.com.
- Google inside the app works through WebViewGold's Social Login Helper
(google_login_helper_triggers / GOOGLE_LOGIN_HELPER_TRIGGERS). Do not change the
user agent, do not open OAuth URLs with window.open, do not use googlelogin://
unless explicitly asked.
- Sign in with Apple (appleid.apple.com) is handled by WebViewGold on iOS.
- Apple shares the name only on the first sign-in: write displayName to users/{uid}
immediately. Expect @privaterelay.appleid.com addresses.
- auth/account-exists-with-different-credential: ask the user to sign in with the
existing method, then linkWithCredential. Never create a second account silently.
// Config.swift: Social Login Helper triggers (documented defaults)
var google_login_helper_triggers: [String] = ["https://accounts.google.com", "https://accounts.youtube.com"]
var facebook_login_helper_triggers: [String] = ["https://m.facebook.com", "https://www.facebook.com"]
// Sign in with Apple (appleid.apple.com) needs no trigger: WebViewGold handles it on iOS.
// Keep sessions across launches: leave deletecache and deletecacheonexit off,
// or users are logged out every time they open the app.
// Custom user agent (useragent_iphone / useragent_ipad): append your marker to the
// default string instead of replacing it.
工作顺序
Apple Developer 账号
在 App ID 上启用 Sign in with Apple,创建一个 Services ID,注册 app.example.com 和返回 URL https://app.example.com/__/auth/handler,创建一个 Sign in with Apple 密钥,并记下 Key ID 和 Team ID。
Firebase 控制台
启用 Apple 提供方(Services ID、Team ID、Key ID、私钥)和 Google。把 app.example.com 添加到授权域名中。