Firebase自定义令牌身份验证回调未调用

时间:2017-05-03 19:12:04

标签: android authentication firebase firebase-authentication

我尝试使用Digits(短信/电话验证)进行身份验证,然后在成功()的回调中进行身份验证,以调用Firebase自定义令牌身份验证。好的,Digits的作品 - > Firebase会发送身份验证并实际隐藏用户,但不会调用Firebase onComplete()的回调。如果我直接调用 signInWithCustomToken()而不通过数字,它就会像它应该的那样工作, onComplete()它被调用。问题是什么?  这是我在 onCreate()

中的内容
    AuthConfig.Builder builder = new AuthConfig.Builder();
    builder.withAuthCallBack(new AuthCallback() {
        @Override
        public void success(DigitsSession session, String phoneNumber) {
            signInWithCustomToken();
        }

        @Override
        public void failure(DigitsException exception) {
        }
    });

    authConfigDigits = builder.build();

    Button digitsButton = (Button) findViewById(R.id.auth_button);
    digitsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Digits.authenticate(authConfigDigits);
        }
    });

然后我有自定义令牌身份验证方法 signInWithCustomToken()本身:

private void signInWithCustomToken() {

    String customToken = "eyJhbGciOiJSUzI1NiJ9.eyJhdWQiOiJodHRwczovL2lkZW50aXR5dG9vbGtpdC5nb29nbGVhcGlzLmNvbS9nb29nbGUuaWRlbnRpdHkuaWRlbnRpdHl0b29sa2l0LnYxLklkZW50aXR5VG9vbGtpdCIsImV4cCI6MTQ5Mzg0MDU5MiwiaWF0IjoxNDkzODM2OTkyLCJpc3MiOiJmaXJlYmFzZS1hZG1pbnNkay0xOXBqY0Btb3ZlY2VudHJhbC0zOGUzYS5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsInN1YiI6ImZpcmViYXNlLWFkbWluc2RrLTE5cGpjQG1vdmVjZW50cmFsLTM4ZTNhLmlhbS5nc2VydmljZWFjY291bnQuY29tIiwidWlkIjoic29tZS11aWQifQ.QdyviQ8vMhpMF7VJP949PdjVBwqM4EbZGxJvhCQtsRloJdIc16FPBG_RrBp2wZivWC-z1TIx1yctdMVGsoDAk5ptp-HlNyp5n31DyfZriGgo8zbhWln4RGG4wGYb7hSxGpyvm4STOM9N7TBBKt4lCZFYdpHVbT2idhr0hipxKCC_Ubjhwbjsfxrj5h59GHSyAdjw_yigNBHEtvsUfdGfzwKn_EW5qm5OAA0icCiqjnvsvW5RkwWKo7kWxqZT87-BoHsf5JguFeLGs0Ow9lgxAS2mSuuvni5qiYdLZdOKhu204Ctsty1pyFYrAWwWdnSrTWJwdkBx9xPgsE5UlipkRQ";

    mFirebaseAuth.signInWithCustomToken(customToken)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Log.e(TAG, "Task is successfull");
                        openMainActivity();
                    } else {
                        Log.e(TAG, "Task failed");
                    }
                }
            });
}

1 个答案:

答案 0 :(得分:0)

我现在通过延迟 signInWithCustomToken()的调用来解决它。 我不确定为什么它不起作用,可能是某种竞争条件,但改变了:

@Override
    public void success(DigitsSession session, String phoneNumber) {
        signInWithCustomToken();
    }

使用:

 @Override
        public void success(DigitsSession session, String phoneNumber) {
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    //100ms delay
                    signInWithCustomToken();
                }
            }, 100);

        }

修复了问题。如果有人能解释原因,我会非常感激。