diff --git a/src/CraftSharp/App.razor b/src/CraftSharp/App.razor index 79d057a..4a40c50 100644 --- a/src/CraftSharp/App.razor +++ b/src/CraftSharp/App.razor @@ -2,17 +2,16 @@ - - + + - +

Sorry, there's nothing at this address.

-
+
- diff --git a/src/CraftSharp/Controllers/UserController.cs b/src/CraftSharp/Controllers/UserController.cs index 78a9d8a..ef09391 100644 --- a/src/CraftSharp/Controllers/UserController.cs +++ b/src/CraftSharp/Controllers/UserController.cs @@ -21,11 +21,10 @@ namespace CraftSharp.Controllers "CurrentUser", user ); } - Console.WriteLine("USER : " + user); - return Ok(new { result = "userCookieSet" }); } + [HttpDelete] public IActionResult DeleteUser() { @@ -36,10 +35,13 @@ namespace CraftSharp.Controllers } + [HttpGet] public IActionResult GetUser() { var jsonUser = HttpContext.Request.Cookies["CurrentUser"]; - return Ok(new { result = JsonConvert.DeserializeObject(jsonUser) }); + return Ok(jsonUser); + } + } } \ No newline at end of file diff --git a/src/CraftSharp/Pages/Connexion.razor.cs b/src/CraftSharp/Pages/Connexion.razor.cs index d1383b7..7602a1f 100644 --- a/src/CraftSharp/Pages/Connexion.razor.cs +++ b/src/CraftSharp/Pages/Connexion.razor.cs @@ -26,15 +26,13 @@ namespace CraftSharp.Pages private string error { get; set; } private ConnexionModel loginRequest { get; set; } = new ConnexionModel(); - private async Task OnSubmit() { error = null; try { await AuthStateProvider.Login(loginRequest); - - var stringified = JsonConvert.SerializeObject(AuthStateProvider.GetCurrentUser()); + var stringified = JsonConvert.SerializeObject(loginRequest); var response = await httpClient.PostAsJsonAsync($"{NavigationManager.BaseUri}User/SetUser", stringified); NavigationManager.NavigateTo("index"); diff --git a/src/CraftSharp/Pages/Opening.razor b/src/CraftSharp/Pages/Opening.razor index be844b6..6487a7d 100644 --- a/src/CraftSharp/Pages/Opening.razor +++ b/src/CraftSharp/Pages/Opening.razor @@ -11,7 +11,7 @@ - +@AuthStateProvider.GetCurrentUser().UserName;
diff --git a/src/CraftSharp/Pages/Opening.razor.cs b/src/CraftSharp/Pages/Opening.razor.cs index 6ef5dce..ef5158f 100644 --- a/src/CraftSharp/Pages/Opening.razor.cs +++ b/src/CraftSharp/Pages/Opening.razor.cs @@ -23,10 +23,7 @@ namespace CraftSharp.Pages [Inject] public IDataService DataService { get; set; } [Inject] - public CustomStateProvider AuthService { get; set; } - [Inject] public CustomStateProvider AuthStateProvider { get; set; } - int NumberOfKeys { get; set; } = 0; int CostInKeys { get; set; } = 1; @@ -42,7 +39,7 @@ namespace CraftSharp.Pages items = await DataService.List(0, totalItem); - NumberOfKeys = AuthService.GetCurrentUser().NumberOfKeys; + NumberOfKeys = AuthStateProvider.GetCurrentUser().NumberOfKeys; } bool canOpen() diff --git a/src/CraftSharp/Pages/_Host.cshtml b/src/CraftSharp/Pages/_Host.cshtml index 30a855b..3451d14 100644 --- a/src/CraftSharp/Pages/_Host.cshtml +++ b/src/CraftSharp/Pages/_Host.cshtml @@ -1,8 +1,24 @@ @page "/" @namespace CraftSharp.Pages +@using CraftSharp.Models; +@using CraftSharp.Services; +@using Microsoft.AspNetCore.Components; +@using Newtonsoft.Json; @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers +@inject CustomStateProvider authService; +@inject HttpClient httpClient; + @{ Layout = "_Layout"; + Console.WriteLine("==============START=============="); + var response = await httpClient.GetAsync($"https://localhost:7139/User/GetUser"); + string jsonUser = await response.Content.ReadAsStringAsync(); + var user = new ConnexionModel(); + if (jsonUser != null && jsonUser.Length != 0) + { + user = JsonConvert.DeserializeObject(jsonUser); + await authService.Login(user); + } } diff --git a/src/CraftSharp/Program.cs b/src/CraftSharp/Program.cs index 21857d6..9405507 100644 --- a/src/CraftSharp/Program.cs +++ b/src/CraftSharp/Program.cs @@ -28,9 +28,9 @@ builder.Services.AddServerSideBlazor(); builder.Services.AddSingleton(); builder.Services.AddOptions(); builder.Services.AddAuthorizationCore(); -builder.Services.AddScoped(); -builder.Services.AddScoped(s => s.GetRequiredService()); -builder.Services.AddScoped(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(s => s.GetRequiredService()); +builder.Services.AddSingleton(); // Add the controller of the app builder.Services.AddControllers(); diff --git a/src/CraftSharp/Services/AuthService.cs b/src/CraftSharp/Services/AuthService.cs index 5652c56..347854f 100644 --- a/src/CraftSharp/Services/AuthService.cs +++ b/src/CraftSharp/Services/AuthService.cs @@ -41,10 +41,13 @@ namespace CraftSharp.Services public void Login(ConnexionModel loginRequest) { + Console.WriteLine("LOGIN : " + loginRequest.UserName); var user = CurrentUser.FirstOrDefault(w => w.UserName == loginRequest.UserName && w.Password == loginRequest.Password); if (user == null) { + Console.WriteLine("LOGINFAILED"); + throw new Exception("User name or password invalid !"); } } diff --git a/src/CraftSharp/Services/CustomStateProvider.cs b/src/CraftSharp/Services/CustomStateProvider.cs index 74adb2f..f1bbc58 100644 --- a/src/CraftSharp/Services/CustomStateProvider.cs +++ b/src/CraftSharp/Services/CustomStateProvider.cs @@ -7,7 +7,6 @@ using Microsoft.AspNetCore.Components; using Blazorise; using Microsoft.JSInterop; using Microsoft.Extensions.Caching.Memory; -using System; namespace CraftSharp.Services { @@ -15,9 +14,6 @@ namespace CraftSharp.Services { private readonly IAuthService _authService; - [Inject] - public NavigationManager NavigationManager { get; set; } - private CurrentUser _currentUser { get; set; } public CustomStateProvider(IAuthService authService) @@ -52,7 +48,6 @@ namespace CraftSharp.Services CurrentUser user; user = _authService.GetUser(loginParameters.UserName); _currentUser = user; - NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } @@ -60,7 +55,6 @@ namespace CraftSharp.Services { _currentUser = new CurrentUser(); - NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } @@ -71,18 +65,19 @@ namespace CraftSharp.Services // No error - Login the user var user = _authService.GetUser(registerParameters.UserName); _currentUser = user; - NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } public CurrentUser GetCurrentUser() { - CurrentUser cacheUser; if (_currentUser != null && _currentUser.IsAuthenticated) { + Console.WriteLine("GETUSER: " + _currentUser.UserName); return _currentUser; } + Console.WriteLine("GETUSER: FAIL"); + return new CurrentUser(); } }