我正在使用Firebase AuthUI登录我的应用程序,特别是使用Facebook。我已在我的控制台中验证我的登录成功,因为我看到用户已添加。
但是,我的回调方法没有触发,我已经实现了toast,我不知道为什么没有调用success方法。我已记录错误代码,不知道它表示什么问题。我是否正确捕获错误?它生成-1,我在Firebase AuthUI docs中没有看到:
public class LoginActivity extends AppCompatActivity {
private static final int RC_SIGN_IN = 123;
private static final String USERS = "Users";
private FirebaseAuth mAuth;
FirebaseDatabase mBaseRef;
DatabaseReference mUserRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mAuth = FirebaseAuth.getInstance();
mBaseRef = FirebaseDatabase.getInstance();
mUserRef = mBaseRef.getReference(USERS);
startActivityForResult(AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build(),
new AuthUI.IdpConfig.FacebookBuilder().build()))
.build(), RC_SIGN_IN);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// RC_SIGN_IN is the request code you passed into startActivityForResult(...) when starting the sign in flow.
if (requestCode == RC_SIGN_IN) {
IdpResponse response = IdpResponse.fromResultIntent(data);
Log.v("RESPONSE", String.valueOf(response.getErrorCode()));
// Successfully signed in
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(), "SIGNED IN SUCCESSFULLY", Toast.LENGTH_LONG).show();
if (mAuth.getCurrentUser() != null) {
HashMap<String, Object> map = new HashMap<>();
map.put("TEST USER ", mAuth.getCurrentUser());
mUserRef.push().updateChildren(map);
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
} else {
//not signed in
Toast.makeText(getApplicationContext(), "SIGN IN FAILED", Toast.LENGTH_LONG).show();
return;
}
} else {
// Sign in failed
if (response == null) {
// User pressed back button
Toast.makeText(getApplicationContext(), "SIGN IN CANCELLED", Toast.LENGTH_LONG).show();
return;
}
if (response.getErrorCode() == ErrorCodes.NO_NETWORK) {
Toast.makeText(getApplicationContext(), "NO INTERNET CONNECTION", Toast.LENGTH_LONG).show();
return;
}
if (response.getErrorCode() == ErrorCodes.UNKNOWN_ERROR) {
Toast.makeText(getApplicationContext(), "UNKNOWN ERROR", Toast.LENGTH_LONG).show();
return;
}
}
}
}
}
编辑:我添加了完整的活动
编辑:我的日志运行得非常快,然后错误会在我读取之前自动删除,它实际上可能是StackOverflow。
答案 0 :(得分:2)
错误代码仅在通过检查onActivityResult
结果代码实际发生错误时才有效,否则它只会返回-1(Activity.RESULT_OK
),如您所注意到的那样。以下是sample:
private void handleSignInResponse(int resultCode, Intent data) {
IdpResponse response = IdpResponse.fromResultIntent(data);
// Successfully signed in
if (resultCode == RESULT_OK) {
startSignedInActivity(response);
finish();
} else {
// Sign in failed
if (response == null) {
// User pressed back button
showSnackbar(R.string.sign_in_cancelled);
return;
}
if (response.getError().getErrorCode() == ErrorCodes.NO_NETWORK) {
showSnackbar(R.string.no_internet_connection);
return;
}
showSnackbar(R.string.unknown_error);
Log.e(TAG, "Sign-in error: ", response.getError());
}
}