Log in with the generated anonymous login ID

I am writing an Android application and I am trying to register users anonymously so they don't have to go through any registration process. I store their anonymous user ID in general settings, and when the app opens, I try to register them based on that user ID. I'm trying to figure out the correct way to do this as there doesn't seem to be an auth function that just accepts a UID. I am currently using auth (), but I don't feel like it is correct.

Here's some sample code:

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() {
           ...

      

+3


source to share


1 answer


You create a new authentication session each time you call FirebaseRef.authAnonymously(...)

. This method only needs to be called once, after which the user will be verified on the page when the page is refreshed. Also note that you do not need to call again FirebaseRef.auth()

after restarting the application, as this part is automatically handled for you.

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

.



Finally, once you create an anonymous authentication session, no new sessions can ever be created with the same uid. This session will run until you expire your preset session end time (configured in your account) or until your user is logged out, after which this uid is permanently deleted.

+5


source







All Articles