Home > database >  How to get current logged user UID
How to get current logged user UID

Time:11-13

How to get current logged-in user UID in Firebase, in c# Xamarin. I want to be able to do this so that I can store the user's data in the database under their UID and retrieve it that way. The problem is I completely don't know where to start, couldn't find anything at web.

Package I'm using for auth is firebaseauthentication.net\3.7.2*

Auth page with one user:auth

Login task (if needed)

    static string webAPIKey = "";
    FirebaseAuthProvider authProvider = new FirebaseAuthProvider(new FirebaseConfig(webAPIKey));

public async Task <string> Login(string email, string pass)
    {

        var tok = await authProvider.SignInWithEmailAndPasswordAsync(email, pass);

        if (!string.IsNullOrEmpty(tok.FirebaseToken))
        {

            return tok.FirebaseToken;

        }

        return "";

    }

CodePudding user response:

Okay so to get user UID from token, approach is very simple. You just need to encode the token and get the data ;

using System.IdentityModel.Tokens.Jwt;

            string token = await _userRepo.Login(mail, pass);

            var handler = new JwtSecurityTokenHandler();
            var jsonToken = handler.ReadToken(token);
            var tokenS = jsonToken as JwtSecurityToken;

            var userid = tokenS.Claims.First(claim => claim.Type == "user_id").Value;
  • Related