Attempt to invoke virtual method 'java.lang.String com.google.android.gms.auth.api.signin.GoogleSignInAccount.getIdToken()' on a null object reference com.pravin.yashlalit.msbtestudymaterial.Authentications.SigninActivity.FirebaseGoogleAuth

Attempt to invoke virtual method 'java.lang.String com.google.android.gms.auth.api.signin.GoogleSignInAccount.getIdToken()' on a null object reference com.pravin.yashlalit.msbtestudymaterial.Authentications.SigninActivity.FirebaseGoogleAuth

4.1
17

                                    public class SigninActivity extends AppCompatActivity {

    SignInButton signInButton;
    GoogleSignInClient mGoogleSignInClient;
    String TAG="SigninActivity";
    FirebaseAuth mAuth;
    int RC_SIGN_IN =1;
    String personName,personGivenName,personEmail,personId;
    Uri personPhoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signin);

        initializations();

    }

    private void initializations() {
        signInButton = findViewById(R.id.google_btn);
        mAuth = FirebaseAuth.getInstance();

        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        mGoogleSignInClient = GoogleSignIn.getClient(this,gso);

        signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                signIn();
            }
        });
    }

    private void signIn() {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent,RC_SIGN_IN);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode==RC_SIGN_IN)
        {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleSignInResult(task);
        }
    }

    private void handleSignInResult(Task<GoogleSignInAccount> task) {
        try{
            GoogleSignInAccount acc = task.getResult(ApiException.class);
            Toast.makeText(getApplicationContext(),"Signing Success",Toast.LENGTH_SHORT).show();
            FirebaseGoogleAuth(acc);
        }catch(ApiException e)
        {
            Toast.makeText(getApplicationContext(),"Signing FAiled",Toast.LENGTH_SHORT).show();
            FirebaseGoogleAuth(null);
        }
    }

    private void FirebaseGoogleAuth(GoogleSignInAccount acc) {
        AuthCredential authCredential = GoogleAuthProvider.getCredential(acc.getIdToken(),null);
        mAuth.signInWithCredential(authCredential).addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                  if (task.isSuccessful())
                  {
                      Toast.makeText(getApplicationContext(),"Firebase Success",Toast.LENGTH_SHORT).show();
                      FirebaseUser user = mAuth.getCurrentUser();
                      updateUI(user);
                  }
                  else
                  {
                      Toast.makeText(getApplicationContext(),"Firebase Failed",Toast.LENGTH_SHORT).show();
                      updateUI(null);
                  }
            }
        });
    }

    private void updateUI(FirebaseUser user) {
        GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(getApplicationContext());
        if (account != null)
        {
             personName = account.getDisplayName();
             personGivenName = account.getGivenName();
             personEmail = account.getEmail();
             personId = account.getId();
             personPhoto = account.getPhotoUrl();

          /*  Toast.makeText(getApplicationContext(),"User Information:" +
                      "\nName: "+personName+
                    "\nGiven: "+personGivenName+
                    "\nEmail: "+personEmail+
                    "\nId: "+personId+
                    "\nPhoto: "+personPhoto.toString(),
                    Toast.LENGTH_SHORT).show();*/

            sendUserToSetupActivity(personName,personEmail,personPhoto);

        }


    }

    private void sendUserToSetupActivity(String personName, String personEmail, Uri personPhoto) {
        startActivity(new Intent(getApplicationContext(),SetupActivity.class)
                        .putExtra("Name",personName)
                        .putExtra("Email",personEmail)
                        .putExtra("Photo",personPhoto.toString())
                     );
        this.finish();

    }

    @Override
    protected void onStart() {
        super.onStart();

        FirebaseUser currentUser = mAuth.getCurrentUser();

        if (currentUser != null) {
            sendUserToMainActivity();
        }
    }
    private void sendUserToMainActivity() {
        startActivity(new Intent(getApplicationContext(), MainActivity.class));
        this.finish();
    }
}

4.1 (10 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source