FacebookとGitHubの認証
FacebookとGitHubを認証プロバイダーとして追加し、ソーシャルログインに対応するようアプリとFirebaseを設定します。
「FacebookとGitHubの認証」はCoddyKit上の無料Firebase Auth & Realtime Database Appsレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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の認証」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Firebase Auth & Realtime Database Appsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Firebase Auth & Realtime Database Appsコースには全4レッスンが含まれています。
「FacebookとGitHubの認証」で何を学びますか?
FacebookとGitHubを認証プロバイダーとして追加し、ソーシャルログインに対応するようアプリとFirebaseを設定します。 ブラウザで直接実行するハンズオンコードでFirebase Auth & Realtime Database Appsを演習し、24時間対応の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 Sign-Inの統合
- FacebookとGitHubの認証
- 匿名認証とカスタム認証
- Apple Sign-Inの統合