Google Sign in using firebase Android

In this blog I will show you how to add Google Sign In to your Android application with Firebase. It is very easy to follow one of the steps below. Then let’s get started.

Implementation of Google Authentication Step

  • Create an Android project
  • Configure a project and add it to the Firebase console.
  • Enable Google registration
  • Add SHA and 256 keys to the application on the Firebase console (Required step to register on Google)
  • Adding an authentication library in app-level build.gradle
  • Configuring the Google client

1. Creation of Android projects

Let’s go to Iroid Studio and make a new project. In this example application I use the package name com.googlesigninfirebase to demonstrate the Google login with firebase. Let’s go to the next step.

2. Implementation of a new project based on Firebase.

Let’s open the Firebase console and add the project. To add a project on Firebase, follow my previous post. Add Firebase manually to pp

3. Enable Google Connect

Select Authentication in the left pane. Go to the login method, select Google and enable Google SignIn. Sort ( Authentication -> Connection method -> Google -> Enable ).

4. SHA and 256 keys added to the Firebase application in the console

This is a necessary step to register with Google. Now continue with the Android Studio project. To obtain the signature of the application, click on a diploma on the right. Now select Application -> Task -> Android and choose SingReport.

Simply copy the project settings and paste them into the firewalls. You must install the two keys SHA1 and SHA-256. Now go back to your Iroid studio and move on.

See also  How to write subquery in select statement in Sql

Adding an authentication library to app-level build.gradle

Implementation of com.firebaseui: firebase-ui-auth: 4.3.1
// Google Sign In SDK (required for Google Sign In only)
Implementation of com.google.android.gms: play-services-auth: 17.0.0.

Create a new event with layouts called LoginActivity.

Open the layout file and the demo button

 

 

Sign in and enter the followingcode

package com.googlesigninfirebase ;

import android.content.intentionally;
import android.os.bundle;
import android.util.log;
import android.view.view;
import android.widget.toast;
import androidx.annotation.NonNull;
import androidx.annotation.Null;
import android.widget.toastappcompat.app.AppCompatActivity;
-Import from com.google.android.gms.auth.api.signin.GoogleSignIn;
-Import from com.google.android.gms.auth.api.signin.GoogleSignInAccount;
-Import from com.google.android.gms.auth.api.signin.GoogleSignInClient;
-Import from com.google.android.gms.auth.api.signin.GoogleSignInAccount.android.gms.auth.api.signin.GoogleSignInOptions;
-Import from com.google.android.gms.common.SignInButton;
-Import from com.google.android.gms.common.apiException;
-Import from com.google.android.gms.tasks.OnCompleteListener;
-Import from com.google.android.gms.common.apiException.tasks;
-Import from com.google.firebase.auth.AuthCredential;
-Import from com.google.firebase.auth.AuthResult;
-Import from com.google.firebase.auth.firebaseAuth;
-Import from com.google.firebase.auth.FirebaseUser;
-Import from com.google.firebase.auth.GoogleAuthProvider ;

the public LoginActivity class expands AppCompatActivity {
private static endpoint TAG = LoginActivity ;
private static endpoint int RC_SIGN_IN = 1001 ;

GoogleSignInClient GoogleSignInClient ;

Private fire station from the fire station;

Write
protected blank onCreate(storedInstanceState bundle) {
super.onCreate(storedInstanceState);
setContentView(R.layout.activity_login) ;

SignInButton signInButton = findViewById(R.id.sign_in_button) ;
signInButton.setOnClickListener(new view.OnClickListener() {
@Overwrite public space onClick(show view) {
// Start login
signInToGoogle() ;
}
}) ;

// Google Client Setup
Google Client Setup();
}

private void configureGoogleClient() {
// configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
// for requestIdToken this is included in theExml file created
// of your google-services.json
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build() ;

// Build GoogleSignInClient with the options specified by gso.
googleSignInClient = GoogleSignIn.getClient (this, gso) ;

// Set the size of the Enter key.
SignInButton signInButton = findViewById(R.id.sign_in_button) ;
signInButton.setSize(SignInButton.SIZE_WIDE) ;

// Initialization Firebase Auth
firebaseAuth = FirebaseAuth.getInstance();
}.

@Bypass
public gap onStart() {
super.onStart() ;

// Make sure the user is registered (unregistered) and update the user interface accordingly.
FirebaseUser currentUser = firebaseAuth.getCurrentUser() ;

if (currentUser != null) {
Log.d(TAG, Currently logged in: + currentUser.getEmail()));
showToastMessage();
}
}.

See also  10 Working Torrent Clients for Mac in 2024 –

public void signInToGoogle() {
Intent signInIntent = googleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, RC_SIGN_IN);
}.

