All skills
firebase avatar

/firebase-auth-basics

@4e28cb3 official
by firebasefirebase/agent-skills461 stars
102

Guide for setting up and using Firebase Authentication. Use this skill when the user's app requires user sign-in, user management, or secure data access using auth rules.

Use this Skill: https://skilld.dev/gh/firebase/agent-skills/firebase-auth-basics

This session only. Nothing lands on disk.

referencesclient_sdk_android.md

≈1.5k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Firebase Authentication - Android Setup Guide (Kotlin)

This guide describes the SDK setup and basic usage patterns for Firebase Authentication in an Android app using Kotlin DSL (build.gradle.kts) and Kotlin code.

Prerequisites

IMPORTANT: Before specifically working with Firebase Authentication, make sure to use the skill and reference firebase_basics/references/android_setup to ensure the following is done.

  • The Firebase CLI is available and authenticated.
  • An Android project exists and is registered with a Firebase Project.
  • The Android project has a Firebase config file (google-services.json) and the Google services Gradle plugin (google-services).

1. Enable Authentication via CLI

Enable the Firebase Authentication service in the Firebase Project using the Firebase CLI:

npx -y firebase-tools@latest init auth

2. Add dependencies to Gradle build files

[!IMPORTANT] [AGENT] RESOLVING THE SDK VERSION DETERMINISTICALLY Never guess or hardcode a memorized out-of-date SDK or plugin version. Before adding dependencies, you MUST query the latest available versions directly from the Google Maven Repository:

# Find the latest Firebase Android BoM version
curl -s https://dl.google.com/dl/android/maven2/com/google/firebase/firebase-bom/maven-metadata.xml | grep -oE '<latest>[^<]+' | cut -d'>' -f2

# Find the latest firebase-auth version
curl -s https://dl.google.com/dl/android/maven2/com/google/firebase/firebase-auth/maven-metadata.xml | grep -oE '<latest>[^<]+' | cut -d'>' -f2

If the dependency is already declared, fetch the exact resolved version currently active in the workspace's build graph:

./gradlew -q :app:dependencyInsight --dependency firebase-auth --configuration releaseRuntimeClasspath

In the module (app-level) build.gradle.kts (usually <project>/<app-module>/build.gradle.kts), add the dependency for Firebase Authentication:

dependencies {
    // [AGENT] Fetch the latest resolved version using the Gradle command above
    implementation(platform("com.google.firebase:firebase-bom:<latest_bom_version>"))

    // Add the dependency for the Firebase Authentication library
    // When using the BoM, don't specify versions in Firebase library dependencies
    implementation("com.google.firebase:firebase-auth")
}

3. Initialize FirebaseAuth

In the Activity or Fragment, initialize the FirebaseAuth instance:

import com.google.firebase.Firebase
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.auth

class MainActivity : AppCompatActivity() {

    private lateinit var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        auth = Firebase.auth

        setContent {
            MaterialTheme {
                Text("Auth initialized!")
            }
        }
    }
}

Jetpack Compose (Modern)

Initialize inside a ComponentActivity using setContent:

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import com.google.firebase.Firebase
import com.google.firebase.auth.auth

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val auth = Firebase.auth

        setContent {
            MaterialTheme {
                Text("Auth initialized!")
            }
        }
    }
}

4. Check current Auth state

Check if a user is already signed in when the activity starts:

public override fun onStart() {
    super.onStart()
    // Check if user is signed in (non-null) and update UI accordingly.
    val currentUser = auth.currentUser
    if (currentUser != null) {
        // User is signed in, navigate to main screen or update UI
    } else {
        // No user is signed in, prompt for login
    }
}

5. Use sign-in providers in the app

Email/Password

Sign up NEW users with Email/Password

Use createUserWithEmailAndPassword to register new users:

fun signUpUser(email: String, password: String) {
    auth.createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Sign up success, update UI with the signed-in user's information
                val user = auth.currentUser
                // Navigate to main screen
            } else {
                // If sign up fails, display a message to the user.
                Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show()
            }
        }
}
Sign in EXISTING users with Email/Password

Use signInWithEmailAndPassword to sign in existing users:

fun signInUser(email: String, password: String) {
    auth.signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Sign in success, update UI with the signed-in user's information
                val user = auth.currentUser
                // Navigate to main screen
            } else {
                // If sign in fails, display a message to the user.
                Toast.makeText(baseContext, "Authentication failed.", Toast.LENGTH_SHORT).show()
            }
        }
}

6. Sign out users

To sign out a user, call signOut() on the FirebaseAuth instance:

auth.signOut()
// Navigate to login screen

Source: SKILL.md on GitHub

No alerts2d5 checks · Risk SAFE
  • Gen Agent Trust Hub2d

    This skill provides a comprehensive and secure guide for integrating Firebase Authentication into Web, Android, iOS, and Flutter applications. It correctly guides users through using official Firebase CLI tools and SDKs, provides essential troubleshooting for common OAuth issues, and emphasizes the importance of using Firebase Security Rules to protect data. No malicious patterns or security risks were detected.

  • Socket2d

    No alerts

  • Snyk2d

    Risk: LOW · No issues

  • Runlayer6mo

    3 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 4e28cb3. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 days ago.

Activeupdated last week
metadata
{
  "category": "Identity"
}
Other metadata
compatibility
This skill is best used with the Firebase CLI, but does not require it. Firebase CLI can be accessed through `npx -y firebase-tools@latest`.

README badge

README badge for firebase/agent-skills/firebase-auth-basics