diff --git a/src/CraftSharp/Models/CurrentUser.cs b/src/CraftSharp/Models/CurrentUser.cs index 1e0c0a8..94c2718 100644 --- a/src/CraftSharp/Models/CurrentUser.cs +++ b/src/CraftSharp/Models/CurrentUser.cs @@ -7,7 +7,7 @@ public string UserName { get; set; } public int NumberOfKeys { get; set; } = 0; public List Inventory { get; set; } = new List(); - + public List Roles { get; set; } = new List() { UserRoles.User }; } } diff --git a/src/CraftSharp/Pages/Opening.razor.cs b/src/CraftSharp/Pages/Opening.razor.cs index 540b8d0..a9c132b 100644 --- a/src/CraftSharp/Pages/Opening.razor.cs +++ b/src/CraftSharp/Pages/Opening.razor.cs @@ -25,7 +25,7 @@ namespace CraftSharp.Pages public IDataService DataService { get; set; } [Inject] - public IAuthService AuthService { get; set; } + public CustomStateProvider AuthService { get; set; } [CascadingParameter] public Task Context { get; set; } @@ -46,7 +46,7 @@ namespace CraftSharp.Pages items = await DataService.List(0, totalItem); var authState = await Context; - NumberOfKeys = AuthService.GetCurrentUser(authState.User.Identity.Name).numberOfKeys; + NumberOfKeys = AuthService.GetCurrentUser().NumberOfKeys; } bool canOpen() diff --git a/src/CraftSharp/Services/AuthService.cs b/src/CraftSharp/Services/AuthService.cs index c975fc2..50c2c55 100644 --- a/src/CraftSharp/Services/AuthService.cs +++ b/src/CraftSharp/Services/AuthService.cs @@ -15,7 +15,7 @@ namespace CraftSharp.Services new AppUser { UserName = "Admin", Password = "123456", Roles = new List { UserRoles.Admin }, numberOfKeys=999 } }; } - public AppUser GetCurrentUser(string userName) +/* public AppUser GetCurrentUser(string userName) { var user = CurrentUser.FirstOrDefault(w => w.UserName == userName); @@ -25,7 +25,7 @@ namespace CraftSharp.Services } return user; - } + }*/ public CurrentUser GetUser(string userName) @@ -46,6 +46,7 @@ namespace CraftSharp.Services UserName = user.UserName, NumberOfKeys = user.numberOfKeys, Inventory = user.inventory, + Roles = user.Roles, Claims = claims.ToDictionary(c => c.Type, c => c.Value) }; } diff --git a/src/CraftSharp/Services/CustomStateProvider.cs b/src/CraftSharp/Services/CustomStateProvider.cs index 7270a67..59abbd2 100644 --- a/src/CraftSharp/Services/CustomStateProvider.cs +++ b/src/CraftSharp/Services/CustomStateProvider.cs @@ -62,7 +62,7 @@ namespace CraftSharp.Services NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } - private CurrentUser GetCurrentUser() + public CurrentUser GetCurrentUser() { if (_currentUser != null && _currentUser.IsAuthenticated) { diff --git a/src/CraftSharp/Services/IAuthService.cs b/src/CraftSharp/Services/IAuthService.cs index 99a9140..255eb81 100644 --- a/src/CraftSharp/Services/IAuthService.cs +++ b/src/CraftSharp/Services/IAuthService.cs @@ -4,8 +4,6 @@ namespace CraftSharp.Services { public interface IAuthService { - AppUser GetCurrentUser(string userName); - CurrentUser GetUser(string userName); void Login(ConnexionModel loginRequest); diff --git a/src/CraftSharp/Shared/HeaderLayout.razor.cs b/src/CraftSharp/Shared/HeaderLayout.razor.cs index c4ed708..468525a 100644 --- a/src/CraftSharp/Shared/HeaderLayout.razor.cs +++ b/src/CraftSharp/Shared/HeaderLayout.razor.cs @@ -12,9 +12,6 @@ namespace CraftSharp.Shared [Inject] public IStringLocalizer Localizer { get; set; } - [Inject] - public IAuthService AuthService { get; set; } - [Inject] public CustomStateProvider AuthStateProvider { get; set; } @@ -44,18 +41,9 @@ namespace CraftSharp.Shared async public void isAdmin() { var authState = await AuthenticationState; - var roles = AuthService.GetCurrentUser(authState.User.Identity.Name).Roles; + var roles = AuthStateProvider.GetCurrentUser().Roles; isUserAdmin = roles.Contains(UserRoles.Admin); } - - /* protected override async Task OnParametersSetAsync() - { - if (!(await AuthenticationState).User.Identity.IsAuthenticated) - { - NavigationManager.NavigateTo("/inscription"); - } - }*/ - private async Task LogoutClick() { await AuthStateProvider.Logout();