Custom credentials in Xamarin forms

Kalibo 1 Reputation point
2020-12-04T22:44:34.47+00:00

Hello guys, I am currently working on a xamarin forms project for a barbershop. For this project I am using firebase. I have already implemented firebase.auth with which I can register and login. I would also like to add roles as an administrator and as a regular user. For this I have found on the site of firebase that this is possible. In my code you can be sure that it mainly comes from the site itself. When testing I see that I get a nullpointerexception and that an object is not initialized which means null. Could someone help me, because there are hardly any sources on the internet about this? On line 64 it goes wrong. I think it has to do with the default instance, but I wouldn't know how to solve this...

public async Task MakeAdmin(string email)
{
try
{
FirebaseAdmin.Auth.UserRecord userRecord = await FirebaseAdmin.Auth.FirebaseAuth.DefaultInstance.GetUserByEmailAsync(email);

            var claims = new Dictionary<string, object>()  
            {  
                { "admin", true },  
            };  
            await FirebaseAdmin.Auth.FirebaseAuth.DefaultInstance.SetCustomUserClaimsAsync(userRecord.Uid, claims);  

        }  
        catch (FirebaseException e)  
        {  
            throw e;  
        }  
    }![45322-screenshoterror.png][1]  
Developer technologies .NET Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. Ángel Rubén Valdeolmos Carmona 611 Reputation points
    2020-12-24T17:42:40.023+00:00

    Hi I have to tell you that custom claims are always created from the backend and never from the client therefore you should use firebase sdk in .net, node js or from cloud functions. Once done, you need to update the token on the client from the server. In the client you get the token and read the custom claims.

    More info:
    Server SDK .Net (non mobile)
    https://github.com/firebase/firebase-admin-dotnet
    https://firebase.google.com/docs/auth/admin/custom-claims

    Cloud Function:
    https://stackoverflow.com/questions/55360425/adding-a-custom-claim-to-the-oncreate-user-firebase-cloud-function

    I am developing an application with firebase and I have it working like this, backend in the cloud (google) with node js and on the server with .net with Firebase admin SDK. You can choose the option that best suits your project, in my case both are necessary.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.