Share via

Securetorage Null

Eduardo Gomez 4,316 Reputation points
2024-03-18T02:25:22.75+00:00

So, for my app, I need the functionality of letting the user change role (Student, Coordinator or teacher)

So, the way that it was setup was when I log in, I immediately get the email from firebase.

this was working fine.

but for some reason when I quit the app, login again and try to change roles it breaks

So, what I did is that When I log in, I store the object in secure storage, but for some reason when I select the role, in order for me to update the object and store it again, the secure storage is null.

Demo

https://reccloud.com/u/x13gvm9

Login

    var user = await authenticationService.LoginWithEmailAndPassword(User.Email!, User.Password!);
    if(user is not null) {
        var currentUser = await dataService.GetByEmailAsync<DemyUser>(Constants.USERS,
        user!.Info.Email);
        if(currentUser?.CurrentRole is not null) {
            FlyoutHelper.CreateFlyoutMenu(currentUser.CurrentRole!);
            FlyoutHelper.CreateFlyoutHeader(currentUser);
            StorageHelper<DemyUser>.StoreObjectToStorage(currentUser, storage);
            await appService.NavigateTo($"//{currentUser.CurrentRole}DashboardPage", true);
        } else {
            StorageHelper<DemyUser>.StoreObjectToStorage(currentUser!, storage);
            await appService.NavigateTo($"//{nameof(RoleSelectionPage)}", true);
        }


RoleSelection

private readonly ISecureStorage _secureStorage;
private readonly IAuthenticationService _authService;
private readonly IDataService<DemyUser> _dataService;
[ObservableProperty]
string? welcomeText;
public ObservableCollection<UserRoles>? Roles { get; set; }
string? selectedRole;
DemyUser? _user;
public RoleSelectionPageViewModel(IAuthenticationService authService,
    IDataService<DemyUser> dataService, ISecureStorage secureStorage) {
    _authService = authService;
    _dataService = dataService;
    _secureStorage = secureStorage;
    InitPopUp();
}
private async void InitPopUp() {
    Roles = GetRoles();
    _user = await StorageHelper<DemyUser>.GetObjFromStorageAsync(_secureStorage);
    WelcomeText = $"Welcome {_user?.FullName}, please chose a role";
}
[RelayCommand]
public void RoleSelected(UserRoles SelectedRole) {
    foreach(var role in Roles!) {
        role.IsSelected = role == SelectedRole; // Set IsSelected to true only for the selected rol
        if(role.IsSelected) {
            role.SelectedColor = Constants.SelectedColor;
        } else {
            role.SelectedColor = Constants.DefaultUnselectedColor;
        }
    }
    selectedRole = SelectedRole.Name.ToString();
}
[RelayCommand]
public async Task UpdateUserCurrentRole() {
    var currentUser = await _dataService.GetByEmailAsync<DemyUser>(Constants.USERS,
        _user!.Email!);

the

_user

is not null, but why I have it in secure storage (if you see the video, you will see that is in there)

My StorageHelper

public static class StorageHelper<T> {
    public static async Task<T?> GetObjFromStorageAsync(ISecureStorage secure) {
        var data = await secure.GetAsync(Constants.LOGGED_USER)!;
        if(data is not null) {
            var obj = JsonSerializer.Deserialize<T>(data!);
            return obj;
        }
        return default;
    }
    public static void StoreObjectToStorage(T data, ISecureStorage storage) {
        if(data is not null) {
            var json = JsonSerializer.Serialize(data);
            storage.SetAsync(Constants.LOGGED_USER, json);
        }
    }
}


and I am injecting the Isecure storage

  builder.Services.AddSingleton(SecureStorage.Default);
Developer technologies | .NET | .NET Multi-platform App UI

Your answer

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