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