简体   繁体   English

从Android应用程序正确注销用户

[英]Properly log out a user from android app

I'm developing a small android app, and basically so far it just has login and logout functionality. 我正在开发一个小型Android应用程序,基本上到目前为止它只具有登录和注销功能。 I'm using Firebase to store user data and also for authentication. 我正在使用Firebase来存储用户数据以及身份验证。

So I have login working and it authenticates users as it should and I have logging out working in the sense that it unauthenticates users. 所以我登录工作并且它应该对用户进行身份验证,并且我已经注销了工作,因为它取消了用户的身份unauthenticates But is there anything I have to do from within the app to kill the session? 但是在应用程序中有什么我必须做的才能杀死会话吗?

if (id == R.id.action_log_out) {
    ref.unauth(); //End user session
    startActivity(new Intent(MainActivity.this, LoginActivity.class)); //Go back to home page
    finish();
}        

Will this work as I think it should? 我觉得这会起作用吗? Obviously if someone logs out they shouldn't be able to hit th back button and magically go back to the last page without re-logging in. 显然,如果有人退出,他们应该无法点击后退按钮并神奇地返回到最后一页而不重新登录。

From Firebase docs 来自Firebase文档

https://firebase.google.com/docs/auth/android/custom-auth https://firebase.google.com/docs/auth/android/custom-auth

call this FirebaseAuth.getInstance().signOut(); 调用此FirebaseAuth.getInstance().signOut();

When Firebase authenticates the user (or you authenticate the user with Firebase), it stores the token for that user in local storage on your device. 当Firebase对用户进行身份验证时(或者使用Firebase对用户进行身份验证),它会将该用户的令牌存储在设备的本地存储中。 This happens when you call one of the authWith... methods (of course only if it successfully authenticates the user). 当您调用其中一个authWith...方法时,会发生这种情况(当然,只有当它成功验证用户身份时)。

Calling ref.unauth(); 调用ref.unauth(); immediately deletes that token from local storage. 立即从本地存储中删除该令牌。

A properly implemented flow would not automatically re-authenticate them when the user presses the back button, but that depends on the flow you implement (which is missing from your question and would likely be too much code anyway). 当用户按下后退按钮时,正确实现的流程不会自动重新验证它们,但这取决于您实现的流程(您的问题中缺少这个流程,并且可能会有太多代码)。

I see 2 options for the issue we have with the back-Button after Logout: 我在登出后看到后退按钮的问题有2个选项:

In your LoginActivity, wich should be you launcher activity, Override onBackPressed Method and leave it empty: 在你的LoginActivity中,应该是你的启动器活动,覆盖onBackPressed方法并将其留空:

    @Override
public void onBackPressed() {
// empty so nothing happens
}

Or/and you can add the LoginActivityIntent in your LogoutActivty if user == null. 或者/并且如果user == null,您可以在LogoutActivty中添加LoginActivityIntent。 This way, whenever a not authenticated user lands on the activity, it will redirect to the LoginActivity instantly, although this looks kinda weird. 这样,每当未经过身份验证的用户登陆该活动时,它将立即重定向到LoginActivity,尽管这看起来有点奇怪。

        mAuth = FirebaseAuth.getInstance();
    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");
                startActivity(new Intent(LogoutActivity.this, LoginActivity.class));
            }
            // ...
        }
    };

First Option is easier, but I guess if you apply both your on the save side ^^ Im coding for 2 weeks now so correct me if im wrong. 第一个选项更容易,但我想如果你在保存方面同时应用你的^^我现在编码2周,所以如果我错了,请纠正我。

你可以用finishAffinity();替换finish() finishAffinity();

Delete tokens and Instance IDs 删除令牌和实例ID

String authorizedEntity = PROJECT_ID;  
String scope = "GCM";
FirebaseInstanceID.getInstance(context).deleteToken(authorizedEntity,scope);

You can also delete the Instance ID itself, including all associated tokens. 您还可以删除实例ID本身,包括所有关联的令牌。 The next time you call getInstance() you will get a new Instance ID: 下次调用getInstance()时,您将获得一个新的实例ID:

FirebaseInstanceID.getInstance(context).deleteInstanceID();
String newIID = InstanceID.getInstance(context).getId();
private void sendToLogin() { //funtion
    GoogleSignInClient mGoogleSignInClient ;
    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
        .requestIdToken(getString(R.string.default_web_client_id))
        .requestEmail()
        .build();
    mGoogleSignInClient = GoogleSignIn.getClient(getBaseContext(), gso);
    mGoogleSignInClient.signOut().addOnCompleteListener(/*CURRENT CLASS */.this,
        new OnCompleteListener<Void>() {  //signout Google
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                FirebaseAuth.getInstance().signOut(); //signout firebase
                Intent setupIntent = new Intent(getBaseContext(), /*To ur activity calss*/);
                Toast.makeText(getBaseContext(), "Logged Out", Toast.LENGTH_LONG).show(); //if u want to show some text
                setupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(setupIntent);
                finish();
            }
        });
}

this code is written to work as copy past just read COMMENTS in code to customize it to ur needs, i prefer to send user to login 这段代码是作为副本编写的,只是在代码中读取COMMENTS以根据您的需要自定义它,我更喜欢发送用户登录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM