Skip to content

utsmannn/EasyGoogleLogin

Repository files navigation

Easy Google Login

Simplify Firebase Authenticate Google Sign In

Download

Youtube simple tutorial

https://www.youtube.com/watch?v=Gic51O8fZxY

Setup google plugin and download library

1. Register your app in firebase auth

  • Create new project in firebase console
  • Add your app and register with SHA-1 in setting project
  • Download copy google-services.json to root folder app
  • Enable Google Sign-in method in authentication

2. Add plugin google-services

  • Add this code your build.gradle files in depedencies (project level) to use the plugin
classpath 'com.google.gms:google-services:4.2.0'

Like this

buildscript {
  dependencies {
    ....
    // Add this line
    classpath 'com.google.gms:google-services:4.2.0'
  }
}

2. Apply plugin google-services in root build.gradle (app level)

apply plugin: 'com.google.gms.google-services'

Like this

apply plugin: 'com.android.application'
....
// add this line
apply plugin: 'com.google.gms.google-services'

3. Download this library, add in dependencies in build.gradle (app level)

implementation 'com.utsman:easygooglelogin:1.0.6'

Setup Google Sign-In

Define class EasyGoogleLogin and setup init in onCreate

private EasyGoogleLogin googleLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    // define class EasyGoogleLogin
    googleLogin = new EasyGoogleLogin(this);
    
    // setup
    String token = getString(R.string.default_web_client_id) // generate token google services
    googleLogin.initGoogleLogin(token, listener)
    
    // call login in button
    Button btnLogin = findViewById(R.id.btn_login)
    btnLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        
            // start login
            googleLogin.signIn(context);
        }
    });
}

Override onStart and set initOnStart

@Override
protected void onStart() {
    super.onStart();
    // add this line
    googleLogin.initOnStart();
}

Last one, override onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // add this line
    googleLogin.onActivityResult(this, requestCode, data);
}

Listener

Write ui changing in listener. Set listener with LoginResultListener(). Setup like this

googleLogin.initGoogleLogin(token, new LoginResultListener() {
    @Override
    public void onLoginSuccess(FirebaseUser user) {
    }

    @Override
    public void onLoginFailed(Exception exception) {
    }

    @Override
    public void onLogoutSuccess(Task<Void> task) {
    }

    @Override
    public void onLogoutError(Exception exception) {
    }
});

Or implement your activity with listener

public class MainActivity extends AppCompatActivity implements LoginResultListener

Override onLoginSuccess, onLoginFailed, onLogoutSuccess and onLogoutError
And change your setup on onCreate like this

googleLogin.initGoogleLogin(token, this) // `this` for LoginResultListener

Logout and Revoke

Logout with

googleLogin.signOut(context);

Revoke with

googleLogin.revokeAccess(context);

Google Sign In Button (Optional)

For add button sign in with style, download firebase-ui in depedencies

implementation 'com.firebaseui:firebase-ui-auth:4.3.0'

After sync, add this xml

<com.google.android.gms.common.SignInButton
    android:id="@+id/sign_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

And Cast to SignInButton

SignInButton signInButton = findViewById(R.id.sign_button);

Simple example

MainActivity.java


License

LICENSE

About

Library for Simplify Firebase Authenticate Google Auth

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages