How to retrieve files of User using user id in flutter firebase? - flutter

How do I get user id of Users when authenticated ? and will the files be stored in different collection for every user since every user is different. And finally how do I show a user his data like his images and posts when Logged In ?

If your user is authenticated with Firebase using any provided auth method by Firebase, you can get the user id in that way:
String get currUid => FirebaseAuth.instance.currentUser?.uid ?? "";
If you authenticated successfully, then a definite userId will come there. In that case, you can write as
String get currUid => FirebaseAuth.instance.currentUser!.uid;

Related

Is it possible to store username in FirebaseAuth.instance.currentUser with Firebase for Flutter?

I want to store user information. Right now I created a collection Users with an email, an username and a profile image. I can get all this data using a method Future<CloudUser> getUserDetails({required String userId});. Is it possible to store the username in the class FirebaseAuth.instance.currentUser? so I don't have to use the method getUserDetails everytime I want to get the username. I am using flutter and firestore.
Is it possible to store the username in the class
FirebaseAuth.instance.currentUser?
You can store the username in the displayName property of the User class.
You can set this value by modifying the user object with the updateDisplayName() method, as shown here in the doc.

How to get first name and last name and birthday... from Google authentication in Flutter?

I want to get the First name ,last name, gender and birthdate of the current user when he signin with google. FirebaseUser object do not give me first name and last name separated, gender and birthdate.
Any help.
Once you've signed in successfully with a google credential, you can grab any details pertaining to that user's account using the FirebaseUser object. Check out firebase's documentation on how to get and use that object. Be sure to keep in mind that the user could have those values set to null, however.
Documentation for using Flutter and FirebaseAuth: https://firebase.flutter.dev/docs/auth/overview

Can we get FacebookAccessToken from FirebaseAuth?

With Flutter, I am able to login with Facebook, and later I want to get the FacebookAccessToken because it is needed to correctly get the user photoURL.
Is there a way to get the token from FirebaseAuth? Or I must store this value in my code?
Login part, we have the access token:
FacebookLoginResult result = await new FacebookLogin().logIn(['email', 'public_profile']);
...
FacebookAccessToken accessToken = result.accessToken;
Now, let's say in another Widget, we want to get the user photoURL
final auth = FirebaseAuth.instance;
String url = auth.currentUser.photoURL;
This url must be appended with ?accessToken=<access_token> retrieved during the first step. There is a bug created. But for now, can we directly get it from FirebaseAuth?
The Facebook access token is available from the auth result immediately after the user signs in from the FacebookAuthCredential object as result.credential.accessToken. It is not available from the current user, so if you don't capture immediately after sign-in there's no way to find it later without going back to the Facebook SDK.
Also see:
Can I get a facebook access token from firebase.auth().currentUser?
How to get provider access token in Firebase functions?
Get Facebook/Google access token on Firebase Auth#onAuthStateChanged?

Passing user id with AuthController

I just made simple authentication app using aqueduct as a back end. I used codes from aqueduct documentation pages for login and registering. When I login with this code in backend
router
.route('/auth/token')
.link(() => AuthController(authServer));
I get back token, token type and expiration date, Is there any chance to also pass userId? Or do I have to create my own controller to do that?
UPDATE
or how can I in my backend to save user id when saving the data
#Operation.post()
Future<Response> addData(#Bind.body(ignore: ['id']) Data newData) async {
final query = Query<Data>(context)..values = newData;
final insertData = await query.insert();
return Response.ok(insertData);
}
Flutter frontend
Login initially with the username/email and password. You will get an authorization token back from the server if the username and password are valid. Then use that token to make further privileged requests to the server.
You don't need to save any personal data about the user (email or password) on the client. You can save the token, though, if you don't want to make the user log in again the next time they use the app. When saving the token you should use a secure storage option. The flutter_secure_storage plugin uses KeyChain on iOS and KeyStore on Android.
Aqueduct backend
You can use the user IDs all you want on the backend. I don't know of any need to pass them to the client, though. On the backend you can query the user ID and then use it to fetch other information from the database.
Here is an example from the documentation:
class NewsFeedController extends ResourceController {
NewsFeedController(this.context);
ManagedContext context;
#Operation.get()
Future<Response> getNewsFeed() async {
var forUserID = request.authorization.ownerID;
var query = Query<Post>(context)
..where((p) => p.author).identifiedBy(forUserID);
return Response.ok(await query.fetch());
}
}
The client only passed in the token. Aqueduct looks up the user id for you based on that token. Now you know the user ID.
Your other tables can have a column for the user ID so that only that user may save and retrieve their data. In the example above, Posts have an Author and an Author has an ID, that is, the user ID.
where((p) => p.author).identifiedBy(forUserID)
is equivalent to
where((p) => p.author.id).equalTo(forUserID)
You can read about this in the Advanced Queries section of the documentation.

Getting a FIRUser from their uid

Is there a way to get FIRUser data back from a Firebase by using their uid? For example:
let firebaseUser:FIRUser = FIRAuth().auth()?.getUserByUid(someuid)
I'm doing this because I want to allow a user to see a friend's display name in the app.
Thanks!
In order to get someone by their Uid, you would need to store The data that you want in the database. You can use child["user"] with child["Uid"] to store things like a user's name or photoURL. Check out this series of videos. He shows you how to do a signup flow with FIRAuth and FIRDatabase. The next video in the series shows how you query the data if you don't already know how to do that.