Login flow with shell

Eduardo Gomez 3,651 Reputation points
2023-12-10T19:00:22.7233333+00:00

the issue that I have is the fact that I need to create flyout menu based on roles or at least set Visibility to false.

Forn example this is my flyout.


    <ShellContent
        ContentTemplate="{DataTemplate view:LoginPage}"
        Route="LoginPage"
        Shell.FlyoutBehavior="Disabled"
        Shell.FlyoutItemIsVisible="False" />

    <FlyoutItem Title="Home">
        <ShellContent
            ContentTemplate="{DataTemplate view:HomePage}"
            Route="HomePage" />
    </FlyoutItem>


    <FlyoutItem Title="New lecture">
        <ShellContent
            ContentTemplate="{DataTemplate view:NewLecturePage}"
            Route="NewLecturePage" />
    </FlyoutItem>

    <FlyoutItem Title="New test">
        <ShellContent
            ContentTemplate="{DataTemplate view:NewTestPage}"
            Route="NewTestPage" />
    </FlyoutItem>


    <FlyoutItem Title="Schedule lecture">
        <ShellContent
            ContentTemplate="{DataTemplate view:ScheduleLecturePage}"
            Route="ScheduleLecturePage" />
    </FlyoutItem>

    <FlyoutItem Title="Schedule test">
        <ShellContent
            ContentTemplate="{DataTemplate view:ScheduleTestPage}"
            Route="ScheduleLecturePage" />
    </FlyoutItem>

    <FlyoutItem Title="Manage participants">
        <ShellContent
            ContentTemplate="{DataTemplate view:ContactPage}"
            Route="ContactPage" />
    </FlyoutItem>

    <FlyoutItem Title="Manage courses">
        <ShellContent
            ContentTemplate="{DataTemplate view:CoursesPage}"
            Route="CoursesPage" />
    </FlyoutItem>

The admin, only can access "Manage Participant" and "Manage Course"

but the problem that I have is that in order to do that, when I received the object in my ShellViewModel, I need to do a switch case and set Booleans to true or false depending on the case.

I received my object in my shell, when I log in.

  [RelayCommand]
  async Task Login() {

      IsBusy = true;

      //var user = await authenticationService.LoginWithEmailAndPassword(User.Email, User.Password);
      var user = await authenticationService.LoginWithEmailAndPassword("******@admin.com", "111111");
      if (user != null) {

          var currentUser = await dataService.GetByUidAsync<User>("Users", user.Uid);

          if (currentUser != null) {

              appShellViewModel.User = currentUser;
          }

          await appService.NavigateTo($"//{nameof(HomePage)}", true);
      }

      IsBusy = false;
  }

  [RelayCommand]
  async Task Register() {

      var IsFilled = await VerifyUserAsync(User);

      if (IsFilled) {
          IsBusy = true;

          var user = await authenticationService.RegisterWithEmailAndPassword(User.Email, User.Password);
          if (user != null) {

              IsPopOpen = false;
              User.Uid = user.Uid;
              User.Id = User.GenerateRandomNumberString();
              User.Name = User.Name;
              User.Email = User.Email;
              User.Password = BCrypt.Net.BCrypt.HashPassword(User.Password);
              User.Role = User.Role;

              var currentUser = await dataService.GetByUidAsync<User>("Users", user.Uid);

              if (currentUser != null) {

                  appShellViewModel.User = currentUser;
              }

              await appService.NavigateTo($"//{nameof(HomePage)}", true);
          }
      }



      IsBusy = false;
  }

the problem is that I dont know to perform the check.

public partial class AppShellViewModel(IAuthenticationService authenticationService, IAppService appService) : BaseViewModel {

    [ObservableProperty]
    User? user;

    [RelayCommand]
    async Task SignOut() {

        authenticationService.SignOut();

        await appService.NavigateTo($"//{nameof(LoginPage)}", true);

    }
}

if I bind a method to the appeared command (Using the Maui toolkit) I can't because the shell is my entry point

App

https://github.com/eduardoagr/DemyAI

Developer technologies | .NET | .NET MAUI
0 comments No comments
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2023-12-11T08:47:48.9366667+00:00

    Hello,

    After testing, you can refer to the following code snippet to control the FlyoutItem entry based on the user role.

    if (currentUser != null) { 
        if(currentUser.Role.Equals("admin")) 
        { 
            foreach(var item in Shell.Current.Items) 
            { 
                if (item.Title != null && item.Title.Contains("Manage")) 
                { 
                    item.IsVisible = true; 
                } 
                else 
                { 
                    item.IsVisible = false; 
                } 
            } 
    
            WeakReferenceMessenger.Default.Send(currentUser, "UserLoggedIn"); 
            await appService.NavigateTo($"//{nameof(ContactPage)}", true); 
        } 
        else 
        { 
            foreach (var item in Shell.Current.Items) 
            { 
                if (item.Title != null && item.Title.Contains("Manage")) 
    
                { 
                    item.IsVisible = false; 
                }
                else 
                { 
                    item.IsVisible = true; 
    
                } 
            } 
    
            WeakReferenceMessenger.Default.Send(currentUser, "UserLoggedIn"); 
            await appService.NavigateTo($"//{nameof(HomePage)}", true); 
        }             
    } 
    

    Best Regards,

    Alec Liu.


    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 additional answers

Sort by: Most helpful

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.