step-up-labs / firebase-authentication-dotnet

C# library for Firebase Authentication
MIT License
376 stars 129 forks source link

(Enchancement) Support Local Storage & Async for IUserRepository #192

Open derekwelton opened 1 year ago

derekwelton commented 1 year ago

Here are my 2 request:

  1. IUserRepository needs to support async. If I want my repository to be in the browser's local storage or in a SQL Lite database, async methods need to be supported
  2. Support Blazor applications by adding a "LocalStorageUserRepository". With the updated 4.0 release, its been difficult to use this library in a blazor web assembly application. In 3.7, I was able to create my own ways, but finding it more difficult in 4.0. For example, I was able to get a Refresh my tokens by just passing in the ID Token and Refresh Token. I'm not able to do this now as I have to go through the IUserRepository.

One solution that would help is you currently have a FilerUserRepository. Here is an example of using LocalStorage In the browser.

public class LocalStorageUserRepository : IUserRepository
{
    private readonly ILocalStorageService _localStorage;

    public LocalStorageUserRepository(ILocalStorageService localStorage)
    {
        _localStorage = localStorage;
    }

    public bool UserExists()
    {
        var task = _localStorage.ContainKeyAsync("firebaseauth");
        return task.GetAwaiter().GetResult();
    }

    public (UserInfo userInfo, FirebaseCredential credential) ReadUser()
    {
        var task = _localStorage.GetItemAsync<UserDal>("firebaseauth");
        var user = task.Result;

        return (user.UserInfo, user.Credential);
    }

    public void SaveUser(User user)
    {
        var authUser = new UserDal(user.Info, user.Credential);
        var task = _localStorage.SetItemAsync("firebaseauth", authUser);
        task.GetAwaiter().GetResult();
    }

    public void DeleteUser()
    {

        var task = _localStorage.RemoveItemAsync("firebaseauth");
        task.GetAwaiter().GetResult();
    }
}
bezysoftware commented 1 year ago

That's something that would require a bigger rewrite, since the repository code if now used (AFAIK) in some constructors and properties (getters / setters). So introducing async would right now be a lot of work which I don't really have capacity for