使用生成的匿名登录ID登录用户 [英] Log a user in using a generated Anonymous Login ID

查看:51
本文介绍了使用生成的匿名登录ID登录用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Android应用程序,并且试图匿名登录用户,这样他们就不必进行任何注册过程.我将他们的匿名用户ID存储在共享首选项中,当应用程序打开时,我试图基于该用户ID登录.我正在尝试找出执行此操作的正确方法,因为似乎没有一个仅接受UID的身份验证功能.目前,我使用auth()拥有它,但我不认为那是正确的.

I am writing an Android application and I am trying to log users in anonymously so they don't have to go through any sort of registration process. I am storing their anonymous user ID in shared preferences, and when the application opens, I am trying to log them in based on that user ID. I am trying to figure out the correct way to do this, as there doesn't seem to be an auth function that just takes in a UID. Currently I have it using auth(), but I don't feel like that is correct.

以下是一些示例代码:

String userID = getUserID();

    if(userID.equals("NOT_FOUND")) {
        ref.authAnonymously(new Firebase.AuthResultHandler() {
            @Override
            public void onAuthenticated(AuthData authData) {
                //successful authentication
                //save auth data
                SharedPreferences prefs = getSharedPreferences(
                        "USER_ID", Context.MODE_PRIVATE);
                String id = authData.getUid();
                prefs.edit().putString("USER_ID", id).commit();
            }

            @Override
            public void onAuthenticationError(FirebaseError firebaseError) {
                //unsuccessful authentication
            }
        });
    } else {
        ref.auth(userID, new Firebase.AuthListener() {
           ...

推荐答案

每次调用 FirebaseRef.authAnonymously(...)时,您都在创建一个新的身份验证会话.此方法只需要调用一次,之后用户将在刷新页面时进行身份验证.另外请注意,重新启动应用程序后,您无需再次调用 FirebaseRef.auth(),因为该文件将自动为您处理.

You're creating a new authentication session each and every time you invoke FirebaseRef.authAnonymously(...). This method only needs to be invoked once, after which the user will authenticated upon page refreshes. Also note that you do not need to call FirebaseRef.auth() again once restarting the application, as that piece is automatically handled for you.

如果您要检查用户的当前身份验证状态,并且,如果用户当前未通过身份验证,则创建新的身份验证会话,请使用同步访问器进行身份验证状态 FirebaseRef.getAuth().

If you'd like to check for the current authentication state of the user, and only then create a new authentication session if the user is not currently authenticated, use the synchronous accessor for authentication state FirebaseRef.getAuth().

最后,一旦创建了匿名身份验证会话,就不可能再使用相同的uid创建新的会话.该会话将一直持续到您预定义的会话到期时间(在帐户信息中心中配置)或您的用户注销之后,该uid将永久退出.

Lastly, once you create an anonymous authentication session, no new sessions may ever be created with the same uid. That session will live until your predefined session expiration time (configured in your account dashboard) or until your user logs out, after which that uid is permanently retired.

这篇关于使用生成的匿名登录ID登录用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