我正在创建一个与Firebase集成的Facebook登录活动。
我已经在Firebase中激活了Facebook登录,也跟着Facebook Developer Console中的所有firebase步骤和所有Facebook步骤。
我的活动有一个com.facebook.widget.LoginButton,点击此按钮后会出现Facebook登录页面,我可以输入凭据,自动调整应用程序并登录。
但是,当我再次进入App时,会出现登录按钮,并且不会保存(缓存)Facebook登录。
Firebase身份验证用户列表也未保存。
可能有什么不对。
我的代码如下。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
setContentView(R.layout.activity_login);
mFirebaseUtils = new FirebaseUtils();
mFirebaseUtils.setFirebase();
try {
@SuppressWarnings("rawtypes")
Class strictModeClass = Class.forName("android.os.StrictMode");
Class strictModeThreadPolicyClass = Class
.forName("android.os.StrictMode$ThreadPolicy");
Object laxPolicy = strictModeThreadPolicyClass.getField("LAX").get(
null);
@SuppressWarnings("unchecked")
Method method_setThreadPolicy = strictModeClass.getMethod(
"setThreadPolicy", strictModeThreadPolicyClass);
method_setThreadPolicy.invoke(null, laxPolicy);
} catch (Exception e) {
}
// View Pager
initData();
mAuth = mFirebaseUtils.mAuth;
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in
Log.d(TAG, "onAuthStateChanged:signed_in:" + user.getUid());
} else {
// User is signed out
Log.d(TAG, "onAuthStateChanged:signed_out");
}
}
};
mCallbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.btnLoginFacebook);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
handleFacebookAccessToken(loginResult.getAccessToken());
}
@Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
}
@Override
public void onError (FacebookException error){
Log.d(TAG, "facebook:onError", error);
}
});
}
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
// [START_EXCLUDE silent]
//showProgressDialog();
// [END_EXCLUDE]
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
// 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.
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
// [START_EXCLUDE]
//hideProgressDialog();
// [END_EXCLUDE]
}
});
}
public void signOut() {
mAuth.signOut();
LoginManager.getInstance().logOut();
}
public String getAndroidVersion() {
String release = Build.VERSION.RELEASE;
int sdkVersion = Build.VERSION.SDK_INT;
return "Android SDK: " + sdkVersion + " (" + release +")";
}
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
FirebaseUser user = mAuth.getCurrentUser();
}
@Override
public void onStop() {
super.onStop();
if (mAuthListener != null) {
mAuth.removeAuthStateListener(mAuthListener);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
//case R.id.button_facebook_signout:
// signOut();
// break;
}
}
答案 0 :(得分:1)
目前尚不清楚如何检查用户是否已登录。 recommend method将使用AuthStateListener。请注意,它不仅在用户登录或注销时触发,而且在注册侦听器后立即触发。你有一个,但它只记录调试消息。
此代码不可靠,因为FirebaseAuth
的实例可能尚未完成初始化:
@Override
public void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
FirebaseUser user = mAuth.getCurrentUser(); // not reliable
}
中突出显示
注意:getCurrentUser也可能因为auth对象而返回null 还没有完成初始化。如果您使用监听器来跟踪 用户的登录状态,您无需处理此案例。
答案 1 :(得分:0)
本周我正在做同样的事情,我将你的代码与我的代码进行比较。在OnCreate中,我相信你错过了
mAuth = FirebaseAuth.getInstance();
FirebaseUser mUser = mAuth.getCurrentUser();
另外,请确保声明
private FirebaseAuth mAuth;
。您可以看到我的code here
应用级Gradle也非常重要。这给我带来了很多问题。确保你有
compile 'com.firebase:firebase-client-android:2.5.2+'
compile 'com.google.android.gms:play-services-auth:9.2.0'
compile 'com.firebaseui:firebase-ui:0.4.4'
您不需要Facebook SDK,因为它已包含在FireBase UI中。如果保留它,可能会导致一些问题。另外,把
apply plugin: 'com.google.gms.google-services'
在依赖关系的右括号下。