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 Cloud console, click Guide me:
The following sections take you through the same steps as clicking Guide me.
Before you begin
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
Enable Identity Platform
In the Cloud console, go to the Identity Platform page in the Cloud Marketplace.
Click Enable Identity Platform.
Configure an email sign-in
Go to the Identity Providers page.
On the Identity Providers page, click Add a provider.
In the Select a provider list, select
Email/Password.Click the Enabled toggle to on.
To save the provider settings, click Save.
Create a user
In the Cloud console, go to the Users page.
Click Add user.
In the Email field, enter an email and password. Note both of these values because you need them in a later step.
To add the user, click Add. The new user is listed on the Users page.
Create a web page
On your local machine, create a new file named
index.html
.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
In the Cloud console, go to the Identity Providers page.
Click Application setup details.
Copy the initialization code into
index.html
. It looks similar to the following:<script src="https://www.gstatic.com/firebasejs/9.8.1/firebase.js"></script> <script> // Initialize Identity Platform var config = { apiKey: "abcdefg123456", authDomain: "myproject.firebaseapp.com" }; firebase.initializeApp(config); </script>
Sign in the user
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 theonAuthStateChanged()
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
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:
In the Cloud console, go to the Identity Providers page.
To delete the provider, click
Delete next to the name of the provider. To confirm, click Delete.In the Cloud console, go to the Users page.
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:
- In the Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- 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:
- Sign in users with JavaScript, Android, iOS, C++, or Unity.
- Sign in users with OIDC or SAML.
- Migrate users from your existing app to Identity Platform.