Firebase SDK Authentication

Firebase SDK authentication is the hands-on path: you build your own login screens and call FirebaseAuth methods directly, like createUserWithEmailAndPassword and signInWithEmailAndPassword, giving you full control over the look and flow in exchange for writing the UI.

11 min read · 7 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Doing it yourself

FirebaseUI gives you login for free but with its own screens. Sometimes you want your own login screen, matching your app's design exactly, and your own control over the flow. For that, you call the Firebase Authentication SDK directly.

You build the login and registration layouts yourself, then invoke FirebaseAuth methods when the user taps your buttons. This lesson shows the core methods and the callback pattern Firebase uses. It is more code than FirebaseUI, but it is how you get complete control, and it makes clear what FirebaseUI was doing for you.

Theory

The core auth methods

With your own UI, three FirebaseAuth methods do the work:

  • createUserWithEmailAndPassword(email, password): register a new user.
  • signInWithEmailAndPassword(email, password): log an existing user in.
  • signOut(): sign the current user out.

Because these talk to Firebase over the network, they are asynchronous: each returns a Task that completes later, success or failure. You attach listeners to react when it finishes, rather than getting an immediate result. So the pattern is always: call the method, then handle the Task's outcome.

Practical

Registering and logging in with the SDK

val auth = Firebase.auth

// Register a new user (from your own registration screen)
auth.createUserWithEmailAndPassword(email, password)
    .addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val user = auth.currentUser        // now signed in
            // navigate to the main screen
        } else {
            // show task.exception?.message on your UI
        }
    }

// Log an existing user in
auth.signInWithEmailAndPassword(email, password)
    .addOnCompleteListener { task -> /* same success/failure handling */ }

Formula

Async: handle the Task, do not expect an instant result

These methods do not return the user immediately. They start a network request and return a Task; the result arrives later in a listener. So you never write val user = auth.signIn(...), you call the method and put your 'what to do next' inside addOnCompleteListener (or success/failure listeners).

Inside, check task.isSuccessful: if true, the user is signed in (auth.currentUser is set) and you proceed; if false, read task.exception to show the error. This asynchronous, callback-driven style is how all networked Firebase calls work, so getting comfortable with it here pays off across the whole subject.

Quiz

Using the Firebase SDK directly, which method registers a brand-new user with an email and password?

  1. signInWithEmailAndPassword(email, password), because it signs them in
  2. createUserWithEmailAndPassword(email, password), which registers a new user account
  3. signOut(), because it resets the account
  4. currentUser, because it creates the user
Show the answer

createUserWithEmailAndPassword(email, password), which registers a new user account

createUserWithEmailAndPassword(email, password) registers (creates) a new user account, and on success signs them in. Option A, signInWithEmailAndPassword, logs in an EXISTING user; it does not create a new account (it would fail if the account does not exist). Option C, signOut, ends the current session; it does not create anything. Option D, currentUser, is a PROPERTY that returns the signed-in user (or null); it reads state, it does not create a user. Match the method to the action: create to register, signIn to log in, signOut to leave, currentUser to check who is signed in.

Think first

Why are these auth calls asynchronous with Tasks instead of returning a result directly?

Why does signInWithEmailAndPassword return a Task you listen to, rather than just returning the user? Then tap.

Show the answer

Because signing in requires talking to Firebase's servers OVER THE NETWORK, which takes time, and blocking the app while waiting would freeze the user interface, so the call is asynchronous and delivers its result via a callback. When you call signInWithEmailAndPassword, the SDK sends the credentials to Firebase, which checks them and responds, a round trip that could take milliseconds or (on a poor connection) seconds. If the method tried to RETURN the user directly, the app's main thread would have to STOP and wait for that network round trip to finish, and on Android the main thread is the UI thread, so the whole app would freeze, no scrolling, no taps, and Android may even kill it for being unresponsive. To avoid that, Firebase returns a Task immediately (representing 'a result that will arrive later') and lets the app keep running; when the network response comes back, your addOnCompleteListener fires with the outcome. This keeps the UI smooth while the request is in flight, and it lets you show a loading spinner, then react to success or failure when it actually happens. This asynchronous, callback-driven model is standard for anything involving the network or slow work on mobile, precisely because responsiveness matters so much. So the Task is not extra ceremony; it is what keeps the app alive and smooth while waiting for the server. Never block the UI thread on the network; hand the result back when it is ready.

Summary

Key takeaways

  • Firebase SDK authentication means building your own login UI and calling FirebaseAuth methods directly.
  • createUserWithEmailAndPassword registers a new user; signInWithEmailAndPassword logs an existing user in; signOut ends the session.
  • These are asynchronous: each returns a Task that completes later, not an immediate result.
  • Attach a listener (addOnCompleteListener) and check task.isSuccessful; on failure read task.exception for the error.
  • Benefit over FirebaseUI: full control of the UI and flow; cost: you write the screens and handle the states yourself.
  • The calls are async because they hit the network, and blocking the UI thread would freeze the app.
  • Memory hook: create to register, signIn to log in, handle the Task's success or failure in a listener.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Introduction to Firebase

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Firebase SDK Authentication · Advance Mobile Application Development - II (Major-15-02) · Gri-Learn