Android应用程序的匿名身份验证流程?

时间:2017-01-08 06:24:26

标签: android firebase firebase-authentication

我正在尝试实施匿名身份验证,然后对数据说明进行Google身份验证。

以下是匿名身份验证的代码

mAuthListener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                user = firebaseAuth.getCurrentUser();
                if (user != null) {
                    // User is signed in
                    userNameText.setText(user.getDisplayName());
                    Picasso.with(MainActivity.this).load(user.getPhotoUrl()).into(profileImageView);
                    Log.d(TAG, "onAuthStateChanged: " + user.getDisplayName());
                } else {
                    // User is signed out
                    Log.d(TAG, "onAuthStateChanged: user is null");
                    mAuth.signInAnonymously()
                            .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {
                                @Override
                                public void onComplete(@NonNull Task<AuthResult> task) {
                                    Log.d(TAG, "signInAnonymously: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.d(TAG, "signInAnonymously", task.getException());
                                        Toast.makeText(MainActivity.this, "Authentication failed.",
                                                Toast.LENGTH_SHORT).show();
                                    }

                                    // ...
                                }
                            });
                }
            }
        };

然后用户可以选择使用Google帐户登录

AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
        mAuth.getCurrentUser().linkWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        Log.d(TAG, "linkWithCredential: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()) {
                            task.getException().printStackTrace();
                            Toast.makeText(MainActivity.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                });

这按预期运行。但即使在应用程序重启后,我也没有收到user.getDisplayName。在firebase控制台中,它显示链接到匿名帐户的用户。 而且,当同一用户重新安装应用程序时,会创建一个新的匿名用户,但这次当用户选择与之前相同的Google帐户时,它不会链接到控制台中已有的用户。请帮助。

1 个答案:

答案 0 :(得分:0)

您的问题是recently addressed on firebase-talk(我发现它有“firebase匿名身份验证应用重新安装”的搜索。)

匿名身份验证帐户不会在应用卸载过程中保留。卸载应用程序时,将删除本地保存的所有内容,包括标识该帐户的匿名身份验证令牌。没有简单的方法可以为最终用户回收该令牌。相反,您应该鼓励最终用户使用受支持的帐户提供商完全登录,以便他们可以从所有设备登录,而无需担心丢失数据。