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 Google Cloud 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 Google Cloud 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 Google Cloud 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 Google Cloud 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.8.4/firebase.js"></script>
    <script>
      // Initialize Identity Platform
      var config = {
        apiKey: "abcdefg123456",
        authDomain: "myproject.firebaseapp.com"
      };
      firebase.initializeApp(config);
    </script>
    

Sign in the user

  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 Google Cloud 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 Google Cloud 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 Google Cloud 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: