Update Firebase User

Eduardo Gomez 3,416 Reputation points
2022-01-20T22:53:44.537+00:00

Hello

I am trying to update the photo URL and displayName of the user.

I have the MainPage, which has an MSN logo, and when you tap on it you will go to the avatar page or profile page.

I have this method

      public async Task<Firebase.Auth.User> UpdateUserData(string PhotoUri, string DisplyName) {

            var savedfirebaseauth = JsonConvert.DeserializeObject<FirebaseAuth>(Preferences.Get(App.FirebaseRefreshToken, string.Empty));
            var RefreshedContent = await AuthProvider.RefreshAuthAsync(savedfirebaseauth);
            Preferences.Set(App.FirebaseRefreshToken, JsonConvert.SerializeObject(RefreshedContent));
            var newUser = await AuthProvider.UpdateProfileAsync(Preferences.Get(App.FirebaseToken, string.Empty),
                DisplyName, PhotoUri);
            return newUser.User;
        }

Witch is supposedly updating the user, then in the ProfileViewModel, when I select an avatar, i am calling

  private async void SelectAvatarAction() {

            await App.services.UpdateUserData(SelectedAvatar, DisplayName);

            Console.WriteLine(FireUser.PhotoUrl);
        }

But it only prints the MSN every time, and it doesn't update

https://github.com/eduardoagr/DuoNotes

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,386 Reputation points Microsoft Vendor
    2022-01-21T07:31:26.363+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    If you want to update the image at the runtime.

    You need to change following places.

    First, you need to edit the savedfirebaseauth.User.PhotoUrl = "msn.svg"; line in the GetProfileInformationAndRefreshToken method of FirebaseServices.cs. you give an specific value, so this image will not change in the MainPage. If we login this application firstly, set a default value like following code.

       public async Task<Firebase.Auth.User> GetProfileInformationAndRefreshToken() {  
         
                   var savedfirebaseauth = JsonConvert.DeserializeObject<FirebaseAuth>(Preferences.Get(App.FirebaseRefreshToken, string.Empty));  
                   var RefreshedContent = await AuthProvider.RefreshAuthAsync(savedfirebaseauth);  
                   Preferences.Set(App.FirebaseRefreshToken, JsonConvert.SerializeObject(RefreshedContent));  
         
                   if (string.IsNullOrEmpty(savedfirebaseauth.User.PhotoUrl))  
                   {  
                       savedfirebaseauth.User.PhotoUrl = "msn.svg";  
                   }  
                 
                   return savedfirebaseauth.User;  
         
               }  
    

    Second, you need to give FireUser value after you update the image in the ProfilePageViewModel.cs. If you do not give value to FireUser, you always get the MSN result.

       private async void SelectAvatarAction() {  
         
                   FireUser = await App.services.UpdateUserData(SelectedAvatar, DisplayName);  
         
                   Console.WriteLine(FireUser.PhotoUrl);  
               }  
    

    Third, the code order is wrong in UpdateUserData method of FirebaseServices.cs. You need get the update user firstly then set it to savedfirebaseauth.User, fresh the token in the end like following method.

       public async Task<Firebase.Auth.User> UpdateUserData(string PhotoUri, string DisplyName)  
               {  
                   var savedfirebaseauth = JsonConvert.DeserializeObject<FirebaseAuth>(Preferences.Get(App.FirebaseRefreshToken, string.Empty));  
                   var newUser = await AuthProvider.UpdateProfileAsync(Preferences.Get(App.FirebaseToken, string.Empty),  
                   DisplyName, PhotoUri);  
         
         
                   savedfirebaseauth.User.DisplayName = DisplyName;  
                   savedfirebaseauth.User.PhotoUrl = PhotoUri;  
         
         
                   var RefreshedContent = await AuthProvider.RefreshAuthAsync(savedfirebaseauth);  
                   Preferences.Set(App.FirebaseRefreshToken, JsonConvert.SerializeObject(RefreshedContent));  
                   return newUser.User;  
               }  
    

    Then you can update image in the ProfilePage at run time. If you want to update the profile image in the mainpage as well when you back to the mainpage, you need to change value from private async void GetUserData() to public async void GetUserData() method in the MainPageViewModel.cs, you can call it in the onappearing method like following background code in MainPage . when you back to the mainpage, OnAppearing will execute again. judge the current viewmodel and get it, we can execute GetUserData to get the latest data.

       public partial class MainPage : ContentPage {  
                
               public MainPage() {  
                   InitializeComponent();  
                   
               }  
               override protected void OnAppearing() {  
                 
                   if(BindingContext is MainPageViewModel vm){  
                       //when you back to the mainpage, you can update the profile image.  
                       vm.GetUserData();  
                   }  
                 
               }  
           }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful