Sign in a user with an email by using Identity Platform

Learn how to use Identity Platform to sign in a user with an email and password.


For step-by-step guidance on this task directly in console, click Guide me:

Guide me


The following sections take you through the same steps as clicking Guide me.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.

Enable Identity Platform

  1. In the console, go to the Identity Platform page in the Cloud Marketplace.

    Go to Identity Platform

  2. Click Enable Identity Platform.

Configure an email sign-in

  1. Go to the Identity Providers page.

  2. On the Identity Providers page, click Add a provider.

  3. In the Select a provider list, select Email/Password.

  4. Click the Enabled toggle to on.

  5. To save the provider settings, click Save.

Create a user

  1. In the console, go to the Users page.

    Go to Users

  2. Click Add user.

  3. In the Email field, enter an email and password. Note both of these values because you need them in a later step.

  4. To add the user, click Add. The new user is listed on the Users page.

Create a web page

  1. On your local machine, create a new file named index.html.

  2. In the HTML file, add two basic HTML containers:

    <div>Identity Platform Quickstart</div>
    <div id="message">Loading...</div>
    

Initialize the Identity Platform Client SDK with your API key

  1. In the console, go to the Identity Providers page.

    Go to Identity Providers

  2. Click Application setup details.

  3. Copy the initialization code into index.html. It looks similar to the following:

    <script src="https://www.gstatic.com/firebasejs/9.9.3/firebase.js"></script>
    <script>
      // Initialize Identity Platform
      var config = {
        apiKey: "abcdefg123456",
        authDomain: "myproject.firebaseapp.com"
      };
      firebase.initializeApp(config);
    </script>
    

Sign in the user (v9 SDK and higher)

Step 1: Install the SDK and initialize Firebase

This page describes setup instructions for version 9 of the Firebase JS SDK, which uses a JavaScript Module format.

This workflow uses npm and requires module bundlers or JavaScript framework tooling because the v9 SDK is optimized to work with module bundlers to eliminate unused code (tree-shaking) and decrease SDK size.

  1. Install Firebase using npm:

    npm install firebase
  2. Initialize Firebase in your app and create a Firebase App object:

    import { initializeApp } from 'firebase/app';
    
    // TODO: Replace the following with your app's Firebase project configuration
    const firebaseConfig = {
      //...
    };
    
    const app = initializeApp(firebaseConfig);
    

    A Firebase App is a container-like object that stores common configuration and shares authentication across Firebase services. After you initialize a Firebase App object in your code, you can add and start using Firebase services.

Step 2: Access Identity Platform in your Javascript

Now that you have initialized the Firebase SDK, you can use it anywhere in your app. For example, here is a simple app that attempts to sign in a hard-coded user and display the result on a web page.

import { initializeApp } from 'firebase/app';
import {
   onAuthStateChanged,
   signInWithEmailAndPassword,
   getAuth
} from 'firebase/auth';

const firebaseConfig = {/** Your app config */ };
const app = initializeApp(firebaseConfig);
const auth = getAuth(app, {/* extra options */ });

document.addEventListener("DOMContentLoaded", () => {
   onAuthStateChanged(auth, (user) => {
       if (user) {
           document.getElementById("message").innerHTML = "Welcome, " + user.email;
       }
       else {
           document.getElementById("message").innerHTML = "No user signed in.";
       }
   });
   signIn();
});

function signIn() {
   const email = "example@gmail.com";
   const password = "hunter2";
   signInWithEmailAndPassword(auth, email, password)
       .catch((error) => {
           document.getElementById("message").innerHTML = error.message;
       });
}

Step 3: Use a module bundler for size reduction

The Firebase Web SDK is designed to work with module bundlers to remove any unused code (tree-shaking). We strongly recommend using this approach for production apps. Tools such as the Angular CLI, Next.js, Vue CLI, or Create React App automatically handle module bundling for libraries installed through npm and imported into your codebase.

See our guide Using module bundlers with Firebase for more information.

Step 4: Import your Javascript into your page

  1. Create a new file named index.html.

  2. Add two basic HTML containers and import the bundled js generated from Step 3: Use a module bundler for size reduction.

    <script defer src="js/bundled.js"></script>
    <div>Identity Platform Quickstart</div>
    <div id="message">Loading...</div>
    
  3. Launch index.html in your web browser. A welcome message displaying your user's email appears.

Sign in the user (v8 SDK)

  1. To sign in the user, copy the following code into the index.html file:

    <script>
      var email = "EMAIL_ID";
      var password = "PASSWORD";
    
      firebase.auth().onAuthStateChanged(function(user) {
        if (user) {
          document.getElementById("message").innerHTML = "Welcome, " + user.email;
        } else {
          document.getElementById("message").innerHTML = "No user signed in.";
        }
      });
    
      firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
        document.getElementById("message").innerHTML = error.message;
      });
    </script>
    

    This code calls signInWithEmailAndPassword() and then processes the result with the onAuthStateChanged() callback.

    Replace the following:

    • EMAIL_ID: the email for the user that you created earlier
    • PASSWORD: the password for the user that you created earlier
  2. Open index.html in your web browser. A welcome message displaying your user's email appears.

You've signed in your first user with Identity Platform.

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

Delete the provider and user

If you used an existing Google Cloud project, delete the provider and user that you created to avoid incurring charges to your account:

  1. In the console, go to the Identity Providers page.

    Go to Identity Providers

  2. To delete the provider, click Delete next to the name of the provider. To confirm, click Delete.

  3. In the console, go to the Users page.

    Go to Users

  4. To delete the user that you created, click Delete next to the name of the user. To confirm, click Delete.

Delete the project

The easiest way to eliminate billing is to delete the project that you created for the tutorial.

To delete the project:

  1. In the console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

What's next

In a real app, your users would sign up using a dedicated registration page, and then sign in by entering their emails and passwords. Identity Platform offers a pre-built authentication UI that you can use for these pages, or you can build your own. You might also want to support additional sign-in methods, such as social providers (like Facebook or Google), phone numbers, OIDC, or SAML.

Learn more about the following: