Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log a user in using a generated Anonymous Login ID

Tags:

firebase

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.

Here is 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() {
           ...
like image 219
paulr Avatar asked Nov 03 '14 18:11

paulr


People also ask

What is anonymous logon user?

An anonymous login is a process that allows a user to login to a website anonymously, often by using "anonymous" as the username. In this case, the login password can be any text, but it is typically a user's email address. Users are able to access general services or public information by using anonymous logins.

How Firebase anonymous login works?

To protect your project from abuse, Firebase limits the number of new email/password and anonymous sign-ups that your application can have from the same IP address in a short period of time. You can request and schedule temporary changes to this quota from the Firebase console.

How anonymous authentication works?

Anonymous authentication gives users access to a website without prompting them for a user name or password. When a user attempts to connect to a public website, the web server assigns the user to the Windows user account called IUSR_computername, where computername is the name of the server on which IIS is running.

Is anonymous login safe?

Overall, truly anonymous login adds privacy and security for personal data. Along these lines, anonymity protects users from harm to their reputation; ideas and thoughts that are posted cannot harm a user. What's more, users value the benefits of anonymity and enjoy the protection that accompanies it.


1 Answers

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.

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

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.

like image 189
Rob DiMarco Avatar answered Oct 11 '22 18:10

Rob DiMarco