Skip to content

Commit

Permalink
Continue with login
Browse files Browse the repository at this point in the history
  • Loading branch information
oveldman committed Mar 22, 2024
1 parent 8e46a4f commit df532fe
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,31 @@ namespace MadWorldNL.Clients.Admin.Application.Authentications;
public class JwtAuthenticationStateProvider : AuthenticationStateProvider
{
private const string AuthenticationType = "jwt";

private AuthenticationState _currentUserState = GetAnonymous();

public override Task<AuthenticationState> GetAuthenticationStateAsync()
{
throw new NotImplementedException();
return Task.FromResult(_currentUserState);
}

public void Authenticate(string jwtToken)
{
var securityToken = new JwtSecurityToken(jwtToken);
var identity = new ClaimsIdentity(securityToken.Claims, AuthenticationType);
var user = new ClaimsPrincipal(identity);
var state = new AuthenticationState(user);
NotifyAuthenticationStateChanged(Task.FromResult(state));
_currentUserState = new AuthenticationState(user);
NotifyAuthenticationStateChanged(Task.FromResult(_currentUserState));
}

public void Logout()
{
_currentUserState = GetAnonymous();
}

private static AuthenticationState GetAnonymous()
{
var user = new ClaimsPrincipal();
return new AuthenticationState(user);
}
}
1 change: 1 addition & 0 deletions sources/Clients.Admin/Clients.Admin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="Grpc.Net.ClientFactory" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Radzen.Blazor" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
</ItemGroup>

Expand Down
6 changes: 4 additions & 2 deletions sources/Clients.Admin/Components/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
<link rel="stylesheet" href="app.css"/>
<link rel="stylesheet" href="MadWorldNL.Clients.Admin.styles.css"/>
<link rel="icon" type="image/png" href="favicon.png"/>
<link rel="stylesheet" href="_content/Radzen.Blazor/css/material-base.css">
<HeadOutlet/>
</head>

<body>
<Routes/>
<script src="_framework/blazor.web.js"></script>
<Routes @rendermode="InteractiveServer"/>
<script src="_framework/blazor.web.js"></script>
<script src="_content/Radzen.Blazor/Radzen.Blazor.js?v=@(typeof(Radzen.Colors).Assembly.GetName().Version)"></script>
</body>

</html>
15 changes: 14 additions & 1 deletion sources/Clients.Admin/Components/Layout/MainLayout.razor
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@

<main>
<div class="top-row px-4">
<a href="https://learn.microsoft.com/aspnet/core/" target="_blank">About</a>
<AuthorizeView>
<Authorized>
<NavLink class="nav-link" href="Logout">
Logout
</NavLink>
</Authorized>
<NotAuthorized>
<NavLink class="nav-link" href="Login">
Login
</NavLink>
</NotAuthorized>
</AuthorizeView>
</div>

<article class="content px-4">
@Body
</article>

<RadzenComponents/>
</main>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@using Microsoft.AspNetCore.Authorization

@page "/AuthenticationTest"
@attribute [Authorize]

Expand Down
18 changes: 14 additions & 4 deletions sources/Clients.Admin/Components/Pages/Authentications/Login.razor
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
@page "/Login"
@using MadWorldNL.Clients.Admin.Services.Authentications
@using Microsoft.AspNetCore.Components.Authorization
@using MadWorldNL.Clients.Admin.Application.Authentications

<p>@Message</p>

<h3>Login</h3>

<RadzenButton Click="@TryLogin" Text="Try to Login" />

@code {
[Inject]
public IAuthenticationService AuthenticationService { get; set; } = null!;
Expand All @@ -13,10 +17,16 @@

[Inject]
public NavigationManager NavigationManager { get; set; } = null!;
public void TryLogin()

private string Message { get; set; } = string.Empty;
private void TryLogin()
{
var result = AuthenticationService.Login("[email protected]", "password", "http://localhost:5000");
if (!result.IsSuccess) return;
var result = AuthenticationService.Login("[email protected]", "Password1234", "http://localhost:5000");
if (!result.IsSuccess)
{
Message = "Something went wrong";
return;
}

((JwtAuthenticationStateProvider)AuthenticationStateProvider).Authenticate(result.AccessToken);
NavigationManager.NavigateTo("/AuthenticationTest");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@namespace MadWorldNL.Clients.Admin.Components.Pages.Authentications
@inject NavigationManager Navigation

@code {
protected override void OnInitialized()
{
Navigation.NavigateTo("/Login");
}
}
31 changes: 25 additions & 6 deletions sources/Clients.Admin/Components/Routes.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="routeData" DefaultLayout="typeof(Layout.MainLayout)"/>
<FocusOnNavigate RouteData="routeData" Selector="h1"/>
</Found>
</Router>
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(Layout.MainLayout)">
<NotAuthorized>
@if (context.User.Identity?.IsAuthenticated != true)
{
<RedirectToLogin />
}
else
{
<p role="alert">You are not authorized to access this resource.</p>
}
</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="@routeData" Selector="h1"/>
</Found>
<NotFound>
<PageTitle>Not found</PageTitle>
<LayoutView Layout="@typeof(Layout.MainLayout)">
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
9 changes: 7 additions & 2 deletions sources/Clients.Admin/Components/_Imports.razor
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.AspNetCore.Components.Web.Virtualization
@using static Microsoft.AspNetCore.Components.Web.RenderMode
@using Microsoft.JSInterop
@using Radzen
@using Radzen.Blazor

@using MadWorldNL.Clients.Admin
@using MadWorldNL.Clients.Admin.Components
@using MadWorldNL.Clients.Admin.Components
@using MadWorldNL.Clients.Admin.Components.Pages.Authentications
3 changes: 3 additions & 0 deletions sources/Clients.Admin/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using MadWorldNL.Clients.Admin.Extensions;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Radzen;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -33,6 +34,8 @@
builder.AddGrpcClients();
builder.AddAdminServices();

builder.Services.AddRadzenComponents();

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand Down
2 changes: 1 addition & 1 deletion sources/Clients.Admin/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"Identity": {
"Host": "grpc://localhost:7091"
"Host": "https://localhost:7091"
},
"JwtSettings": {
"Key": "mSWX4ctFHyPAPYddRzgVETAUEj3oJE2cNCPfhbyW9K5M4rXYjR"
Expand Down
1 change: 1 addition & 0 deletions sources/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageVersion Include="Microsoft.IdentityModel.Tokens" Version="7.4.0" />
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
<PackageVersion Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0" />
<PackageVersion Include="Radzen.Blazor" Version="4.28.5" />
<PackageVersion Include="Respawn" Version="6.2.1" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="7.4.0" />
Expand Down

0 comments on commit df532fe

Please sign in to comment.