Testing the Persistence of Firebase’s Anonymous Authentication
By: Rob Jones
Tags: Flutter, Android App Development, Authentication, Firebase, Programming

Setting up:

Since this article is strictly about anonymous authentication persistence, I am omitting the process of setting up the app and enabling anonymous authentication within Firebase. For instructions on how to set up your app with anonymous authentication,see the firebase_auth pub.dev page and the documentation from Firebase.

The three plugins being used and their versions:

firebase_core: ^0.5.2+1

firebase_auth: ^0.18.3+1

(Test 7 only) shared_preferences: ‘>=0.5.12+2 <2.0.0’

I created a default Flutter project on Android and modified it to add some RaisedButtons to do a few different things:

Login anonymously via Firebase

Check the login status with Firebase

Check what the user’s Unique ID (UID) is

Sign out

The output of each button will be displayed above the buttons. Further, a count of how many times the “Sign in Anonymously” button is tapped in a session is displayed.

Our starting screen

Import firebase_core and firebase_auth :

import 'package:firebase_auth/firebase_auth.dart' as auth; import 'package:firebase_core/firebase_core.dart';

Create the FirebaseAuth instance:

final auth.FirebaseAuth _auth = auth.FirebaseAuth.instance;

Our “Sign in Anonymously” code:

await _auth.signInAnonymously();

///Asynchronously creates and becomes an anonymous user.

Our “Check Login Status” code:

auth.User user = _auth.currentUser;

///Check if user is null

Our “Get UID” code:

auth.User user = _auth.currentUser;

/// user.uid will return the UID

Our “Sign Out” code:

_auth.signOut();

///Signs out the current user.

///If successful, it also updates any [authStateChanges], ///[idTokenChanges] or [userChanges] stream listeners.

A few new methods will be added in Test 7 regarding shared_preferences .

The final code is at the end of this article.

Test 1: Sign in, close app, re-open app from emulator, check Login Status and UID

All we are doing here is signing in anonymously, getting the UID, closing the app, and then re-launching the app in the emulator to make sure we are still logged in and have the same UID we did before closing the app.

After tapping “Sign in Anonymously,” “Check Login Status,” and “Get UID,” we get the following:

Now, let’s close the app, re-open it, and check again. Note that I am not closing the app and then running the app in the IDE — I’m opening the app from the emulator directly.

Tap “Check Login Status” and “Get UID” to see our results:

Login Status — Never changed

UID — Never changed

With anonymous authentication, when the user closes the app and then reopens it later, they will still be logged in anonymously with the same UID.

Test 2: Sign in, close app, rebuild app from IDE, check Login Status and UID

Similar to Test 1, but instead of launching the app from the same build on the emulator, we are running it from the IDE and checking if anything changed.

Running it from the IDE the first time we get the following results:

Again, the same session is persisting from Test 1 (still logged in with the same UID).

Now we stop the app from the IDE and run the app from the IDE again. Tapping the “Check Login Status” and “Get UID” buttons produces the following:

Still logged in with the same UID, so it does persist as it did in Test 1.

Test 3: Sign in Anonymously multiple times

Here we will be calling the signInAnonymously method multiple times to see if anything changes. Here is our UID (same as the end result of Test 2):

After the second “Sign in Anonymously,” “Check Login Status,” and “Get UID” taps, here is our UID:

Second “Sign in Anonymously” tap

It’s all still the same.

After the 10th tap, here is our UID:

Ten “Sign in Anonymously” taps

Still the same.

After the 50th tap, here is our UID:

Fifty “Sign in Anonymously” taps

Nothing changed. The same session/UID persists after signing in anonymously numerous times.

Per firebase_auth regarding this test:

If there is already an anonymous user signed in, that user will be returned instead. If there is any other existing user signed in, that user will be signed out.

Test 4: Signing out and Signing in

We will call the signOut method with the “Sign Out” button and then tap the “Sign in Anonymously” button again to see if a new UID is generated.

Here is our current UID (same as the end of Test 3):

Same UID as last test

Now we will tap “Sign Out” and then “Sign In Anonymously.” After tapping “Get UID,” here is what we get:

After signing out and signing in again, we get a new UID

As you would expect, our last session has ended and we have a new UID.

Test 5: Deleting the app’s cache

We will get the current UID, go to the app in the emulator and delete the app’s cache, then re-open the app (from the emulator) and check the login status/UID then.

Let’s get the current UID (it’s the same session as at the end of Test 4):

Now close the app and delete (clear) the app’s cache:

To find this on API 30: Tap and hold the app icon -> App Info -> Storage and cache

Now re-open the app (from the emulator) and tap “Check Login Status” and “Get UID”:

Same session — same UID. Nothing changed, so it does indeed persist if you clear the app’s cache.

Test 6: Deleting the app’s data (storage)

Almost identical to the steps we took in Test 5, we will delete the app’s data (storage) and see if the UID changes:

First, run the app from the IDE and check the login status and get the current UID:

Same session and UID as at the end of Test 5.

Close the app and delete the app’s data (storage):

To find this on API 30: Tap and hold the app icon -> App Info -> Storage and cache

You can see we get the warning about how all accounts and files will be deleted. We will confirm the delete:

Re-open the app (from the emulator) and tap “Check Login Status”:

Clearing the app’s storage has deleted our Firebase session

Deleting the app’s data has removed our anonymous login session, so we will no longer have the same UID.

Just to confirm, tap “Sign in Anonymously” and “Get UID” to see what the UID is:

It’s a new UID; thus, deleting the app’s data (storage) will terminate our anonymous session.

Test 7: Clearing shared_preferences

You may use the plugin shared_preferences to store data locally on the device. shared_preferences has a .clear() function that will delete anything that is stored by that plugin.

I’ll install the shared_preferences plugin, import it, and add a few more RaisedButtons to:

Save something to the device with shared_preferences . This will .setString() with the key “myKey”:

SharedPreferences prefs = await SharedPreferences.getInstance(); prefs.setString( "myKey", "Hello this is my value in sharedPrefs");

Retrieve that data with shared_preferences to confirm it works. This will .getString() with the “myKey” key:

SharedPreferences prefs = await SharedPreferences.getInstance(); String prefsData = prefs.getString("myKey");

Call .clear() on shared_preferences to delete the saved data:

SharedPreferences prefs = await SharedPreferences.getInstance(); bool cleared = await prefs.clear();

The result of the button actions will be displayed above with the other result data.

After saving the data and retrieving it with the “Save data…” and “Get data…” buttons, respectively, here is our result:

The data was successfully saved and retrieved with shared_preferences , so we know it’s working. You can also see that we have the same anonymous session with the same UID as we did at the end of Test 6.

Now let’s tap the “Clear data…” button and then “Get data…” to be sure that our .clear() call worked:

It worked as expected. Tap the “Check Login Status” and “Get UID” buttons again to check if the session is still the same:

Nothing changed with our login status or UID

Nothing changed — we are still logged in and the UID is the same. Clearing our SharedPreferences instance had no effect on our anonymous authentication.

Just to be thorough, we will close the app and re-open it in the emulator and check our login status again:

It’s still all good.

Test 8: Deleting the app and then reinstalling

You can probably guess what will happen here, but we will do it anyway.

We will get our current UID, delete the app from the emulator, use the IDE to reinstall the same version of the app, and then get our login status/UID.

Our current UID (same as the end of Test 7):

Delete (uninstall) the app from the emulator:

To find this on API 30: Tap and hold the app icon -> App Info

Re-install the app by running it in the IDE. After it has finished installing, tap “Check Login Status” and we see:

No longer signed in

Our anonymous session has terminated and we no longer have the UID. Tap “Sign in Anonymously” and check the UID:

Different UID = different anonymous user according to Firebase.