Enable authentication
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
using GameIdeas.BlazorApp.Pages.User.Gateways;
|
||||
using GameIdeas.Shared.Dto;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Forms;
|
||||
|
||||
namespace GameIdeas.BlazorApp.Pages.User.Components;
|
||||
|
||||
public partial class Login
|
||||
{
|
||||
[Parameter] public IAuthGateway AuthGateway { get; set; } = default!;
|
||||
|
||||
private EditContext? EditContext;
|
||||
private UserDto UserDto = new();
|
||||
private bool IsLoading = false;
|
||||
@@ -21,9 +25,19 @@ public partial class Login
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
IsLoading = true;
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
IsLoading = false;
|
||||
await AuthGateway.Login(UserDto);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
finally
|
||||
{
|
||||
IsLoading = false;
|
||||
StateHasChanged();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,18 +3,19 @@ using GameIdeas.BlazorApp.Shared.Constants;
|
||||
using GameIdeas.BlazorApp.Shared.Exceptions;
|
||||
using GameIdeas.Resources;
|
||||
using GameIdeas.Shared.Dto;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
|
||||
namespace GameIdeas.BlazorApp.Pages.User.Gateways;
|
||||
|
||||
public class AuthGateway(IHttpClientService httpClient,
|
||||
JwtAuthenticationStateProvider stateProvider) : IAuthGateway
|
||||
AuthenticationStateProvider stateProvider) : IAuthGateway
|
||||
{
|
||||
public async Task<bool> Login(UserDto userDto)
|
||||
{
|
||||
try
|
||||
{
|
||||
var token = await httpClient.PostAsync<TokenDto>(Endpoints.Auth.Login, userDto);
|
||||
await stateProvider.NotifyUserAuthenticationAsync(token!.Token!);
|
||||
await ((JwtAuthenticationStateProvider)stateProvider).NotifyUserAuthenticationAsync(token!.Token!);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
@@ -27,7 +28,7 @@ public class AuthGateway(IHttpClientService httpClient,
|
||||
{
|
||||
try
|
||||
{
|
||||
await stateProvider.NotifyUserLogoutAsync();
|
||||
await ((JwtAuthenticationStateProvider)stateProvider).NotifyUserLogoutAsync();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
@using GameIdeas.BlazorApp.Shared.Components.BackdropFilter
|
||||
@using GameIdeas.BlazorApp.Pages.User.Components
|
||||
@using GameIdeas.BlazorApp.Shared.Components.BackdropFilter
|
||||
@using GameIdeas.BlazorApp.Shared.Constants
|
||||
@using GameIdeas.Shared.Constants
|
||||
@using Microsoft.AspNetCore.Components.Authorization
|
||||
|
||||
<div class="menu">
|
||||
<div class="icon" @onclick=HandleAccountClicked>
|
||||
@@ -9,13 +12,36 @@
|
||||
@if (ContentVisile)
|
||||
{
|
||||
<div class="content">
|
||||
<CascadingAuthenticationState>
|
||||
<AuthorizeView Roles="@($"{GlobalConstants.ADMINISTRATOR}, {GlobalConstants.MEMBER}")">
|
||||
<Authorized>
|
||||
<div class="menu-element">
|
||||
@ResourcesKey.CategoriesManager
|
||||
</div>
|
||||
<span class="line"></span>
|
||||
</Authorized>
|
||||
</AuthorizeView>
|
||||
|
||||
<AuthorizeView Roles="@GlobalConstants.ADMINISTRATOR">
|
||||
<Authorized>
|
||||
<div class="menu-element">
|
||||
@ResourcesKey.UserManager
|
||||
</div>
|
||||
<span class="line"></span>
|
||||
</Authorized>
|
||||
</AuthorizeView>
|
||||
|
||||
<AuthorizeView>
|
||||
<Authorized>
|
||||
<div class="menu-element" @onclick="HandleLogoutClicked">
|
||||
@ResourcesKey.Logout
|
||||
</div>
|
||||
</Authorized>
|
||||
<NotAuthorized>
|
||||
<Login AuthGateway="AuthGateway" />
|
||||
</NotAuthorized>
|
||||
</AuthorizeView>
|
||||
</CascadingAuthenticationState>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
using GameIdeas.BlazorApp.Pages.User.Gateways;
|
||||
using GameIdeas.BlazorApp.Services;
|
||||
using Microsoft.AspNetCore.Components;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
|
||||
namespace GameIdeas.BlazorApp.Pages.User;
|
||||
|
||||
public partial class UserMenu
|
||||
{
|
||||
[Inject] private IAuthGateway AuthGateway { get; set; } = default!;
|
||||
|
||||
private bool ContentVisile = false;
|
||||
|
||||
private void HandleLogoutClicked()
|
||||
private async Task HandleLogoutClicked()
|
||||
{
|
||||
await AuthGateway.Logout();
|
||||
ContentVisile = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ using GameIdeas.BlazorApp.Pages.Games.Gateways;
|
||||
using GameIdeas.BlazorApp.Pages.User.Gateways;
|
||||
using GameIdeas.BlazorApp.Services;
|
||||
using GameIdeas.Resources;
|
||||
using Microsoft.AspNetCore.Components.Authorization;
|
||||
using Microsoft.AspNetCore.Components.Web;
|
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
|
||||
|
||||
@@ -30,7 +31,7 @@ services.AddHttpClient(
|
||||
services.AddBlazoredLocalStorage();
|
||||
services.AddAuthorizationCore();
|
||||
|
||||
services.AddScoped<JwtAuthenticationStateProvider>();
|
||||
services.AddScoped<AuthenticationStateProvider, JwtAuthenticationStateProvider>();
|
||||
|
||||
services.AddScoped<IHttpClientService, HttpClientService>();
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ public class Translations (TranslationService translationService)
|
||||
public string EnterUsername => translationService.Translate(nameof(EnterUsername));
|
||||
public string EnterPassword => translationService.Translate(nameof(EnterPassword));
|
||||
public string UserManager => translationService.Translate(nameof(UserManager));
|
||||
public string CategoriesManager => translationService.Translate(nameof(CategoriesManager));
|
||||
public string Filters => translationService.Translate(nameof(Filters));
|
||||
public string LastAdd => translationService.Translate(nameof(LastAdd));
|
||||
public string Research => translationService.Translate(nameof(Research));
|
||||
@@ -72,6 +73,7 @@ public static class ResourcesKey
|
||||
public static string EnterUsername => _instance?.EnterUsername ?? throw new InvalidOperationException("ResourcesKey.EnterUsername is not initialized.");
|
||||
public static string EnterPassword => _instance?.EnterPassword ?? throw new InvalidOperationException("ResourcesKey.EnterPassword is not initialized.");
|
||||
public static string UserManager => _instance?.UserManager ?? throw new InvalidOperationException("ResourcesKey.UserManager is not initialized.");
|
||||
public static string CategoriesManager => _instance?.CategoriesManager ?? throw new InvalidOperationException("ResourcesKey.CategoriesManager is not initialized.");
|
||||
public static string Filters => _instance?.Filters ?? throw new InvalidOperationException("ResourcesKey.Filters is not initialized.");
|
||||
public static string LastAdd => _instance?.LastAdd ?? throw new InvalidOperationException("ResourcesKey.LastAdd is not initialized.");
|
||||
public static string Research => _instance?.Research ?? throw new InvalidOperationException("ResourcesKey.Research is not initialized.");
|
||||
|
||||
@@ -5,13 +5,15 @@ using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace GameIdeas.WebAPI.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class UserController(
|
||||
IUserService userService,
|
||||
ILoggerFactory loggerFactory) : Controller
|
||||
{
|
||||
private readonly ILogger<UserController> logger = loggerFactory.CreateLogger<UserController>();
|
||||
|
||||
[HttpPost("login")]
|
||||
[HttpPost("Login")]
|
||||
public async Task<ActionResult<TokenDto>> Login([FromBody] UserDto model)
|
||||
{
|
||||
try
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
"EnterUsername": "Nom d'utilisateur",
|
||||
"EnterPassword": "Mot de passe",
|
||||
"UserManager": "Gestion des utilisateurs",
|
||||
"CategoriesManager": "Gestion des catégories",
|
||||
"Filters": "Les filtres",
|
||||
"LastAdd": "Les ajouts récents",
|
||||
"Research": "Rechercher",
|
||||
|
||||
@@ -3,6 +3,7 @@ using GameIdeas.Shared.Model;
|
||||
using GameIdeas.WebAPI.Context;
|
||||
using GameIdeas.WebAPI.Services.Categories;
|
||||
using GameIdeas.WebAPI.Services.Games;
|
||||
using GameIdeas.WebAPI.Services.Users;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -63,6 +64,7 @@ services.AddAuthorization();
|
||||
services.AddSingleton<TranslationService>();
|
||||
services.AddSingleton<Translations>();
|
||||
|
||||
services.AddScoped<IUserService, UserService>();
|
||||
services.AddScoped<IGameReadService, GameReadService>();
|
||||
services.AddScoped<IGameWriteService, GameWriteService>();
|
||||
services.AddScoped<ICategoryService, CategoryService>();
|
||||
|
||||
Reference in New Issue
Block a user