How to set sign in anonymously in Firebase
welcome to firebase series tutorial.
In this post, we are going learn about how to sign in anonymously in firebase.
But before starting, I am providing you some ideas why we need sign in anonymously?
If you are using real-time database and storage you need to create a rule for access database and storage.
By default the rule is
So we sign in anonymously in Firebase from our app to protect database and storage.
Let's start,
you already know how to connect Firebase for your project.
now go to firebase console and select authentication.
click this to go directly Firebase Console.
Now go to sign in method
and click anonymous and enable it.
that's the ending of the configuration of firebase console
now time for coding
Note: I use kotlin as a default language.
add below line to your build.gradle file
In this post, we are going learn about how to sign in anonymously in firebase.
But before starting, I am providing you some ideas why we need sign in anonymously?
If you are using real-time database and storage you need to create a rule for access database and storage.
By default the rule is
{ "rules": { ".read": "auth != null", ".write": "auth != null" } }here auth means Authentication. So if your app hasn't any login option you sign in anonymously sign in from your app. So by doing this, that database and storage can be access only from your app. so you can protect your database. Or if you change the rule to access everyone there is a possibility to lose your database. anyone can delete your database with in a single request.
So we sign in anonymously in Firebase from our app to protect database and storage.
Let's start,
you already know how to connect Firebase for your project.
now go to firebase console and select authentication.
click this to go directly Firebase Console.
Now go to sign in method
and click anonymous and enable it.
that's the ending of the configuration of firebase console
now time for coding
Note: I use kotlin as a default language.
add below line to your build.gradle file
implementation 'com.google.firebase:firebase-auth:11.2.0'
Now create an instance of FirebaseAuth classsee the code-
val mAuth = FirebaseAuth.getInstance()use signInAnonymously() method and add a complete listener. if the task is successful we show a message that Authentication is successful and also user id. if failed then we will show a fail message.
mAuth.signInAnonymously() .addOnCompleteListener(this, { task -> if (task.isSuccessful) { val user = mAuth.currentUser Toast.makeText(this, "Authentication successful. user id ${user.uid}", Toast.LENGTH_SHORT).show() } else { // If the sign in fails displays a message to the user. Toast.makeText(this, "Authentication failed.", Toast.LENGTH_SHORT).show() } })That's it coding see the final screen short in the firebase console
you find some details about your user
1. creation date
2. signed in date
3. user id
That's it from this post hope this will help you to understand this concept.
and you will implement anonymous login option for your app and make a better security for your Firebase database and storage.
Happy coding
1. creation date
2. signed in date
3. user id
That's it from this post hope this will help you to understand this concept.
and you will implement anonymous login option for your app and make a better security for your Firebase database and storage.
Happy coding
No comments :