Skip to content

Commit

Permalink
Finish login & logout services
Browse files Browse the repository at this point in the history
  • Loading branch information
oveldman committed Mar 23, 2024
1 parent df532fe commit 0edef71
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using MadWorldNL.Clients.Admin.Domain;
using MadWorldNL.Clients.Admin.Services.Authentications;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;

namespace MadWorldNL.Clients.Admin.Application.Authentications;

public class AuthenticationManager : IAuthenticationManager
{
private readonly IAuthenticationService _authenticationService;
private readonly JwtAuthenticationStateProvider _authenticationStateProvider;
private readonly ProtectedLocalStorage _localStorage;

public AuthenticationManager(
IAuthenticationService authenticationService,
AuthenticationStateProvider authenticationStateProvider,
ProtectedLocalStorage localStorage)
{
_authenticationService = authenticationService;
_authenticationStateProvider = (JwtAuthenticationStateProvider)authenticationStateProvider;
_localStorage = localStorage;
}

public async Task<AuthenticationToken> LoginFromSessionAsync()
{
var storageResult = await _localStorage.GetAsync<AuthenticationToken>(AuthenticationToken.Entry);

if (!storageResult.Success || !(storageResult.Value?.IsSuccess ?? false))
{
return new AuthenticationToken();
}

_authenticationStateProvider.Authenticate(storageResult.Value.AccessToken);
return storageResult.Value;

}

public async Task<AuthenticationToken> LoginAsync(string username, string password)
{
var token = _authenticationService.Login(username, password, "localhost");

if (!token.IsSuccess)
{
return token;
}

await _localStorage.SetAsync(AuthenticationToken.Entry, token);
_authenticationStateProvider.Authenticate(token.AccessToken);

return token;
}

public async Task LogoutAsync()
{
_authenticationStateProvider.Logout();
await _localStorage.DeleteAsync(AuthenticationToken.Entry);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using MadWorldNL.Clients.Admin.Domain;

namespace MadWorldNL.Clients.Admin.Application.Authentications;

public interface IAuthenticationManager
{
Task<AuthenticationToken> LoginFromSessionAsync();
Task<AuthenticationToken> LoginAsync(string username, string password);

Task LogoutAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void Authenticate(string jwtToken)
public void Logout()
{
_currentUserState = GetAnonymous();
NotifyAuthenticationStateChanged(Task.FromResult(_currentUserState));
}

private static AuthenticationState GetAnonymous()
Expand Down
21 changes: 13 additions & 8 deletions sources/Clients.Admin/Components/Pages/Authentications/Login.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
@page "/Login"
@using MadWorldNL.Clients.Admin.Services.Authentications
@using MadWorldNL.Clients.Admin.Application.Authentications

<p>@Message</p>
Expand All @@ -10,25 +9,31 @@

@code {
[Inject]
public IAuthenticationService AuthenticationService { get; set; } = null!;

[Inject]
public AuthenticationStateProvider AuthenticationStateProvider { get; set; } = null!;
public IAuthenticationManager AuthenticationManager { get; set; } = null!;

[Inject]
public NavigationManager NavigationManager { get; set; } = null!;

private string Message { get; set; } = string.Empty;
private void TryLogin()

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await AuthenticationManager.LoginFromSessionAsync();
StateHasChanged();
}
}

private async Task TryLogin()
{
var result = AuthenticationService.Login("email@emai.nl", "Password1234", "http://localhost:5000");
var result = await AuthenticationManager.LoginAsync("t", "t");
if (!result.IsSuccess)
{
Message = "Something went wrong";
return;
}

((JwtAuthenticationStateProvider)AuthenticationStateProvider).Authenticate(result.AccessToken);
NavigationManager.NavigateTo("/AuthenticationTest");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@page "/Logout"
@using MadWorldNL.Clients.Admin.Application.Authentications

<h3>You are logout now. Until next time!</h3>

@code {
[Inject]
public IAuthenticationManager AuthenticationManager { get; set; } = null!;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
await AuthenticationManager.LogoutAsync();
StateHasChanged();
}
}

}
2 changes: 2 additions & 0 deletions sources/Clients.Admin/Domain/AuthenticationToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ namespace MadWorldNL.Clients.Admin.Domain;

public class AuthenticationToken
{
public const string Entry = nameof(AuthenticationToken);

public bool IsSuccess { get; init; }
public string AccessToken { get; init; } = string.Empty;
public string RefreshToken { get; init; } = string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public static void AddAdminServices(this WebApplicationBuilder builder)
{
builder.Services.AddScoped<AuthenticationStateProvider, JwtAuthenticationStateProvider>();

builder.Services.AddScoped<IAuthenticationManager, AuthenticationManager>();

builder.Services.AddScoped<IAuthenticationService, AuthenticationService>();
}
}

0 comments on commit 0edef71

Please sign in to comment.