@
Overwrite protected disability in ActivityResult (int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// The result returned when Intent is run from GoogleSignInApi.getSignInIntent(…);
if (requestCode == RC_SIGN_IN) {
Task Task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign Successful, authentication with Firebase
GoogleSignInAccount account = task.getResult(ApiException).class);
showToastMessage (Google Sign In successful);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI accordingly
Log.w(TAG, Google Sign In failed, e);
showToastMessage(Google Sign In failed + e);
}
}
}.

private invalid firebaseAuthMetGoogle(GoogleSignInAccount account) {
Log.d(TAG, firebaseAuthMetGoogle : + account.getId()) ;

AuthCredential credential = GoogleAuthProvider.getCredential(account.getIdToken(), null);
firebaseAuth.signInMetCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener() {
@
catch public void onComplete(@NonNull task))). {
if (task.isSuccessful()) {
// Login successful, update user interface with user data
FirebaseUser = firebaseAuth.getCurrentUser() ;

Log.d(TAG, signInCredential: success: currentUser: + user.getEmail() ;

showToastMessage(Firebase Authentication Succcceded );
launchMainActivity(user);
} else {
// If the connection fails, a message will be displayed to the user.

Log.w(TAG, signInCredential:failure, task.getException()) ;

showToastMessage(Firebase authentication failed: + task.getException());}}}.

private void showToastMessage(String message) {
Toast.makeText(LoginActivity.this, message, Toast.LENGTH_LONG).show();
}

private void startMainActivity(FirebaseUser user) {if (user != invalid) {MainActivity.startActivity(this, user.getDisplayName());finish();}}}}

Open MainActvity presentation Add a disconnect and revoke access Button

 

Go to main activity and add the logout code

package com.googlesigninfirebase ;

import android.content.Context;
import android.content.intentional;
import android.os.bundle;
import android.util.log;
import android.view.view; import
android.widget.textView; import
androidx.annotation.NonNull; import
androidx.appCompatActivity; import
com.google.android gms.auth.api. signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
-Import from com.google.android.gms.tasks.OnCompleteListener;
-Import from com.google.android.gms.tasks.task;
-Import from com.google.firebase.auth.FirebaseAuth ;

The MainActivity public class extends AppCompatActivity to View.OnClickListener {
private static endpoint TAG = MainActivity;
private static endpoint ARG_NAME = username ;

public static void startActivity (context, string username) {
Intention = new intention (context, MainActivity.class);
intent.putExtra(ARG_NAME, username);
context.startActivity(intent);
}

FirebaseAuth FirebaseAuth;
GoogleSignInClient ;

@Example
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.textViewWelcome);
if (getIntent().hasExtra(ARG_NAME))). {
textView.setText(String.format(Welcome – %s, getIntent().getStringExtra(ARG_NAME))));
}
findViewById(R.id.buttonLogout).setOnClickListener(this);
findViewById(R.id.buttonDisconnect).setOnClickListener(this) ;

See also  How To Market Your Skills As A Freelance Bookkeeper

googleSignInClient = GoogleSignIn.getClient(this, GoogleSignInOptions.DEFAULT_SIGN_IN);
firebaseAuth = FirebaseAuth.getInstance();
}.

@Clisk(view)
public invalidity check {switch
(view.getId()) { R.id.buttonAmount in case
:
signOut();
break;
case R.id.buttonDisconnect:
revokeAccess();
break;
}
}.

private void signOut() {
// Firebase out
firebaseAuth.signOut() ;

// Google is
googleSignInClient.signOut().addOnCompleteListener(this,
new OnCompleteListener()). {
@
override public gap onComplete(@NonNull task))) {
// Google login failed, update user interface to
Log.w(TAG, logged out by Google);
}
});
}.

private void revocateAccess() {
// Firebase sign out
firebaseAuth.signOut() ;

// Google revokes the access
googleSignInClient.revocateAccess().addOnCompleteListener(this,
new OnCompleteListener()). {
@
override public gap onComplete(@NonNull task))) {
// Google connection error, UI update or
Log.w(TAG, access revoked);
}
});
}
}.

Conclusion

With this Android guide, we learned to work with Google Sign In with Firebase Android. I hope this will help you, help me by sharing this message with all your friends who are learning how to develop Android applications.

Related Tags:

live video campaign,best live streaming service for business,live streaming content ideas,facebook live influencers,live streaming advertising,live content industry,live stream consulting,live streaming uses,how to market a live stream,live streaming statistics 2019,1.1 livestream genshin,how to set up a paid live stream,how to earn money from nonolive,how to make money on live me,youtube live checklist,things to live stream,facebook live registration,live streaming questions,the business of live streaming,benefits of going live on youtube,live streaming trend,marketing in streaming,target market for streaming services,netflix targeting strategy,target market for music streaming services,live stream marketing examples,brands using live streaming,video streaming brands,most successful live streams,video marketing,live stream examples