arthur_menu
Arthur VALIN 2 years ago
parent 7883166abe
commit 3b96b44989

@ -1,4 +1,5 @@
<CascadingBlazoredModal> @using CraftSharp.Models;
<CascadingBlazoredModal>
<Router AppAssembly="@typeof(App).Assembly"> <Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData"> <Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(CraftLayout)" /> <AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(CraftLayout)" />
@ -13,3 +14,5 @@
</NotFound> </NotFound>
</Router> </Router>
</CascadingBlazoredModal> </CascadingBlazoredModal>

@ -5,6 +5,8 @@ using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using CraftSharp.Models; using CraftSharp.Models;
using CraftSharp.Services; using CraftSharp.Services;
using Blazorise;
using Newtonsoft.Json;
namespace CraftSharp.Pages namespace CraftSharp.Pages
{ {
@ -16,6 +18,7 @@ namespace CraftSharp.Pages
[Inject] [Inject]
public NavigationManager NavigationManager { get; set; } public NavigationManager NavigationManager { get; set; }
private string error { get; set; } private string error { get; set; }
private ConnexionModel loginRequest { get; set; } = new ConnexionModel(); private ConnexionModel loginRequest { get; set; } = new ConnexionModel();

@ -2,6 +2,8 @@
using Microsoft.Extensions.Localization; using Microsoft.Extensions.Localization;
using CraftSharp.Models; using CraftSharp.Models;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using CraftSharp.Services;
namespace CraftSharp.Pages namespace CraftSharp.Pages
{ {

@ -91,7 +91,7 @@
align-items:center; align-items:center;
position:relative; position:relative;
height: 500px; height: 500px;
margin-top: 10%;
} }
.chest { .chest {

@ -4,6 +4,5 @@
@{ @{
Layout = "_Layout"; Layout = "_Layout";
} }
<component type="typeof(App)" render-mode="ServerPrerendered"/>
<component type="typeof(App)" render-mode="ServerPrerendered" />

@ -15,6 +15,9 @@ using GraphQL.Client.Abstractions;
using GraphQL.Client.Http; using GraphQL.Client.Http;
using GraphQL.Client.Serializer.Newtonsoft; using GraphQL.Client.Serializer.Newtonsoft;
using CraftSharp; using CraftSharp;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using System;
using Microsoft.JSInterop;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -60,7 +63,6 @@ builder.Services.Configure<RequestLocalizationOptions>(options =>
}); });
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment()) if (!app.Environment.IsDevelopment())
{ {

@ -1,13 +1,21 @@
using CraftSharp.Models; using CraftSharp.Models;
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims; using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Microsoft.AspNetCore.Components;
using Blazorise;
using Microsoft.JSInterop;
using Microsoft.Extensions.Caching.Memory;
namespace CraftSharp.Services namespace CraftSharp.Services
{ {
public class CustomStateProvider : AuthenticationStateProvider public class CustomStateProvider : AuthenticationStateProvider
{ {
private readonly IAuthService _authService; private readonly IAuthService _authService;
private CurrentUser _currentUser;
[CascadingParameter]
public CurrentUser UserObject { get; set; }
public CustomStateProvider(IAuthService authService) public CustomStateProvider(IAuthService authService)
{ {
@ -22,7 +30,7 @@ namespace CraftSharp.Services
var userInfo = GetCurrentUser(); var userInfo = GetCurrentUser();
if (userInfo.IsAuthenticated) if (userInfo.IsAuthenticated)
{ {
var claims = new[] { new Claim(ClaimTypes.Name, _currentUser.UserName) }.Concat(_currentUser.Claims.Select(c => new Claim(c.Key, c.Value))); var claims = new[] { new Claim(ClaimTypes.Name, UserObject.UserName) }.Concat(UserObject.Claims.Select(c => new Claim(c.Key, c.Value)));
identity = new ClaimsIdentity(claims, "Server authentication"); identity = new ClaimsIdentity(claims, "Server authentication");
} }
} }
@ -37,17 +45,18 @@ namespace CraftSharp.Services
public async Task Login(ConnexionModel loginParameters) public async Task Login(ConnexionModel loginParameters)
{ {
_authService.Login(loginParameters); _authService.Login(loginParameters);
// No error - Login the user // No error - Login the user
var user = _authService.GetUser(loginParameters.UserName); CurrentUser user;
_currentUser = user; user = _authService.GetUser(loginParameters.UserName);
UserObject = user;
Console.WriteLine("\t\tLOGIN: " + UserObject.UserName);
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
} }
public async Task Logout() public async Task Logout()
{ {
_currentUser = null;
UserObject = new CurrentUser();
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
} }
@ -57,18 +66,19 @@ namespace CraftSharp.Services
// No error - Login the user // No error - Login the user
var user = _authService.GetUser(registerParameters.UserName); var user = _authService.GetUser(registerParameters.UserName);
_currentUser = user; UserObject = user;
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync()); NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
} }
public CurrentUser GetCurrentUser() public CurrentUser GetCurrentUser()
{ {
if (_currentUser != null && _currentUser.IsAuthenticated) CurrentUser cacheUser;
if (UserObject != null && UserObject.IsAuthenticated)
{ {
return _currentUser; Console.WriteLine("Return user");
return UserObject;
} }
return new CurrentUser(); return new CurrentUser();
} }
} }

@ -12,33 +12,3 @@
</select> </select>
</label> </label>
</p> </p>
@code
{
private CultureInfo[] supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr-FR"),
new CultureInfo("tr-TR")
};
private CultureInfo Culture
{
get => CultureInfo.CurrentCulture;
set
{
if (CultureInfo.CurrentUICulture == value)
{
return;
}
var culture = value.Name.ToLower(CultureInfo.InvariantCulture);
var uri = new Uri(this.NavigationManager.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
var query = $"?culture={Uri.EscapeDataString(culture)}&" + $"redirectUri={Uri.EscapeDataString(uri)}";
// Redirect the user to the culture controller to set the cookie
this.NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true);
}
}
}

