How to get FirebaseUser from a uid? - firebase

Let's say I've a user id
String uid = "1234abcdefgh890";
Now, I'd want to get a FirebaseUser using this uid, how do I do that?
FirebaseAuth auth = FirebaseAuth.instance;
FirebaseUser user = (await auth.signInWithXXX(uid)).user; // is there any method like this
How can I get the FirebaseUser then?

There is no API in the Firebase client-side SDKs to get user information based on a UID, as this could potentially be a security concern.
If you need this information, you can either implement your own server that uses the Firebase Admin SDKs (which do have this functionality), or you can store user information in a secondary database, such as Firebase's own Realtime Database or Cloud Firestore. Then you can query this database from the rest of your application, and secure access to fit your needs with the database's server-side security rules.
Also see:
How do I return a list of users if I use the Firebase simple username & password authentication
Firebase get user by ID
How to get the user email in Firebase based on user id
Using Firebase User UID to retrieve profile info
Firebase get Authentication details using UID

Related

Firebase cookies without user login [duplicate]

I use Flutter and Firebase, just ask myself how to allow users to add items to cart when they're not logged in yet and keep the cart when they logging in, anyone have an idea for this ?
On Firebase you'd typically start the user off with an anonymous authentication account. With that sort of account, Firebase generates a user ID (UID) for the user, without them having to enter any credentials. You then associate the cart/items with the UID, and then when you/they are ready to identify themselves, you can sign them in with another provider, and link that to the anonymous account.
I've provided links to the documentation for Android developers above, but the same functionality is available in the FlutterFire libraries too. For example, anonymous sign-in on Flutter is as simple as:
UserCredential userCredential = await FirebaseAuth.instance.signInAnonymously();
you can store this data in a separate database SQL lite as an example

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.

Does firebase auth store and manage access token for different provider

I am working on a flutter app where the user will sign-in using Facebook. Later I want to access facebook graph API and for then I need the access token of the user.
I of now I am storing the access token in firestore and assessing from there. But is there any way using which we can get the access token from currentUser. Because in the current situation the access then gets expired and I need to refresh it again and again. Does firebase handle it automatically?
Below is my code which I use to sign in with Facebook using firebase.
FacebookLogin facebookLogin = new FacebookLogin();
FacebookLoginResult result = await facebookLogin.logIn(['email', 'public_profile', 'user_friends']);
switch (result.status) {
case FacebookLoginStatus.loggedIn:
AuthCredential authCredential = FacebookAuthProvider.getCredential(accessToken: result.accessToken.token);
FirebaseUser user = (await FirebaseAuth.instance.signInWithCredential(authCredential)).user;
//Here I save user details in firestore
}
In order to do this you will need to set up your own token via Facebook's sign in system. When you sign in using Facebook, you can configure it such that their token IS the Facebook token as well.
Check out this for a from-scratch Facebook login.
And this for creating a custom Firebase token.

Where does firebase authentication store the displayName of its users?

I'm using Firebase Authentication as the authenticator for a .NET app.
I'm not using any other part of Firebase (eg. Firestore, Realtime DB) other than the Authentication.
When I log in using the firebase-ui to get a JWT token issued, it returns a "displayName" as part of the AuthResult which is basically the name field when the user first signed up.
I'm only using the Email / Password sign-in method. Where is this data stored? The Users tab on Firebase Authentication console only shows 5 fields - Identifier, Provider, Created, Signed In and User UID.
Is Firebase authentication storing the user fields like this in some sort of hidden place that I can't access unless I log in as the user?
Firebase Authentication stores user profiles in its internal database. Some of the properties are exposed in the Firebase console, but not all of them. You can get all documented properties through the Admin SDK.

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.

Resources