集成 Apple 登录
将“通过 Apple 登录”添加到 Firebase 应用中,满足 Apple 针对社交登录设置的 App Store 要求,并正确处理 Apple 注重隐私的电子邮件中继。
集成 Apple 登录 是 CoddyKit 上的免费 Firebase Auth & Realtime Database Apps 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Firebase Auth & Realtime Database Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Firebase Auth & Realtime Database Apps 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Apple Sign-In
If your iOS app offers any third-party social login (Google, Facebook, etc.), Apple's App Store guidelines require you to also offer Sign in with Apple. Beyond compliance, it is a fast, privacy-respecting option users trust.
How It Differs
Apple Sign-In has unique traits compared to other providers:
- Users can hide their real email via a private relay address
- Name and email are only returned on the first sign-in
- Requires an Apple Developer account to configure
Enabling the Provider
In the Firebase console open Authentication > Sign-in method and enable Apple. For web you must also supply your Apple Service ID, Team ID, Key ID, and a private key generated in the Apple Developer portal.
Configuring the OAuth Provider
On the web you use Firebase's OAuthProvider with the Apple provider ID and request the scopes you need.
import { OAuthProvider } from 'firebase/auth';
const provider = new OAuthProvider('apple.com');
provider.addScope('email');
provider.addScope('name');Triggering the Sign-In
Use signInWithPopup (or redirect on mobile web) to launch the Apple flow and receive a Firebase user.
import { getAuth, signInWithPopup } from 'firebase/auth';
const result = await signInWithPopup(getAuth(), provider);
const user = result.user;
console.log('Signed in as', user.uid);Capturing Name on First Sign-In
Apple sends the user's full name only once, on the very first authorization. Persist it immediately, because subsequent sign-ins will not include it.
const credential = OAuthProvider.credentialFromResult(result);
const fullName = result._tokenResponse?.displayName;
if (fullName) saveProfileName(user.uid, fullName);The Private Email Relay
If a user chooses to hide their email, Apple returns a relay address like abc123@privaterelay.appleid.com. Email sent to it is forwarded to the user.
- Treat it as a valid, stable email
- Do not require the 'real' address
Account Linking
A user may sign in with Apple after previously using email/password with the same address. Use Firebase account linking to merge identities into a single account rather than creating duplicates.
import { linkWithCredential } from 'firebase/auth';
const cred = OAuthProvider.credentialFromResult(result);
await linkWithCredential(existingUser, cred);Native iOS Considerations
In a native iOS app you generate a nonce and pass Apple's identity token plus that nonce to Firebase. The nonce protects against replay attacks and is required by Firebase for native Apple credentials.
Handling Cancellation
Users can dismiss the Apple sheet. This surfaces as an error you should treat as a silent no-op, not a failure to display loudly.
try {
await signInWithPopup(getAuth(), provider);
} catch (e) {
if (e.code === 'auth/popup-closed-by-user') return;
showError('Sign-in failed, please try again.');
}Testing and Review
Apple reviewers actively check that Sign in with Apple is offered when other social logins exist. Test the full flow, including the hide-email path, before submitting your app for review.
Quick Check
Test your understanding of Apple Sign-In.
Recap
You can now add Apple Sign-In correctly.
- Required when other social logins are offered on iOS
- Configure the provider with Service/Team/Key IDs
- Capture the name on first sign-in only
- Support the private email relay
- Use nonces natively and link duplicate accounts
常见问题解答
「集成 Apple 登录」课时是免费的吗?
是的 — 「集成 Apple 登录」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Firebase Auth & Realtime Database Apps 课程的其余内容,请升级到 CoddyKit PRO。 Firebase Auth & Realtime Database Apps 课程共包含 4 节课。
「集成 Apple 登录」这节课中我会学到什么?
将“通过 Apple 登录”添加到 Firebase 应用中,满足 Apple 针对社交登录设置的 App Store 要求,并正确处理 Apple 注重隐私的电子邮件中继。 你通过在浏览器中直接运行的动手代码来练习 Firebase Auth & Realtime Database Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Firebase Auth & Realtime Database Apps 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Firebase Auth & Realtime Database Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「集成 Apple 登录」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Firebase Auth & Realtime Database Apps 课中编写并运行代码吗?
能。每节 Firebase Auth & Realtime Database Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 集成 Google 登录
- Facebook 与 GitHub 身份验证
- 匿名与自定义身份验证
- 集成 Apple 登录