Authenticating Anonymously With Firebase Auth | Updating Firebase Without Sign In | Complete Guide To Firebase Anonymous Auth
We all love using app but whenever an App asks us to sign in or sign up to use app, it feels like not now. We all make apps where we don’t want user to spend time sign in or entering email ID etc. So firebase has come up with amazing feature to get user signed in Anonymously, just a unique ID.
Lets Get Started
First lets understand how This auth works, lets say you want to collect feedback for your app but without letting user creating account or sign in. So you create a button, so when it is pressed the user is signed in with a unique UID without creating account or anything just by single click.
So lets first start by adding gradle files(Use Update Versions)
implementation 'com.google.firebase:firebase-auth:19.0.0'
Enable anonymous auth:
- In the Firebase console, open the Auth section.
- On the Sign-in Methods page, enable the Anonymous sign-in method.
Add Above code to initialize firebase auth
Now add above code in your onstart method, this will check if the user is signed in or not, if not then take back to another activity or splash some toast. If the user is signed in then do your code work.
Now create a signInAnonymously method and add above code, you can put your code here if user is signed In than what to do. So here we complete the signing part, now user will able to sign in with UIDs.
Adding Values In Database Anonymously Firebase
Define and initialize firebase database reference
Now Create a method and define in onstart or onresume, where you can insert data in to your database. Make sure you define the method in else part
Onstart....FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser == null){
sendToStart();
} else {
//SAVEDATAREPORT
savereport(currentUser);
}
Now we have complete most of the part, add internet permission in manifest file and lets make part to logout.
Define logout method in any button or wherever you want, once user is logged out he will never be able to sign in with same UID, if again user will sign in then it will create new database and new auth ID.
Happy Learning !