Facebook 与 GitHub 身份验证
将 Facebook 和 GitHub 添加为身份验证提供商,并配置应用和 Firebase 以支持社交账号登录
Facebook 与 GitHub 身份验证 是 CoddyKit 上的免费 Firebase Auth & Realtime Database Apps 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Firebase Auth & Realtime Database Apps 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Firebase Auth & Realtime Database Apps 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Social Logins: Facebook & GitHub
Welcome! In this lesson, we'll dive into integrating two popular social login options: Facebook and GitHub.
Using social providers like these can significantly improve the user experience by offering quick, familiar sign-up and login methods.
Why Offer Social Logins?
Social logins provide a seamless experience for your users. Here's why they're great:
- Convenience: Users can sign in with accounts they already have.
- Reduced Friction: No need to create new usernames or passwords.
- Trust: Users often feel more secure using established platforms.
- Faster Onboarding: Quick access to your app's features.
Facebook App Setup: Developer Console
Before enabling Facebook login in Firebase, you need to create an app on the Facebook for Developers platform.
- Go to
developers.facebook.comand create a new app. - Choose the 'Consumer' type for typical user authentication.
- Note down your App ID and App Secret; you'll need them for Firebase.
Facebook Login Product Configuration
After creating your Facebook app, you must configure the 'Facebook Login' product:
- In your Facebook App Dashboard, add the 'Facebook Login' product.
- Under 'Settings' for Facebook Login, ensure 'Web OAuth Login' is enabled.
- Add your Firebase OAuth redirect URI (found in Firebase Auth settings) to 'Valid OAuth Redirect URIs'. This is crucial for security.
Firebase Console: Enable Facebook
Now, let's enable Facebook as an authentication provider in Firebase:
- Navigate to the Authentication section in your Firebase project console.
- Go to the 'Sign-in method' tab.
- Click 'Add new provider' and select Facebook.
- Paste your Facebook App ID and App Secret into the fields provided.
- Make sure to enable the provider and save your changes!
Code: Facebook Sign-In
Once configured, you can use the Firebase SDK to sign in users with Facebook. This JavaScript example shows the core logic, typically triggered by a button.
/* This is a conceptual example for web/JS.
Firebase SDK must be initialized before use. */
// 1. Get Auth instance & Facebook provider
const auth = firebase.auth();
const provider = new firebase.auth.FacebookAuthProvider();
// 2. Define the sign-in function
function signInWithFacebook() {
console.log("Attempting Facebook sign-in...");
auth.signInWithPopup(provider)
.then((result) => {
// User successfully signed in
const user = result.user;
console.log("Logged in as:", user.displayName);
})
.catch((error) => {
// Handle errors (e.g., user cancelled, network error)
console.error("Facebook login failed:", error.message);
});
}
// Call the function (in a real app, this is a button click)
signInWithFacebook();GitHub App Setup: Developer Console
Integrating GitHub is similar to Facebook. First, you need to create an OAuth App on GitHub:
- Go to
github.com/settings/developers. - Click 'New OAuth App'.
- Fill in the 'Application name', 'Homepage URL'.
- For 'Authorization callback URL', use your Firebase OAuth redirect URI.
- Click 'Register application' and note your Client ID and Client Secret.
Firebase Console: Enable GitHub
Next, enable GitHub as an authentication provider in your Firebase project:
- In the Firebase console, go to Authentication > Sign-in method.
- Click 'Add new provider' and select GitHub.
- Paste your GitHub Client ID and Client Secret into the respective fields.
- Enable the provider and save your changes.
The Firebase OAuth redirect URI for GitHub will be displayed here, ensure it matches what you entered on GitHub.
Code: GitHub Sign-In
Just like Facebook, once GitHub is configured, you can use the Firebase SDK to sign in users. This JavaScript snippet illustrates the process.
/* This is a conceptual example for web/JS.
Firebase SDK must be initialized before use. */
// 1. Get Auth instance & GitHub provider
const auth = firebase.auth();
const provider = new firebase.auth.GithubAuthProvider();
// 2. Define the sign-in function
function signInWithGitHub() {
console.log("Attempting GitHub sign-in...");
auth.signInWithPopup(provider)
.then((result) => {
// User successfully signed in
const user = result.user;
console.log("Logged in as:", user.displayName);
})
.catch((error) => {
// Handle errors (e.g., user cancelled, network error)
console.error("GitHub login failed:", error.message);
});
}
// Call the function (in a real app, this is a button click)
signInWithGitHub();Quick Check: Social Login Setup
You've learned the steps to integrate social logins. Which of these are essential for setting up a new social authentication provider (like Facebook or GitHub) with Firebase?
Recap: Facebook & GitHub Auth
Great job! In this lesson, you learned how to add Facebook and GitHub authentication to your Firebase project.
- We covered creating developer apps on each platform.
- You saw how to configure the necessary settings, including OAuth redirect URIs.
- Finally, we enabled these providers in the Firebase console and looked at conceptual code for signing users in.
Social logins simplify user onboarding and enhance your app's accessibility!
常见问题解答
「Facebook 与 GitHub 身份验证」课时是免费的吗?
是的 — 「Facebook 与 GitHub 身份验证」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Firebase Auth & Realtime Database Apps 课程的其余内容,请升级到 CoddyKit PRO。 Firebase Auth & Realtime Database Apps 课程共包含 4 节课。
「Facebook 与 GitHub 身份验证」这节课中我会学到什么?
将 Facebook 和 GitHub 添加为身份验证提供商,并配置应用和 Firebase 以支持社交账号登录 你通过在浏览器中直接运行的动手代码来练习 Firebase Auth & Realtime Database Apps,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Firebase Auth & Realtime Database Apps 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Firebase Auth & Realtime Database Apps 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「Facebook 与 GitHub 身份验证」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Firebase Auth & Realtime Database Apps 课中编写并运行代码吗?
能。每节 Firebase Auth & Realtime Database Apps 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 集成 Google 登录
- Facebook 与 GitHub 身份验证
- 匿名与自定义身份验证
- 集成 Apple 登录