I am developing a Flutter App and want to use Firebase Auth for user login. My problem is that I want to first ask for the email, check that the format is valid, and then send the user to a LogIn screen or SignIn screen depending on whether the email already exists on Firebase.
I read this question where it says that the only solution is to perform a signIn with createUserWithEmailAndPassword() method and check the error message, a solution that in the long term is not very reliable.
As flutter is evolving very fast, do you guys know of a better solution to accomplish this?
Thanks in advance!
I think you could also create a cloud function to make that check in the server, that would be an alternative method. The question stated a restriction about making the check within the app.
As regarding making the check using the error message, that was already fixed and now you can check it using the error code. (I updated my answer)
As you are signing up means mostly you will save user's data like profile name or something. While signing up new user save user's email into firestore if it's first time. Then from next time run a query whether email is present in firestore or not, which implies whether user is already authenticated or not (first time or already created account). If no document found with that corresponding email means user is new guy , you can sign in. If document found with corresponding email then login user instead of sign up.
Related
now I am learning flutter for chat app, I need to get currentUserEmail, now I have two ways to save currentUserEmail, a. save it in sharedPreference; b. always get it from FirebaseAuth
So here is my question:
does FirebaseAuth.instance.currentUser.email Flutter count to one time read from firesbase(since currentUserEmail value will be used multiple times in my app, it may increase the cost)
is it safe to get currentUserEmail and then save it in sharedPreference, so in the future I can easily get this currentUserEmail from sharedPreference instead of always get this from FirebaseAuth. I am afraid hacker can change this currentUserEmail value locally, so the app may treat this user as someone else.
thank you for any info in advance and keep safe!
Regarding reading data from your Firestore and whether it'll affect the cost of using Firebase, a similar question was answered there:
https://stackoverflow.com/a/52701789/3213173
https://medium.com/firebase-tips-tricks/how-to-drastically-reduce-the-number-of-reads-when-no-documents-are-changed-in-firestore-8760e2f25e9e
So a short answer is - yes, it may affect, but you can reduce the cost by implementing solutions from the linked articles.
Regarding saving the user's email in the preferences - yes, you can save it as long as the authentication in your application doesn't rely on the email address only. If you're using any Authentication service (such as Firebase Auth), then changing email in the preferences won't cause any issues with user's authentication.
There is no need to cache the values of FirebaseAuth.instance.currentUser in sharedPreference yourself, as the Firebase SDK already caches this value for you. There is no cost for accessing FirebaseAuth.instance.currentUser or any of its properties.
In my Firebase web app, I offer 3 different authentication methods – phone, email/password, and Google.
When new users go through the FirebaseUI sign-in flow, sometimes they don't remember which method they signed up with originally and use a different method (which creates a new user). For example, they created an account originally using their phone number, but when they return later (unauthenticated, perhaps on a new device), they see "Sign in with Google" and try that option – which creates a brand new user/account.
They are then confused why their account state is blank/new.
Is there a way to define a sign-in flow as such, so that if a user does not exist, it should not create a duplicate account (or at least offer an option to link to an existing user)?
I'm using the FirebaseUI for simplicity and hoping there's a flag or something I can set in the config that will achieve this.
For example, they created an account originally using their phone number, but when they return later (unauthenticated, perhaps on a new device), they see "Sign in with Google" and try that option – which creates a brand new user/account.
That's normal behavior since you don't have any information that it is the same user. To solve this, you should collect the email address of the user first time he/she signs up with the phone number. In this way, you can check the second time, whether the user has already an account in your app by searching for the existence of the email, as explained in my answers from the following posts. That's for the Realtime Database:
Checking if a particular value exists in the Firebase database
And that's for Firestore:
Firestore query - checking if username already exists
Is there a way to define a sign-in flow as such, so that if a user does not exist, it should not create a duplicate account (or at least offer an option to link to an existing user)?
Yes, by getting specific data to recognize the user. You either get the email address, as explained above or you get the phone number if it tries to sign in the first time with Google.
I'm using FireAuth to sign up my users, but i'm not using FireCloud (because we use our own back-end cloud database), and I would like to know if when a user connect with Google Sign In, if it is the first time or not he connect (to know if it create the user an account or simply log into one).
I've found this on the documentation of Firebase, it look perfect, but it's in beta and I don't seem to find the correct method with the dart package.
https://firebase.google.com/docs/functions/auth-events
Hope you can help me !
Thanks you for reading !
You can use isNewUser, which will return if the user is logging in for the first time:
AuthResult user = await auth.signInWithCredential(credential);
print(user.additionalUserInfo.isNewUser);
https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_auth/firebase_auth/lib/src/additional_user_info.dart
I have a question about storing data in the database.
I'm working on some screens within an app: the login screen (where the user can access your account), the signup screen (where the user can create a new account), the screen where the user provides information (such 'Mothers name and height'), and the profile screen (where information provided by the user is displayed).
My question is: how can I save this information provided by the user in Firebase Database?
I'm having no problems in login screen and signup screen, however I have no idea how I can save in Firebase Database the information provided by the user in the screen where the user provides information.
Can any of you guys help me with this? I know I can create a user with name, email and password using firebase auth, however I have no idea how I can save in Firebase Database the information provided by the user. And I know I can add a node but I don't know how. I read some articles but none of them talk about creating a node in Firebase.
Thanks in advance!
Notes:
*The screen where the user provides information comes after the user creates an account on the signup screen.
*If you want to see what I'm trying to do, please look at the project in GitHub: https://github.com/JoaoVRodrigues01/React-Native-Codec-App
The best approach is in fact store all data in your firebase database. If you store only on local storage the user can delete it and lost all data that supposed to be available, or also can't share data between different users.
Remember that to write/read your database node, the user must have the correctly permissions, either be logged or your database allows anonymous access.
You can use react-native-firebase (https://invertase.io/react-native-firebase/#/) if you need to persist your data, so use it locally when user is offline, for instance.
Hope it helps.
I'm trying to write a function that will check to see if an email is registered with my Firebase app (with Javascript/Web), before proceeding with the sign up process (if the email isn't registered).
I created a node named active_emails which I wanted to populate with registered emails (like an array). I was going to check the email against this list and if it didn't exist then I would allow the user to proceed with the registration process.
I see from the answer here: Proper way to store values array-like in Firebase that Firebase creates keys inside the array. So a normal Javascript array check won't work. I understand how the answer above works, but I was wondering, since I'm essentially doing a look up on the emails of registered Firebase users, is there another way to do this check?
Am I correct in thinking that the answer above requires Firebase to compile and then send an array of emails back to the user client-side? This seems like it might affect performance (if there are hundreds of thousands, or millions of emails on file), and would it expose all user emails to the client? Is there another way to check if a user is registered or not, maybe something like attempting a registration and then catching a duplicate email error (although this sounds like a messy way to go about this).
The top answer here: How to find out if an email is already registered with Firebase Simple Login? suggests using fetchProvidersForEmail as a way of checking to see if an email is registered, but that answer is for android and I don't know if it's possible to do such a thing with Javascript/Web.
I've tried to integrate this method here:
function checkEmail(){
firebase.database().ref().fetchProvidersForEmail("emailaddress#gmail.com").once("value", function(snapshot) {
console.log(snapshot);
});
}
But it shows the error: firebase.database(...).ref(...).fetchProvidersForEmail is not a function. Does anybody have any ideas on the best way to go about achieving this check?
Thank you in advance!
This method is for auth only:
firebase.auth().fetchProvidersForEmail("emailaddress#gmail.com")
This is deprecated.
firebase.auth().fetchProvidersForEmail("emailaddress#gmail.com")
Use this.
firebase.auth().fetchSignInMethodsForEmail(email)