Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginActivity extends AppCompatActivity {
- //a constant for detecting the login intent result
- private static final int RC_SIGN_IN = 1;
- //Tag for the logs optional
- private static final String TAG = "AndroidMads";
- //creating a GoogleSignInClient object
- GoogleSignInClient mGoogleSignInClient;
- //And also a Firebase Auth object
- FirebaseAuth mAuth;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- //first we initialized the Firebase Auth object
- mAuth = FirebaseAuth.getInstance();
- //Then we need a GoogleSignInOptions object
- //And we need to build it as below
- GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
- .requestIdToken(getString(R.string.default_web_client_id))
- .requestEmail()
- .build();
- //Then we will get the GoogleSignInClient object from GoogleSignIn class
- mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
- //Now we will attach a click listener to the sign_in_button
- //and inside onClick() method we are calling the signIn() method that will open
- //google sign in intent
- findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- //getting the google signin intent
- Intent signInIntent = mGoogleSignInClient.getSignInIntent();
- //starting the activity for result
- startActivityForResult(signInIntent, RC_SIGN_IN);
- }
- });
- }
- @Override
- protected void onStart() {
- super.onStart();
- //if the user is already signed in
- //we will close this activity
- //and take the user to profile activity
- if (mAuth.getCurrentUser() != null) {
- finish();
- startActivity(new Intent(this, ActivitySplash.class));
- }
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- //if the requestCode is the Google Sign In code that we defined at starting
- if (requestCode == RC_SIGN_IN) {
- //Getting the GoogleSignIn Task
- Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
- try {
- //Google Sign In was successful, authenticate with Firebase
- GoogleSignInAccount account = task.getResult(ApiException.class);
- //authenticating with firebase
- firebaseAuthWithGoogle(account);
- } catch (ApiException e) {
- Log.v("API Exception", e.toString());
- Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
- }
- }
- }
- private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
- Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
- //getting the auth credential
- AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
- //Now using firebase we are signing in the user here
- mAuth.signInWithCredential(credential)
- .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
- @Override
- public void onComplete(@NonNull Task<AuthResult> task) {
- if (task.isSuccessful()) {
- if (mAuth.getCurrentUser() != null) {
- startActivity(new Intent(LoginActivity.this, ActivitySplash.class));
- finish();
- }
- } else {
- // If sign in fails, display a message to the user.
- Log.w(TAG, "signInWithCredential:failure", task.getException());
- Toast.makeText(LoginActivity.this, "Authentication failed.",
- Toast.LENGTH_SHORT).show();
- }
- }
- });
- }}
Add Comment
Please, Sign In to add comment