Add authentication and authorization (#21)
Reviewed-on: #21
This commit was merged in pull request #21.
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
using GameIdeas.BlazorApp.Services;
|
||||
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,
|
||||
AuthenticationStateProvider stateProvider) : IAuthGateway
|
||||
{
|
||||
public async Task<bool> Login(UserDto userDto)
|
||||
{
|
||||
try
|
||||
{
|
||||
var token = await httpClient.PostAsync<TokenDto>(Endpoints.Auth.Login, userDto);
|
||||
await ((JwtAuthenticationStateProvider)stateProvider).NotifyUserAuthenticationAsync(token!.Token!);
|
||||
return true;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw new AuthenticationUserException(ResourcesKey.UserLoginFailed);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task Logout()
|
||||
{
|
||||
try
|
||||
{
|
||||
await ((JwtAuthenticationStateProvider)stateProvider).NotifyUserLogoutAsync();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
throw new AuthenticationUserException(ResourcesKey.UserLogoutFailed);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user