Google Sign-in in Flutter without Firebase
Overview
This article will demonstrate how to set up Google OAuth and Google Sign in the flutter application using the Google Cloud Platform and not using firebase.
Why should you use Google Sign-in or any other login method?
When your users sign in with their Google account, the same account they use for Gmail, Photos and other Google services, they are relieved of the effort of logging in.
User passwords are not required with OAuth2, a secure authentication technique that employs tokens.
Users are not required to establish a new account or remember or manage a new password.
How to Setup Google Sign-In and OAuth2?
The steps are as follows:
- Navigate to Google Cloud Console
- Click select project and create a new project (enter the required and intended fields) then select the project.
3. Search the OAuth consent screen, select ‘external’ and click create.
4. In the edit app registration section, fill the following details and proceed with saving and continue.
5. Add scopes and Test users if any.
6. Summary would look like this.
7. Now navigate to credentials, then click on create credentials and select OAuth Client ID.
8. Create OAuth Client Id by fill the details, it will require an SHA-1 certificate fingerprint and package name.
9. Find Package name (In Flutter Directory, go to the android, open app folder and click build.gradle then move to android section)
10. How to obtain an SHA-1 Certificate fingerprint?
You’ll need to follow below steps:
Go to URL — https://flutter.dev/docs/deployment/android and follow instructions for Create an upload keystore.
Replace alias for better readability:
keytool -genkey -v -keystore c:\Users\USER_NAME\upload-keystore.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias upload
With
keytool -genkey -v -keystore c:\Users\USER_NAME\upload-keystore.jks -storetype JKS -keyalg RSA -keysize 2048 -validity 10000 -alias androiddebugkey
i. run the command anywhere on the desktop and it will create jks file
ii. moves this file, android folder> app of the flutter project directory.
iii. update build.gradle file by adding the below code under the android section:
iv. Open Flutter project directory and navigate to the android directory, Open cmd or PowerShell and run command ./gradlew signingReport. Use SHA-1 certificate fingerprint in the OAuth Client ID section.
11. After creating OAuth Client, it will open a popup that shows the client ID.
12. Now open your flutter project in the IDE.
i. Add google_signin_in package in pubspec.yaml
ii. Create a class googleSignInService:
iii. Use this GoogleSignInService class by import in the intended dart file. Here I created a method googleSignIn in the Login class.
iv. For log out, you’ll need to call a logout method of GoogleSignInService class.
By registering our application on the google cloud platform, creating credentials and configuring the flutter application. Now, the Flutter application is ready with google sign-in capabilities.
I hope you found this article useful.
Feel free to connect with me on LinkedIn