@ -0,0 +1,36 @@
using CraftSharp.Services;
using Microsoft.AspNetCore.Components;
using System.Globalization;
namespace CraftSharp.Shared
{
public partial class CultureSelector
{
private CultureInfo[] supportedCultures = new[]
{
new CultureInfo("en-US"),
new CultureInfo("fr-FR"),
new CultureInfo("tr-TR")
};
private CultureInfo Culture
{
get => CultureInfo.CurrentCulture;
set
{
if (CultureInfo.CurrentUICulture == value)
{
return;
}
var culture = value.Name.ToLower(CultureInfo.InvariantCulture);
var uri = new Uri(this.NavigationManager.Uri).GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
var query = $"?culture={Uri.EscapeDataString(culture)}&" + $"redirectUri={Uri.EscapeDataString(uri)}";
// Redirect the user to the culture controller to set the cookie
this.NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true);
}
}
}
}

@ -22,10 +22,10 @@
</span> </span>
</NavLink> </NavLink>
</li> </li>
<span class="img_wrap"> <a class="img_wrap" href="/index">
<img class="nav-image" src="Images/CraftSharp-Logo.png" alt="Logo Application"/> <img class="nav-image" src="Images/CraftSharp-Logo.png" alt="Logo Application"/>
<img class="nav-image" src="/Images/@(Localizer["subtext-img"]).png" width="70%" /> <img class="nav-image" src="/Images/@(Localizer["subtext-img"]).png" width="70%" />
</span> </a>
<li class="nav-item"> <li class="nav-item">
<NavLink class="nav-link" href="opening"> <NavLink class="nav-link" href="opening">

@ -40,7 +40,6 @@ namespace CraftSharp.Shared
async public void isAdmin() async public void isAdmin()
{ {
var authState = await AuthenticationState;
var roles = AuthStateProvider.GetCurrentUser().Roles; var roles = AuthStateProvider.GetCurrentUser().Roles;
isUserAdmin = roles.Contains(UserRoles.Admin); isUserAdmin = roles.Contains(UserRoles.Admin);
} }

@ -45,8 +45,6 @@
position: absolute; position: absolute;
left: 75%; left: 75%;
transform: translate(-50%, 0); transform: translate(-50%, 0);
user-select: none;
pointer-events: none;
} }
.nav-image { .nav-image {

Loading…
Cancel
Save