代码之家  ›  专栏  ›  技术社区  ›  Zar E Ahmer

Firebase身份验证回调不工作

  •  0
  • Zar E Ahmer  · 技术社区  · 6 年前

    使用电子邮件和密码登录 打电话但不回任何电话 callback 对于 addOnFailureListener addOnCompleteListener

    我做错什么了。?? distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip 在里面 格拉德尔-包装器.properties 是最新的。。

    应用程序

    buildscript {
    
        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.1.3'
            classpath 'com.google.gms:google-services:4.0.1'
    
    
            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
    }
    
    allprojects {
        repositories {
            google()
            jcenter()
        }
    }
    
    task clean(type: Delete) {
        delete rootProject.buildDir
    }
    

    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 27
        defaultConfig {
            applicationId "firebase.learning"
            minSdkVersion 16
            targetSdkVersion 27
            multiDexEnabled true
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
    
        implementation 'com.android.support:appcompat-v7:27.1.1'
        implementation 'com.android.support:recyclerview-v7:27.1.1'
        implementation 'com.android.support:design:27.1.1'
    
        implementation 'com.android.support:multidex:1.0.3'
    
        implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    
        implementation 'com.google.firebase:firebase-firestore:17.0.4'
        implementation 'com.google.firebase:firebase-auth:16.0.2'
        implementation 'com.google.android.gms:play-services-auth:15.0.1'
    
    
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    }
    // Add to the bottom of the file
    apply plugin: 'com.google.gms.google-services'
    

    符号活动类

    public class SignInActivity extends AppCompatActivity {
    
        private static final String TAG = "SignInActivity";
        private EditText inputEmail, inputPassword;
        private ProgressBar progressBar;
        private Button btnSignup, btnLogin, btnReset;
    
        private FirebaseAuth mAuth;
        private FirebaseAuth.AuthStateListener mAuthListener;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            //Get Firebase auth instance
            mAuth = FirebaseAuth.getInstance();
    
            if (mAuth.getCurrentUser() != null) {
                startActivity(new Intent(SignInActivity.this, AddQuestionActivity.class));
                finish();
            }
    
            // set the view now
            setContentView(R.layout.activity_sign_in);
    
            mAuthListener = new FirebaseAuth.AuthStateListener() {
                @Override
                public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                    FirebaseUser user = firebaseAuth.getCurrentUser();
    
                    if (user != null) {
                        // User is signed in
                        //redirect
                        ///updateUI(user);
    
                    } else {
                        // User is signed out
                        Log.d(TAG, "onAuthStateChanged:signed_out");
                        ///updateUI(null);
                    }
    
                }
            };
    
            inputEmail = (EditText) findViewById(R.id.email);
            inputPassword = (EditText) findViewById(R.id.password);
            progressBar = (ProgressBar) findViewById(R.id.progressBar);
            btnSignup = (Button) findViewById(R.id.btn_signup);
            btnLogin = (Button) findViewById(R.id.btn_login);
            btnReset = (Button) findViewById(R.id.btn_reset_password);
    
            //Get Firebase auth instance
            mAuth = FirebaseAuth.getInstance();
    
            btnSignup.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ///startActivity(new Intent(SignInActivity.this, AddQuestionActivity.class));
                }
            });
    
            btnReset.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ///startActivity(new Intent(LoginActivity.this, ResetPasswordActivity.class));
                }
            });
    
            btnLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String email = inputEmail.getText().toString();
                    final String password = inputPassword.getText().toString();
    
                    if (TextUtils.isEmpty(email)) {
                        Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();
                        return;
                    }
    
                    if (TextUtils.isEmpty(password)) {
                        Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();
                        return;
                    }
    
                    progressBar.setVisibility(View.VISIBLE);
    
    
                    mAuth.signInWithEmailAndPassword(email,password)
                            .addOnFailureListener(SignInActivity.this, new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Toast.makeText(SignInActivity.this,"Failure",Toast.LENGTH_LONG).show();
                                }
                            })
                            .addOnCompleteListener(SignInActivity.this, new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    if (task.isSuccessful()) {
                                        // Sign in success, update UI with the signed-in user's information
                                        Log.d(TAG, "signInWithEmail:success");
                                        FirebaseUser user = mAuth.getCurrentUser();
                                        ///updateUI(user);
    
                                        Intent intent = new Intent(SignInActivity.this, AddQuestionActivity.class);
                                        startActivity(intent);
                                        finish();
                                    } else {
                                        // If sign in fails, display a message to the user.
                                        Log.w(TAG, "signInWithEmail:failure", task.getException());
                                        Toast.makeText(SignInActivity.this, "Authentication failed.",
                                                Toast.LENGTH_SHORT).show();
                                        ///updateUI(null);
                                    }
                                }
                            });
    
                    //authenticate user
                    /*auth.signInWithEmailAndPassword(email, password)
                            .addOnCompleteListener(SignInActivity.this, new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    // If sign in fails, display a message to the user. If sign in succeeds
                                    // the auth state listener will be notified and logic to handle the
                                    // signed in user can be handled in the listener.
                                    progressBar.setVisibility(View.GONE);
                                    if (!task.isSuccessful()) {
                                        // there was an error
                                        if (password.length() < 6) {
                                            inputPassword.setError("Password too short, enter minimum 6 characters!");
                                        } else {
                                            Toast.makeText(SignInActivity.this, "Authentication failed, check your email and password or sign up", Toast.LENGTH_LONG).show();
                                        }
                                    } else {
                                        Intent intent = new Intent(SignInActivity.this, AddQuestionActivity.class);
                                        startActivity(intent);
                                        finish();
                                    }
                                }
                            });*/
                }
            });
        }
    
        @Override
        protected void onStart() {
            super.onStart();
            mAuth.addAuthStateListener(mAuthListener);
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            if (mAuthListener != null) {
                mAuth.removeAuthStateListener(mAuthListener);
            }
        }
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Alex Mamo    6 年前

    为了解决这个问题,请在 build.gradle 归档以下依赖项:

        implementation 'com.google.firebase:firebase-core:16.0.1'
    

    你的app gradle文件现在必须显式列出 com.google.firebase:firebase-core 作为Firebase服务按预期工作的依赖项。

    别忘了加入你的顶层 文件以下Google服务插件:

    classpath 'com.google.gms:google-services:4.0.2'