BenGoBlue05 / UdacityAlumni

Udacity Alumni Android App
57 stars 30 forks source link

SignIn Button not working #59

Closed knightcube closed 6 years ago

knightcube commented 6 years ago

Steps to reproduce

1)In the LoginActivity.java when i make the following changes- a) Remove this line - findViewById(R.id.sign_in_button).setOnClickListener(this); b)And use @OnClick annotation on sign_in_button because we are using butterknife library why doesn't the button respond to the click?

Expected behavior

Button click should be noticed

Actual behavior

Button does not respond. There is no crash as such. It is just a doubt

Code Sample

I changed the LoginActivity.java file to this -

package com.google.developer.udacityalumni.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.developer.udacityalumni.R;
import com.google.developer.udacityalumni.model.User;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GoogleAuthProvider;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import butterknife.OnClick;

/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends BaseActivity implements GoogleApiClient.OnConnectionFailedListener {

    private static final String LOG_TAG = LoginActivity.class.getSimpleName();

    private static final int RC_SIGN_IN = 9001;
    private GoogleApiClient mGoogleApiClient;
    private FirebaseAuth mAuth;
    private DatabaseReference mDatabase;
    private FirebaseAuth.AuthStateListener mAuthListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getWindow().setStatusBarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
        mAuth = FirebaseAuth.getInstance();
        mDatabase = FirebaseDatabase.getInstance().getReference();
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, this /* OnConnectionFailedListener */)
                .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                .build();

        mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if (user != null) bindUserValues(user);
            }
        };
    }

    private void bindUserValues(@NonNull FirebaseUser user) {
        DatabaseReference ref = mDatabase.child("users").child(user.getUid());
        String displayName = user.getDisplayName();
        String email = user.getEmail();
        String photoUrl = user.getPhotoUrl() != null ? user.getPhotoUrl().toString() : null;
        User usr = new User(displayName, email, photoUrl);
        ref.setValue(usr);
    }

    @OnClick(R.id.sign_in_button)
    public void signInButtonClicked(){
        Log.i("TAG", "signInButtonClicked: ");
        signIn();
    }

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

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

        if (requestCode == RC_SIGN_IN) {
            GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
            if (result.isSuccess()) {
                GoogleSignInAccount account = result.getSignInAccount();
                firebaseAuthWithGoogle(account);
            } else {
                Log.e(LOG_TAG, "Google Sign In failed.");
            }
        }
    }

    private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
        Log.d(LOG_TAG, "firebaseAuthWithGoogle:" + acct.getId());
        showProgressDialog();
        AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.i(LOG_TAG, "signIn:onComplete:" + task.isSuccessful());
                        hideProgressDialog();
                        if (!task.isSuccessful()) {
                            Log.i(LOG_TAG, "signInWithCredential", task.getException());
                            Toast.makeText(LoginActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                        startActivity(new Intent(LoginActivity.this, MainActivity.class));
                    }
                });
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        // An unresolvable error has occurred and Google APIs (including Sign-In) will not
        // be available.
        Log.d(LOG_TAG, "onConnectionFailed:" + connectionResult);
        Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(mAuthListener);
    }

    @Override
    public void onStop() {
        super.onStop();
        if (mAuthListener != null) {
            mAuth.removeAuthStateListener(mAuthListener);
        }
    }
}
imGurpreetSK commented 6 years ago

I'm getting the following error, followed by sign in failed. The interesting thing is that after the app process is killed and app is relaunced, I'm logged in and can use the application.

02-27 14:32:22.350 6173-6173/com.google.developer.udacityalumni W/System.err: com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.
02-27 14:32:22.353 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzevs.zzf(Unknown Source)
02-27 14:32:22.353 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzeoa.zzc(Unknown Source)
02-27 14:32:22.353 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzeoa.zzb(Unknown Source)
02-27 14:32:22.353 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzenf.zzb(Unknown Source)
02-27 14:32:22.353 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzett.zze(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzett.zzb(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzetv.zzb(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzetd.zza(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzetd.zza(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzetd.zza(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzeth.zzb(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzevi.zza(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzfmg.zza(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at io.grpc.internal.zzv.zza(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at io.grpc.internal.zzx.zza(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at io.grpc.internal.zzx.zza(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at io.grpc.internal.zzx$zza.zzc(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at io.grpc.internal.zzx$zza.zza(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at io.grpc.internal.zzad.zzdep(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at io.grpc.internal.zzaq.run(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at io.grpc.internal.zzeo.run(Unknown Source)
02-27 14:32:22.354 6173-6173/com.google.developer.udacityalumni W/System.err:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
02-27 14:32:22.355 6173-6173/com.google.developer.udacityalumni W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-27 14:32:22.355 6173-6173/com.google.developer.udacityalumni W/System.err:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
02-27 14:32:22.355 6173-6173/com.google.developer.udacityalumni W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
02-27 14:32:22.355 6173-6173/com.google.developer.udacityalumni W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
02-27 14:32:22.355 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzeuu$zza.run(Unknown Source)
02-27 14:32:22.355 6173-6173/com.google.developer.udacityalumni W/System.err:     at java.lang.Thread.run(Thread.java:818)
02-27 14:32:22.355 6173-6173/com.google.developer.udacityalumni W/System.err: Caused by: com.google.android.gms.internal.zzfok: PERMISSION_DENIED: Missing or insufficient permissions.
02-27 14:32:22.355 6173-6173/com.google.developer.udacityalumni W/System.err:     at com.google.android.gms.internal.zzfof.zzddl(Unknown Source)
02-27 14:32:22.355 6173-6173/com.google.developer.udacityalumni W/System.err:   ... 28 more

@knightcube Do you face a similar issue?

knightcube commented 6 years ago

No your error happened because you didn't give permissions in your firebase auth tab

knightcube commented 6 years ago

https://stackoverflow.com/questions/37477644/firebase-permission-denied-error/37477852

imGurpreetSK commented 6 years ago

Thanks for that! It's quite astonishing how the stack trance point to the newer FireStore for permissions and not to the realtime database.

knightcube commented 6 years ago

by the way my issue is actually resolved....i just did a silly error...i didnt bind the views....silly of me!...i will close this issue right away.