Firebase Auth & Realtime Database Apps · Lektion

Google Sign-In integrieren

Integrieren Sie Google Sign-In in Ihre Anwendung, damit sich Benutzer nahtlos mit ihren Google-Konten authentifizieren können

Lektion 1 von 410 Schritte

Google Sign-In integrieren ist eine kostenlose Firebase Auth & Realtime Database Apps-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Firebase Auth & Realtime Database Apps-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Firebase Auth & Realtime Database Apps-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Welcome to Google Sign-In

Google Sign-In is a secure way to let users log into your app using their Google accounts. It's fast, convenient, and users trust it.

This lesson will guide you through integrating Google Sign-In with Firebase Authentication, making your app more accessible.

Why Google Sign-In?

Using Google Sign-In offers several benefits:

  • Ease of Use: Users don't need to create new passwords.
  • Security: Leverages Google's robust authentication system.
  • Trust: Users are familiar with the Google sign-in flow.
  • Developer Simplicity: Firebase handles much of the complexity.

Integration Steps Overview

Integrating Google Sign-In with Firebase involves a few key steps:

  1. Enable Google as a provider in Firebase.
  2. Configure your app's Google Sign-In client.
  3. Handle the user's sign-in flow.
  4. Exchange the Google credential for a Firebase credential.
  5. Sign in the user to Firebase.

Enable Google in Firebase

First, you need to tell Firebase that you'll be using Google Sign-In.

Go to your Firebase Console, select your project, then navigate to Authentication > Sign-in method. Find 'Google' and enable it. Make sure to save your changes!

Web Client ID for Auth

Even for mobile apps, Firebase often uses a Web Client ID to securely authenticate with Google's backend services. This ID ensures that the Google token received by your app can be verified by Firebase.

After enabling Google Sign-In in the Firebase Console, Firebase automatically creates and links a Web Client ID for you. You'll typically find this in your project's `google-services.json` (Android) or `GoogleService-Info.plist` (iOS) file.

Configure Google Sign-In Client

In your application, you need to configure the Google Sign-In client. This setup includes requesting the user's ID token, which Firebase will use for authentication.

Here's a conceptual example of how you'd set up these options:

class GoogleSignInOptions {
  boolean requestIdToken;
  String webClientId;

  GoogleSignInOptions(String clientId) {
    this.webClientId = clientId;
  }

  GoogleSignInOptions requestIdToken(boolean value) {
    this.requestIdToken = value;
    return this;
  }

  String build() {
    return "Options(ID_Token:" + requestIdToken + ", ClientID:" + webClientId + ")";
  }
}

public class Main {
  public static void main(String[] args) {
    String WEB_CLIENT_ID = "YOUR_WEB_CLIENT_ID.apps.googleusercontent.com";
    GoogleSignInOptions gso = new GoogleSignInOptions(WEB_CLIENT_ID)
            .requestIdToken(true); // Request ID token for Firebase Auth

    System.out.println("Google Sign-In options configured:");
    System.out.println(gso.build());
  }
}

Initiate Sign-In Flow

Once the Google Sign-In client is configured, you'll typically have a "Sign in with Google" button. When the user taps this button, you'll launch the Google Sign-In intent to prompt the user to choose their Google account.

After the user selects an account, Google returns an authentication result, which includes the ID token we requested earlier.

Authenticate with Firebase

With the Google ID token in hand, the next step is to create a Firebase credential using this token and then sign in to Firebase Authentication.

Firebase handles the verification of the Google token and creates a new user or links to an existing one.

class GoogleAuthProvider {
  static String getCredential(String idToken) {
    return "GoogleCredential(" + idToken.substring(0, 10) + "...)";
  }
}

class FirebaseAuth {
  void signInWithCredential(String credential) {
    System.out.println("Firebase Auth: Signing in with " + credential);
    // Simulate success
    System.out.println("User signed in successfully!");
  }
}

public class Main {
  public static void main(String[] args) {
    // Imagine this ID token comes from Google Sign-In result
    String googleIdToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";

    // 1. Create a Firebase credential from the Google ID token
    String credential = GoogleAuthProvider.getCredential(googleIdToken);

    // 2. Sign in to Firebase with the credential
    FirebaseAuth auth = new FirebaseAuth();
    auth.signInWithCredential(credential);
  }
}

Check Your Understanding

What is the primary piece of information obtained from Google Sign-In that Firebase uses to authenticate a user?

Google Sign-In Summary

You've learned how to integrate Google Sign-In with Firebase Authentication! This powerful combination provides a secure and user-friendly way for users to access your application.

Key takeaways:

  • Enable Google as a provider in Firebase.
  • Configure your app's Google Sign-In client to request an ID token.
  • Use the obtained ID token to create a Firebase credential.
  • Sign in to Firebase Auth with that credential.

Next, we'll explore other social login options like Facebook and GitHub!

Kostenlos starten

Lerne Firebase Auth & Realtime Database Apps mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
11
Lektionen
44

Häufig gestellte Fragen

Ist die Lektion „Google Sign-In integrieren“ kostenlos?

Ja — der vollständige Text von „Google Sign-In integrieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Firebase Auth & Realtime Database Apps-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Firebase Auth & Realtime Database Apps-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Google Sign-In integrieren“?

Integrieren Sie Google Sign-In in Ihre Anwendung, damit sich Benutzer nahtlos mit ihren Google-Konten authentifizieren können Du übst Firebase Auth & Realtime Database Apps mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Firebase Auth & Realtime Database Apps zu starten?

Keine Vorkenntnisse erforderlich. Firebase Auth & Realtime Database Apps auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.

Wie lange dauert die Lektion „Google Sign-In integrieren“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Firebase Auth & Realtime Database Apps-Lektion Code schreiben und ausführen?

Ja. Jede Firebase Auth & Realtime Database Apps-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Google Sign-In integrieren
  2. Facebook- und GitHub-Authentifizierung
  3. Anonyme und benutzerdefinierte Authentifizierung
  4. Apple-Sign-In-Integration
← Zurück zu Firebase Auth & Realtime Database Apps