From 410c7cd4c24c0537eddd6dd4ce9858d4b525144c Mon Sep 17 00:00:00 2001 From: "arthur.valin" Date: Sat, 31 Dec 2022 01:14:12 +0100 Subject: [PATCH] Creating UserController --- src/CraftSharp/Controllers/UserController.cs | 45 +++++++++++++++++++ src/CraftSharp/Pages/Connexion.razor.cs | 11 ++++- src/CraftSharp/Pages/Opening.razor.cs | 2 - src/CraftSharp/Program.cs | 3 +- .../Services/CustomStateProvider.cs | 7 +++ 5 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 src/CraftSharp/Controllers/UserController.cs diff --git a/src/CraftSharp/Controllers/UserController.cs b/src/CraftSharp/Controllers/UserController.cs new file mode 100644 index 0000000..78a9d8a --- /dev/null +++ b/src/CraftSharp/Controllers/UserController.cs @@ -0,0 +1,45 @@ +using CraftSharp.Models; +using CraftSharp.Services; +using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.Localization; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json; +using System.Globalization; +using System.Net; + +namespace CraftSharp.Controllers +{ + [Microsoft.AspNetCore.Mvc.Route("[controller]/[action]")] + public class UserController : Controller + { + [HttpPost] + public IActionResult SetUser([FromBody] String user) + { + if (user != null) + { + HttpContext.Response.Cookies.Append( + "CurrentUser", user + ); + } + Console.WriteLine("USER : " + user); + + return Ok(new { result = "userCookieSet" }); + } + + public IActionResult DeleteUser() + { + + this.HttpContext.Response.Cookies.Delete( + "CurrentUser" + ); + return Ok(new { result = "userCookieDeleted" }); + + } + + public IActionResult GetUser() + { + var jsonUser = HttpContext.Request.Cookies["CurrentUser"]; + return Ok(new { result = JsonConvert.DeserializeObject(jsonUser) }); + } + } +} \ No newline at end of file diff --git a/src/CraftSharp/Pages/Connexion.razor.cs b/src/CraftSharp/Pages/Connexion.razor.cs index 0d6c7e1..d1383b7 100644 --- a/src/CraftSharp/Pages/Connexion.razor.cs +++ b/src/CraftSharp/Pages/Connexion.razor.cs @@ -7,6 +7,9 @@ using CraftSharp.Models; using CraftSharp.Services; using Blazorise; using Newtonsoft.Json; +using System.Text.RegularExpressions; +using Newtonsoft.Json.Linq; +using System.Text; namespace CraftSharp.Pages { @@ -17,7 +20,9 @@ namespace CraftSharp.Pages [Inject] public NavigationManager NavigationManager { get; set; } - + + [Inject] + public HttpClient httpClient { get; set; } private string error { get; set; } private ConnexionModel loginRequest { get; set; } = new ConnexionModel(); @@ -28,7 +33,11 @@ namespace CraftSharp.Pages try { await AuthStateProvider.Login(loginRequest); + + var stringified = JsonConvert.SerializeObject(AuthStateProvider.GetCurrentUser()); + var response = await httpClient.PostAsJsonAsync($"{NavigationManager.BaseUri}User/SetUser", stringified); NavigationManager.NavigateTo("index"); + } catch (Exception ex) { diff --git a/src/CraftSharp/Pages/Opening.razor.cs b/src/CraftSharp/Pages/Opening.razor.cs index 5347b86..6ef5dce 100644 --- a/src/CraftSharp/Pages/Opening.razor.cs +++ b/src/CraftSharp/Pages/Opening.razor.cs @@ -26,8 +26,6 @@ namespace CraftSharp.Pages public CustomStateProvider AuthService { get; set; } [Inject] public CustomStateProvider AuthStateProvider { get; set; } - [CascadingParameter] - public Task Context { get; set; } int NumberOfKeys { get; set; } = 0; int CostInKeys { get; set; } = 1; diff --git a/src/CraftSharp/Program.cs b/src/CraftSharp/Program.cs index 31efd19..21857d6 100644 --- a/src/CraftSharp/Program.cs +++ b/src/CraftSharp/Program.cs @@ -18,6 +18,7 @@ using CraftSharp; using Microsoft.AspNetCore.Mvc.Infrastructure; using System; using Microsoft.JSInterop; +using CraftSharp.Controllers; var builder = WebApplication.CreateBuilder(args); @@ -36,7 +37,7 @@ builder.Services.AddControllers(); // Add the localization to the app and specify the resources path builder.Services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; }); - +builder.Services.AddScoped(); builder.Services.AddHttpClient(); builder.Services.AddBlazoredModal(); diff --git a/src/CraftSharp/Services/CustomStateProvider.cs b/src/CraftSharp/Services/CustomStateProvider.cs index 059cab0..74adb2f 100644 --- a/src/CraftSharp/Services/CustomStateProvider.cs +++ b/src/CraftSharp/Services/CustomStateProvider.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Components; using Blazorise; using Microsoft.JSInterop; using Microsoft.Extensions.Caching.Memory; +using System; namespace CraftSharp.Services { @@ -14,6 +15,9 @@ namespace CraftSharp.Services { private readonly IAuthService _authService; + [Inject] + public NavigationManager NavigationManager { get; set; } + private CurrentUser _currentUser { get; set; } public CustomStateProvider(IAuthService authService) @@ -48,6 +52,7 @@ namespace CraftSharp.Services CurrentUser user; user = _authService.GetUser(loginParameters.UserName); _currentUser = user; + NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } @@ -55,6 +60,7 @@ namespace CraftSharp.Services { _currentUser = new CurrentUser(); + NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); } @@ -65,6 +71,7 @@ namespace CraftSharp.Services // No error - Login the user var user = _authService.GetUser(registerParameters.UserName); _currentUser = user; + NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); }