In firebase cloud function how to get which app user is logged - firebase

We are creating user profile in firestore using cloud function
when new user create cloud function will trigger and we write the user info to firebase collection but, now in my firebase we have 2 apps and in need to store user profile in two different collection The issue is how can i identify which app is users by the user for login app A or app B
Is there any way to do that?

You need to send to the cloud function the tokenId of your user, you can get the tokenID doing something like this:
firebase.auth().currentUser.getIdToken(true).then(function (token) {
// Send the token
})
Then, on the cloud function you need to decode the tokenID to get the current userId, you can do something like the following:
admin.auth().verifyIdToken(idToken).then(function(decodedToken) {
var uid = decodedToken.uid;
})
With the uid, you can use admin.auth().getUser(uid) to get the current user record.

Related

Flutter Firebase authentication: reverting back to a specific anonymous user

The Firebase authentication documentation for Kotlin states that:
The call to linkWithCredential will fail if the credentials are already linked to another user account. In this situation, you must handle merging the accounts and associated data as appropriate for your app:
And then a code example is provided, the Flutter equivalent of which could be written as follows:
final prevUser = FirebaseAuth.instance.currentUser;
try {
final result = await FirebaseAuth.instance.signInWithCredential(credential);
final currentUser = result.user;
// Merge prevUser and currentUser accounts and data
// ...
} catch (e) {
// ...
}
Let's assume prevUser is an anonymous user. If the merge of prevUser and currentUser accounts and data doesn't succeed, is there any way to restore the Firebase sign-in state to what it was prior to the call to signInWithCredential, i.e. to make prevUser the currently signed in Firebase user again?
My app backend has its own representation of a user - AppUser - that is keyed by Firebase user uid, and there is an AppUser user that corresponds to (i.e. has the same uid as) prevUser. I don't want to recover from the failed merge by signing in anonymously, because that would give me a new anonymous user, with a different uid, which doesn't match up with said AppUser user.
No. Once an anonymous user signs out, or becomes replaced with a new sign-in, that anonymous account is effectively lost forever, from the perspective of the client app. The client app was the only holder of credentials that could perform a sign-in on that account.
You might want to consider at least persisting the uid of that account, then provide it to whatever process performs the merge. This would typically be done on a backend so that it could bypass any security restrictions on what can be done with an account when the user is not controlling it directly from a client app.

Creating User with Email, password, display name and photoURL

I'm creating an application with flutter and using firebase for authentication in data storage (possible more).
I have an auth onCreate cloud function which adds some user information to my realtime database whenever a user is created. But, I also want to set a displayName and photoURL when the user is created because if I update the profile info after, the onCreate won't be triggered. I also don't want users to be able to write to this part of the database.
Is there any method I can user to pass an displayName and photoURL when the user is created? Currently, I'm just creating the user with an email and password.
Thanks.
Instead of creating the users directly from the client, you can use a callable function to create users using the Admin SDK, which includes displayName and photoURL in its options.

How to use the same firebase anonymous user in a flutter app

I'm currently developing a Flutter app for in a internal project whose process is :
You can log in as an authenticated user (mail / passwd) and complete some actions / tasks / validate things
You can log anonymously and complete some actions, BUT you need to validate these actions through an authenticated user (AU).
By doing so, each time an AU log himself, he can validate and then switch back to anonymous mode. The problem is that Firebase creates a new anonymous user each time.
The app could be utilized on multiple devices across the company, which could create 100's of anonymous users a day, while in fact it was only 6-7 users, so what can I do to avoid this ?
I've read about customIdToken but I haven't come to a solution for my problem.
Here is the code I'm using:
Future<FirebaseUser> signInAnonToken(String token) async {
FirebaseUser user = await _firebaseAuth.signInWithCustomToken(token: token);
return user;
}
FirebaseUser userAnon = await widget.auth.signInAnonToken("useranonuid");
Where "useranonuid" is the uid of the anonymous user but also the token I get by using the getIdToken(refresh:true) method
Thanks in advance.
The problem is that Firebase creates a new anonymous user each time.
The workaround that I did on my end is to add deleting the anonymous user on the end of each workflow. This should prevent the Firebase project hitting the 100 million anonymous user account limit.
There's no way to restore previous Firebase Anonymous Auth instance. That would defeat the purpose of being "anonymous" in the first place.

Is it able to store Firebase user to our own database?

If I use the user registration and login through the firebase authentication, the user information are stored in the firebase database side. How can I get the user information (such as FirstName, LastName, Gender and etc.) and store in my own database? Is it use the access token to call my API to check the user identity?
I suggest to use a cloud functions to triggers when user was created.
exports.userCreate = functions.auth.user().onCreate((user) => {
const email = user.email; // The email of the user.
const displayName = user.displayName; // The display name of the user.
// Write insert email, displayName, ... to your own database.
});
Ref: https://firebase.google.com/docs/functions/auth-events
When a user registers for your app using Firebase Authentication, no information is sent to your Firebase database by default...they only have an Auth account.
You need to manually collect and send information to your database. Once a user is authenticated within your app, you can listen for their Auth user info with onAuthStateChanged.
Lotsa folks then use the uid as the key in their database and store profile info below that.

how to use cloud functions to save user's displayName when creating account in firebase [duplicate]

This question already has answers here:
How to add DisplayName with email + password authentication in Firebase? Android
(2 answers)
Closed 5 years ago.
Question: When a user account is created, how to use cloud functions to save his displayName to realtime DB in firebase
The scenario steps:
I have a signup page containing email, password, displayName fields,
to allow users to create new account in firebase
Above page will 1) trigger createUserWithEmailAndPassword to create
the user account, 2) save displayName value to userProfile in Auth DB.
In Cloud function, the code below is used so new user's email can
be saved to DB users node.
exports.createProfile = functions.auth.user().onCreate(event =>
return admin.database().ref(`/users/${event.data.uid}`).update
({email: event.data.email})
I wish to save displayName to users node with above code, but it seems not possible because it is not in the context of event in onCreate.
What shall I do? It becomes nasty if I need to use both client and cloud function to update users node.
Best regards,
D
You need to use firebase.User.updateProfile() method to update the displayName once the firebase.auth().createUserWithEmailAndPassword(email, password) method completes. Like this -
firebase.auth().createUserWithEmailAndPassword(email, password).catch(function (error) {
}).then(function (user) {
user.updateProfile({
displayName: "Jane Q. User",//pass displayName from your signup form here
photoURL: "<image-url>"//you can pass this empty
}).then(function () {
//redirect to your post-registration page
});
})
The documentation for auth events suggests the displayName is accessible via event.data.displayName. If you are seeing any other behavior, it's a bug or you might be doing something wrong that you're not mentioning here.

Resources