- Authenticate Using Google Sign-In with JavaScript
- Before you begin
- Handle the sign-in flow with the Firebase SDK
- Web version 9
- Web version 8
- Web version 9
- Web version 8
- Web version 9
- Web version 8
- Web version 9
- Web version 8
- Web version 9
- Web version 8
- Web version 9
- Web version 8
- Web version 9
- Web version 8
- Handling account-exists-with-different-credential Errors
- Popup mode
- Redirect mode
- Advanced: Handle the sign-in flow manually
- Web version 9
- Web version 8
- Web version 9
- Web version 8
- Advanced: Authenticate with Firebase in Node.js
- Web version 9
- Web version 8
- Authenticate with Firebase in a Chrome extension
- Customizing the redirect domain for Google sign-in
- Next steps
- Web version 9
- Web version 8
- Get Started with Firebase Authentication on Android
- Connect your app to Firebase
- Add Firebase Authentication to your app
- Kotlin+KTX
- (Optional) Prototype and test with Firebase Local Emulator Suite
- Authenticate Using Google Sign-In on Android
- Before you begin
- Kotlin+KTX
- Authenticate Using Google Sign-In on Android
- Before you begin
- Kotlin+KTX
- Authenticate Using Google Sign-In on Android
- Before you begin
- Kotlin+KTX
Authenticate Using Google Sign-In with JavaScript
You can let your users authenticate with Firebase using their Google Accounts by integrating Google Sign-In into your app. You can integrate Google Sign-In either by using the Firebase SDK to carry out the sign-in flow, or by carrying out the Google Sign-In flow manually and passing the resulting ID token to Firebase.
Before you begin
- Add Firebase to your JavaScript project.
- Enable Google Sign-In in the Firebase console:
- In the Firebase console, open the Auth section.
- On the Sign in method tab, enable the Google sign-in method and click Save.
Handle the sign-in flow with the Firebase SDK
If you are building a web app, the easiest way to authenticate your users with Firebase using their Google Accounts is to handle the sign-in flow with the Firebase JavaScript SDK. (If you want to authenticate a user in Node.js or other non-browser environment, you must handle the sign-in flow manually.)
To handle the sign-in flow with the Firebase JavaScript SDK, follow these steps:
- Create an instance of the Google provider object:
Web version 9
Web version 8
Web version 9
Web version 8
Web version 9
Web version 8
Web version 9
Web version 8
Web version 9
Web version 8
This is also where you can catch and handle errors. For a list of error codes have a look at the Auth Reference Docs.
To sign in by redirecting to the sign-in page, call signInWithRedirect :
Web version 9
Web version 8
Web version 9
Web version 8
Handling account-exists-with-different-credential Errors
If you enabled the One account per email address setting in the Firebase console, when a user tries to sign in a to a provider (such as Google) with an email that already exists for another Firebase user’s provider (such as Facebook), the error auth/account-exists-with-different-credential is thrown along with an AuthCredential object (Google ID token). To complete the sign in to the intended provider, the user has to sign first to the existing provider (Facebook) and then link to the former AuthCredential (Google ID token).
Popup mode
If you use signInWithPopup , you can handle auth/account-exists-with-different-credential errors with code like the following example:
Redirect mode
This error is handled in a similar way in the redirect mode, with the difference that the pending credential has to be cached between page redirects (for example, using session storage).
Advanced: Handle the sign-in flow manually
You can also authenticate with Firebase using a Google Account by handling the sign-in flow with the Google Sign-In SDK:
- Integrate Google Sign-In into your app by following the integration guide. Be sure to configure Google Sign-In with the Google Client ID generated for your Firebase project. You can find your project’s Google Client ID in your Project’s Developers Console Credentials page. Then replace: After you integrate Google Sign-In, your web page has a Google Sign-In button configured with a callback function, as in the following example:
- In the sign-in button’s result callback, exchange the ID token from the Google’s auth response for a Firebase credential and sign-in Firebase:
Web version 9
Web version 8
Web version 9
Web version 8
Advanced: Authenticate with Firebase in Node.js
To authenticate with Firebase in a Node.js application:
- Sign in the user with their Google Account and get the user’s Google ID token. You can accomplish this in several ways. For example:
- If your app has a browser front end, use Google Sign-In as described in the Handle the sign-in flow manually section. Get the Google ID token from the auth response: Then, send this token to your Node.js app.
- If your app runs on a device with limited input capabilities, such as a TV, you can use the Google Sign-In for TVs and Devices flow.
- After you get the user’s Google ID token, use it to build a Credential object and then sign in the user with the credential:
Web version 9
Web version 8
Authenticate with Firebase in a Chrome extension
If you are building a Chrome extension app, you must add your Chrome extension ID:
- Open your project in the Firebase console.
- In the Authentication section, open the Sign-in method page.
- Add a URI like the following to the list of Authorized Domains:
Only popup operations ( signInWithPopup , linkWithPopup , and reauthenticateWithPopup ) are available to Chrome extensions, as Chrome extensions cannot use HTTP redirects. You should call these methods from a background page script rather than a browser action popup, as the authentication popup will cancel the browser action popup. The popup methods may only be used in extensions using Manifest V2. The newer Manifest V3 only allows background scripts in the form of service workers, which cannot perform the popup operations at all.
In your Chrome extension’s manifest file make sure that you add the https://apis.google.com URL to the content_security_policy allowlist.
Customizing the redirect domain for Google sign-in
On project creation, Firebase will provision a unique subdomain for your project: https://my-app-12345.firebaseapp.com .
This will also be used as the redirect mechanism for OAuth sign in. That domain would need to be allowed for all supported OAuth providers. However, this means that users may see that domain while signing in to Google before redirecting back to the application: Continue to: https://my-app-12345.firebaseapp.com.
To avoid displaying your subdomain, you can set up a custom domain with Firebase Hosting:
- Follow steps 1 through 3 in Set up your domain for Hosting. When you verify your domain ownership, Hosting provisions an SSL certificate for your custom domain.
- Add your custom domain to the list of authorized domains in the Firebase console: auth.custom.domain.com .
- In the Google developer console or OAuth setup page, whitelist the URL of the redirect page, which will be accessible on your custom domain: https://auth.custom.domain.com/__/auth/handler .
- When you initialize the JavaScript library, specify your custom domain with the authDomain field:
Next steps
After a user signs in for the first time, a new user account is created and linked to the credentials—that is, the user name and password, phone number, or auth provider information—the user signed in with. This new account is stored as part of your Firebase project, and can be used to identify a user across every app in your project, regardless of how the user signs in.
In your apps, the recommended way to know the auth status of your user is to set an observer on the Auth object. You can then get the user’s basic profile information from the User object. See Manage Users.
In your Firebase Realtime Database and Cloud Storage Security Rules, you can get the signed-in user’s unique user ID from the auth variable, and use it to control what data a user can access.
You can allow users to sign in to your app using multiple authentication providers by linking auth provider credentials to an existing user account.
To sign out a user, call signOut :
Web version 9
Web version 8
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Источник
Get Started with Firebase Authentication on Android
Connect your app to Firebase
Add Firebase Authentication to your app
By using the Firebase Android BoM, your app will always use compatible versions of the Firebase Android libraries.
(Alternative) Declare Firebase library dependencies without using the BoM
If you choose not to use the Firebase BoM, you must specify each Firebase library version in its dependency line.
Note that if you use multiple Firebase libraries in your app, we highly recommend using the BoM to manage library versions, which ensures that all versions are compatible.
Kotlin+KTX
By using the Firebase Android BoM, your app will always use compatible versions of the Firebase Android libraries.
(Alternative) Declare Firebase library dependencies without using the BoM
If you choose not to use the Firebase BoM, you must specify each Firebase library version in its dependency line.
Note that if you use multiple Firebase libraries in your app, we highly recommend using the BoM to manage library versions, which ensures that all versions are compatible.
To use an authentication provider, you need to enable it in the Firebase console. Go to the Sign-in Method page in the Firebase Authentication section to enable Email/Password sign-in and any other identity providers you want for your app.
(Optional) Prototype and test with Firebase Local Emulator Suite
Before talking about how your app authenticates users, let’s introduce a set of tools you can use to prototype and test Authentication functionality: Firebase Local Emulator Suite. If you’re deciding among authentication techniques and providers, trying out different data models with public and private data using Authentication and Firebase Security Rules, or prototyping sign-in UI designs, being able to work locally without deploying live services can be a great idea.
An Authentication emulator is part of the Local Emulator Suite, which enables your app to interact with emulated database content and config, as well as optionally your emulated project resources (functions, other databases, and security rules).
Using the Authentication emulator involves just a few steps:
- Adding a line of code to your app’s test config to connect to the emulator.
- From the root of your local project directory, running firebase emulators:start .
- Using the Local Emulator Suite UI for interactive prototyping, or the Authentication emulator REST API for non-interactive testing.
Now let’s continue with how to authenticate users.
Источник
Authenticate Using Google Sign-In on Android
You can let your users authenticate with Firebase using their Google Accounts by integrating Google Sign-In into your app.
Before you begin
Using the Firebase Android BoM, declare the dependency for the Firebase Authentication Android library in your module (app-level) Gradle file (usually app/build.gradle ).
Also, as part of setting up Firebase Authentication, you need to add the Google Play services SDK to your app.
By using the Firebase Android BoM, your app will always use compatible versions of the Firebase Android libraries.
(Alternative) Declare Firebase library dependencies without using the BoM
If you choose not to use the Firebase BoM, you must specify each Firebase library version in its dependency line.
Note that if you use multiple Firebase libraries in your app, we highly recommend using the BoM to manage library versions, which ensures that all versions are compatible.
Kotlin+KTX
By using the Firebase Android BoM, your app will always use compatible versions of the Firebase Android libraries.
(Alternative) Declare Firebase library dependencies without using the BoM
If you choose not to use the Firebase BoM, you must specify each Firebase library version in its dependency line.
Note that if you use multiple Firebase libraries in your app, we highly recommend using the BoM to manage library versions, which ensures that all versions are compatible.
If you haven’t yet specified your app’s SHA-1 fingerprint, do so from the Settings page of the Firebase console. Refer to Authenticating Your Client for details on how to get your app’s SHA-1 fingerprint.
- In the Firebase console, open the Auth section.
- On the Sign in method tab, enable the Google sign-in method and click Save.
Источник
Authenticate Using Google Sign-In on Android
You can let your users authenticate with Firebase using their Google Accounts by integrating Google Sign-In into your app.
Before you begin
Using the Firebase Android BoM, declare the dependency for the Firebase Authentication Android library in your module (app-level) Gradle file (usually app/build.gradle ).
Also, as part of setting up Firebase Authentication, you need to add the Google Play services SDK to your app.
By using the Firebase Android BoM, your app will always use compatible versions of the Firebase Android libraries.
(Alternative) Declare Firebase library dependencies without using the BoM
If you choose not to use the Firebase BoM, you must specify each Firebase library version in its dependency line.
Note that if you use multiple Firebase libraries in your app, we highly recommend using the BoM to manage library versions, which ensures that all versions are compatible.
Kotlin+KTX
By using the Firebase Android BoM, your app will always use compatible versions of the Firebase Android libraries.
(Alternative) Declare Firebase library dependencies without using the BoM
If you choose not to use the Firebase BoM, you must specify each Firebase library version in its dependency line.
Note that if you use multiple Firebase libraries in your app, we highly recommend using the BoM to manage library versions, which ensures that all versions are compatible.
If you haven’t yet specified your app’s SHA-1 fingerprint, do so from the Settings page of the Firebase console. Refer to Authenticating Your Client for details on how to get your app’s SHA-1 fingerprint.
- In the Firebase console, open the Auth section.
- On the Sign in method tab, enable the Google sign-in method and click Save.
Источник
Authenticate Using Google Sign-In on Android
You can let your users authenticate with Firebase using their Google Accounts by integrating Google Sign-In into your app.
Before you begin
Using the Firebase Android BoM, declare the dependency for the Firebase Authentication Android library in your module (app-level) Gradle file (usually app/build.gradle ).
Also, as part of setting up Firebase Authentication, you need to add the Google Play services SDK to your app.
By using the Firebase Android BoM, your app will always use compatible versions of the Firebase Android libraries.
(Alternative) Declare Firebase library dependencies without using the BoM
If you choose not to use the Firebase BoM, you must specify each Firebase library version in its dependency line.
Note that if you use multiple Firebase libraries in your app, we highly recommend using the BoM to manage library versions, which ensures that all versions are compatible.
Kotlin+KTX
By using the Firebase Android BoM, your app will always use compatible versions of the Firebase Android libraries.
(Alternative) Declare Firebase library dependencies without using the BoM
If you choose not to use the Firebase BoM, you must specify each Firebase library version in its dependency line.
Note that if you use multiple Firebase libraries in your app, we highly recommend using the BoM to manage library versions, which ensures that all versions are compatible.
If you haven’t yet specified your app’s SHA-1 fingerprint, do so from the Settings page of the Firebase console. Refer to Authenticating Your Client for details on how to get your app’s SHA-1 fingerprint.
- In the Firebase console, open the Auth section.
- On the Sign in method tab, enable the Google sign-in method and click Save.
Источник