【问题标题】:Firebase Auth SignOut not working on UninstallFirebase Auth SignOut 不适用于卸载
【发布时间】:2018-10-19 22:59:14
【问题描述】:

我正在使用 Sign-in with Google 方法在我的应用中登录用户。

问题 - 更新

  1. 如果我卸载应用程序(在注销或未注销后)并重新安装应用程序,FirebaseAuth.getInstance().getCurrentUser() 将不为空。因此,用户可以访问该帐户。

  2. 我什至尝试通过清除应用数据,问题仍然存在。

在添加“FirebaseInstanceIdService”和“FirebaseMessagingService”服务之前,该应用运行良好。意思是卸载后自动退出。

Manifest.xml

    <meta-data   
android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="@string/default_notification_channel_id"/>
    <meta-data android:name="firebase_messaging_auto_init_enabled"
        android:value="false" />
    <meta-data android:name="firebase_analytics_collection_enabled"
        android:value="false" />

....

<service
        android:name=".services.MyFirebaseMessagingService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

    <service
        android:name=".services.MyFirebaseInstanceIDService"
        android:exported="false">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

....

MyFirebaseMessagingService.class

public class MyFirebaseMessagingService extends FirebaseMessagingService {

private static final String TAG = "MessagingService";

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    Log.e(TAG, String.valueOf(remoteMessage));
}
}

MyFirebaseInstanceIDService.class

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

private static final String TAG = "InstanceIdService";

@Override
public void onTokenRefresh() {
    super.onTokenRefresh();

    if (FirebaseAuth.getInstance().getCurrentUser() == null){
        return;
    }

    String tokenId = FirebaseInstanceId.getInstance().getToken();

    saveTokenIdToDatabase(tokenId);
}
}

signOutAccount 方法

private void signOutAccount(){

FirebaseAuth.getInstance.signOut();

            Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            finishAffinity();
}

【问题讨论】:

  • 您在使用智能锁吗?如果是这样,即使是全新安装,firebase 也会自动登录您的 Google 帐户
  • 试试这个FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { FirebaseUser user = firebaseAuth.getCurrentUser(); if (user == null) { // user auth state is changed - user is null // launch login activity startActivity(new Intent(MainActivity.this, LoginActivity.class)); finish(); } } };
  • @GuilhermeFGL 没有智能锁。我什至没有改变我的测试设备。这是在我从事 FirebaseMessagingService 之后发生的
  • @D. 不,它不起作用。
  • 请您删除这个if (FirebaseAuth.getInstance().getCurrentUser() == null){ return; }

标签: android firebase firebase-authentication firebase-cloud-messaging


【解决方案1】:

你不是唯一一个为此苦苦挣扎的人。我有类似的问题。重新安装(或更新)应用程序后,在 BaseActivity 中我检查了用户是否已登录。他是.. FirebaseAuth.getInstance().getCurrentUser() 返回非空值。因此,当我使用uid 从firebase 数据库中获取数据时,我遇到了一些乱七八糟的东西——一些随机项目。

另外请注意,我只能在某些设备上重现它。例如,我在 Nexus 5x 上获得了非空值,但在 Oneplue 3t (8.0.0) 上却没有。

经过一番调查,我发现了几个有趣的事实。

事实 #1 (docs)

在某些情况下getCurrentUser 将返回非空值 FirebaseUser,但底层令牌无效。

事实上,当getCurrentUser() != null 时,您无法确定用户是否真正登录。我的结论是,我不能依赖getCurrentUser,我应该重新考虑检查用户是否登录的方式( as an option)。

事实 #2

Android 应用有时会在重新安装应用后记住其数据,因为 自动备份。 See the post for more details.

解决方案

<application
  android:allowBackup="false"
  android:fullBackupContent="false"
  ...>

我将 android:allowBackupandroid:fullBackupContent 设置为 false。所以当我重新安装应用程序(或更新它)时,firebase 当前用户是null

【讨论】:

    猜你喜欢
    • 2020-07-12
    • 2021-06-27
    • 1970-01-01
    • 2022-01-19
    • 2017-03-26
    • 2020-11-15
    • 2023-01-15
    • 2018-07-08
    • 2016-11-06
    相关资源
    最近更新 更多