Correcting bug with GetCurrentUser function

arthur_menu
Arthur VALIN 2 years ago
parent 905db68161
commit 7883166abe

@ -7,7 +7,7 @@
public string UserName { get; set; } public string UserName { get; set; }
public int NumberOfKeys { get; set; } = 0; public int NumberOfKeys { get; set; } = 0;
public List<Item> Inventory { get; set; } = new List<Item>(); public List<Item> Inventory { get; set; } = new List<Item>();
public List<UserRoles> Roles { get; set; } = new List<UserRoles>() { UserRoles.User };
} }
} }

@ -25,7 +25,7 @@ namespace CraftSharp.Pages
public IDataService DataService { get; set; } public IDataService DataService { get; set; }
[Inject] [Inject]
public IAuthService AuthService { get; set; } public CustomStateProvider AuthService { get; set; }
[CascadingParameter] [CascadingParameter]
public Task<AuthenticationState> Context { get; set; } public Task<AuthenticationState> Context { get; set; }
@ -46,7 +46,7 @@ namespace CraftSharp.Pages
items = await DataService.List(0, totalItem); items = await DataService.List(0, totalItem);
var authState = await Context; var authState = await Context;
NumberOfKeys = AuthService.GetCurrentUser(authState.User.Identity.Name).numberOfKeys; NumberOfKeys = AuthService.GetCurrentUser().NumberOfKeys;
} }
bool canOpen() bool canOpen()

@ -15,7 +15,7 @@ namespace CraftSharp.Services
new AppUser { UserName = "Admin", Password = "123456", Roles = new List<UserRoles> { UserRoles.Admin }, numberOfKeys=999 } new AppUser { UserName = "Admin", Password = "123456", Roles = new List<UserRoles> { UserRoles.Admin }, numberOfKeys=999 }
}; };
} }
public AppUser GetCurrentUser(string userName) /* public AppUser GetCurrentUser(string userName)
{ {
var user = CurrentUser.FirstOrDefault(w => w.UserName == userName); var user = CurrentUser.FirstOrDefault(w => w.UserName == userName);
@ -25,7 +25,7 @@ namespace CraftSharp.Services
} }
return user; return user;
} }*/
public CurrentUser GetUser(string userName) public CurrentUser GetUser(string userName)
@ -46,6 +46,7 @@ namespace CraftSharp.Services
UserName = user.UserName, UserName = user.UserName,
NumberOfKeys = user.numberOfKeys, NumberOfKeys = user.numberOfKeys,
Inventory = user.inventory, Inventory = user.inventory,
Roles = user.Roles,
Claims = claims.ToDictionary(c => c.Type, c => c.Value) Claims = claims.ToDictionary(c => c.Type, c => c.Value)
}; };
} }

@ -62,7 +62,7 @@ namespace CraftSharp.Services
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
} }
private CurrentUser GetCurrentUser() public CurrentUser GetCurrentUser()
{ {
if (_currentUser != null && _currentUser.IsAuthenticated) if (_currentUser != null && _currentUser.IsAuthenticated)
{ {

@ -4,8 +4,6 @@ namespace CraftSharp.Services
{ {
public interface IAuthService public interface IAuthService
{ {
AppUser GetCurrentUser(string userName);
CurrentUser GetUser(string userName); CurrentUser GetUser(string userName);
void Login(ConnexionModel loginRequest); void Login(ConnexionModel loginRequest);

@ -12,9 +12,6 @@ namespace CraftSharp.Shared
[Inject] [Inject]
public IStringLocalizer<HeaderLayout> Localizer { get; set; } public IStringLocalizer<HeaderLayout> Localizer { get; set; }
[Inject]
public IAuthService AuthService { get; set; }
[Inject] [Inject]
public CustomStateProvider AuthStateProvider { get; set; } public CustomStateProvider AuthStateProvider { get; set; }
@ -44,18 +41,9 @@ namespace CraftSharp.Shared
async public void isAdmin() async public void isAdmin()
{ {
var authState = await AuthenticationState; var authState = await AuthenticationState;
var roles = AuthService.GetCurrentUser(authState.User.Identity.Name).Roles; var roles = AuthStateProvider.GetCurrentUser().Roles;
isUserAdmin = roles.Contains(UserRoles.Admin); isUserAdmin = roles.Contains(UserRoles.Admin);
} }
/* protected override async Task OnParametersSetAsync()
{
if (!(await AuthenticationState).User.Identity.IsAuthenticated)
{
NavigationManager.NavigateTo("/inscription");
}
}*/
private async Task LogoutClick() private async Task LogoutClick()
{ {
await AuthStateProvider.Logout(); await AuthStateProvider.Logout();

Loading…
Cancel
Save