-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
106 additions
and
8 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
sources/Clients.Admin/Application/Authentications/AuthenticationManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
sources/Clients.Admin/Application/Authentications/IAuthenticationManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
sources/Clients.Admin/Components/Pages/Authentications/Logout.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters