Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int RC_SIGN_IN = 1;
- private FirebaseAuth mAuth = FirebaseAuth.getInstance();
- GoogleSignInClient mGoogleSignInClient;
- GoogleSignInOptions gso;
- gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
- .requestIdToken(getString(R.string.default_web_client_id))
- .requestEmail()
- .build();
- mGoogleSignInClient = GoogleSignIn.getClient(MainActivity.this, gso);
- Intent signInIntent = mGoogleSignInClient.getSignInIntent();
- startActivityForResult(signInIntent, RC_SIGN_IN);
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == RC_SIGN_IN) {
- Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
- try {
- GoogleSignInAccount account = task.getResult(ApiException.class);
- firebaseAuthWithGoogle(account);
- } catch (ApiException e) {
- Log.d("demo", "Google sign in failed: " + e.getMessage());
- }
- }
- }
- private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
- AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
- mAuth.signInWithCredential(credential).addOnCompleteListener(this, new OnCompleteListener < AuthResult > () {
- @Override
- public void onComplete(@NonNull Task < AuthResult > task) {
- if (task.isSuccessful()) {
- FirebaseUser user = mAuth.getCurrentUser();
- Intent intent = new Intent(MainActivity.this, TripsActivity.class);
- startActivity(intent);
- finish();
- } else {
- Log.d("demo", "Google sign in failed");
- }
- }
- });
- }
- //Logout
- FirebaseAuth.getInstance().signOut();
- gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
- .requestIdToken(getString(R.string.default_web_client_id))
- .requestEmail()
- .build();
- mGoogleSignInClient = GoogleSignIn.getClient(getActivity(), gso);
- mGoogleSignInClient.signOut()
- .addOnCompleteListener(getActivity(), new OnCompleteListener < Void > () {
- @Override
- public void onComplete(@NonNull Task < Void > task) {
- Intent intent = new Intent(getActivity(), MainActivity.class);
- startActivity(intent);
- getActivity().finish();
- }
- });
- }
Add Comment
Please, Sign In to add comment