Skip to content

Commit

Permalink
Start with the login
Browse files Browse the repository at this point in the history
  • Loading branch information
oveldman committed Apr 6, 2024
1 parent 53a0f22 commit 15912aa
Show file tree
Hide file tree
Showing 19 changed files with 187 additions and 17 deletions.
14 changes: 14 additions & 0 deletions sources/Clients.Admin.BFF/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@

builder.AddIdentity();

const string madWorldOrigins = "_myAllowSpecificOrigins";
builder.Services.AddCors(options =>
{
options.AddPolicy(name: madWorldOrigins,
policy =>
{
policy.WithOrigins(builder.Configuration.GetSection("Cors:AllowedOrigins").Get<string[]>()!);
policy.AllowAnyMethod();
policy.AllowAnyHeader();
});
});

var app = builder.Build();

// Configure the HTTP request pipeline.
Expand All @@ -22,4 +34,6 @@
app.UseHttpsRedirection();
app.AddIdentityEndpoints();

app.UseCors(madWorldOrigins);

app.Run();
5 changes: 5 additions & 0 deletions sources/Clients.Admin.BFF/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"Audience": "https://admin.mad-world.nl",
"Host": " http://localhost:5266"
},
"Cors": {
"AllowedOrigins": [
"https://localhost:7222"
]
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
5 changes: 5 additions & 0 deletions sources/Clients.Admin.BFF/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
"Audience": "https://admin.mad-world.nl",
"Host": " http://localhost:5266"
},
"Cors": {
"AllowedOrigins": [
"https://localhost:7222"
]
},
"Logging": {
"LogLevel": {
"Default": "Information",
Expand Down
18 changes: 16 additions & 2 deletions sources/Clients.Admin.Web/App.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
@using MadWorldNL.Clients.Identity.Blazor.Shared

<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly" AdditionalAssemblies="new []{ typeof(IIdentityMarker).Assembly }" >
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(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>
Expand All @@ -10,4 +23,5 @@
<p role="alert">Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</Router>
</CascadingAuthenticationState>
3 changes: 2 additions & 1 deletion sources/Clients.Admin.Web/_Imports.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@using System.Net.Http
@using System.Net.Http.Json
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Forms
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Web
Expand All @@ -12,4 +13,4 @@

@using Clients.Admin.Web
@using Clients.Admin.Web.Layout

@using MadWorldNL.Clients.Identity.Blazor.Shared.Pages
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using MadWorldNL.Clients.Identity.Api.Contracts.Authentications;

namespace MadWorldNL.Clients.Identity.Blazor.Shared.Authentications;

public class AuthenticationManager : IAuthenticationManager
{
private readonly IAuthenticationService _authenticationService;

public AuthenticationManager(IAuthenticationService authenticationService)
{
_authenticationService = authenticationService;
}

public async Task<LoginProxyResponse> LoginAsync(LoginProxyRequest request)
{
return await _authenticationService.LoginAsync(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public AuthenticationService(IHttpClientFactory clientFactory)

public async Task<LoginProxyResponse> LoginAsync(LoginProxyRequest request)
{
var response = await _client.PostAsJsonAsync(Endpoint, request);
var response = await _client.PostAsJsonAsync($"{Endpoint}/Login", request);
return await response.Content.ReadFromJsonAsync<LoginProxyResponse>() ?? new LoginProxyResponse();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using MadWorldNL.Clients.Identity.Api.Contracts.Authentications;

namespace MadWorldNL.Clients.Identity.Blazor.Shared.Authentications;

public interface IAuthenticationManager
{
Task<LoginProxyResponse> LoginAsync(LoginProxyRequest request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Security.Claims;
using Microsoft.AspNetCore.Components.Authorization;

namespace MadWorldNL.Clients.Identity.Blazor.Shared.Authentications;

public class MyAuthenticationStateProvider : AuthenticationStateProvider
{
public override Task<AuthenticationState> GetAuthenticationStateAsync()
{
var state = new AuthenticationState(new ClaimsPrincipal());

NotifyAuthenticationStateChanged(Task.FromResult(state));
return Task.FromResult(state);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.Web" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" />
<PackageReference Include="Microsoft.Extensions.Http" />
<PackageReference Include="Radzen.Blazor" />
</ItemGroup>
Expand All @@ -24,4 +25,9 @@
<PackageVersion Update="Microsoft.AspNetCore.Components.Web" Version="8.0.3" />
</ItemGroup>

<ItemGroup>
<UpToDateCheckInput Remove="RadzenExtra\ErrorMessage.razor" />
<UpToDateCheckInput Remove="RadzenExtra\TestTest.razor" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using MadWorldNL.Clients.Identity.Blazor.Shared.Authentications;
using MadWorldNL.Clients.Identity.Blazor.Shared.Settings;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
Expand All @@ -10,9 +11,14 @@ public static class WebAssemblyHostBuilderExtensions
{
public static void AddIdentity(this WebAssemblyHostBuilder builder)
{
builder.Services.AddCascadingAuthenticationState();
builder.Services.AddScoped<AuthenticationStateProvider, MyAuthenticationStateProvider>();
builder.Services.AddAuthorizationCore();

builder.Services.AddOptions<IdentitySettings>()
.Bind(builder.Configuration.GetSection(IdentitySettings.Entry));

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

builder.AddIdentityClients();
Expand Down
29 changes: 29 additions & 0 deletions sources/Clients.Identity.Blazor.Shared/Pages/Login.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
@page "/Login"

<ErrorMessage Show="@_showError" Message="There was an error logging in. " />

@TempLoginMessage

<AuthorizeView>
<Authorized>
<h1>You are logged in</h1>
</Authorized>
<NotAuthorized>
<h1>Not logged in</h1>
<RadzenRow Gap="2rem" Class="rz-p-0 rz-p-lg-4">
<RadzenRow AlignItems="AlignItems.Center">
<RadzenFormField Text="Email" Variant="Variant.Filled" Style="width: 100%;">
<RadzenTextBox @bind-Value="@_loginRequest.Email" Style="width: 100%;" />
</RadzenFormField>
</RadzenRow>
<RadzenRow AlignItems="AlignItems.Center">
<RadzenFormField Text="Password" Variant="Variant.Filled" Style="width: 100%;">
<RadzenPassword @bind-Value="@_loginRequest.Password" aria-label="enter password" />
</RadzenFormField>
</RadzenRow>
<RadzenRow AlignItems="AlignItems.Center">
<RadzenButton Shade="Shade.Lighter" Click=@LoginAsync Text="Login" ButtonStyle="ButtonStyle.Primary"/>
</RadzenRow>
</RadzenRow>
</NotAuthorized>
</AuthorizeView>
31 changes: 31 additions & 0 deletions sources/Clients.Identity.Blazor.Shared/Pages/Login.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using MadWorldNL.Clients.Identity.Api.Contracts.Authentications;
using MadWorldNL.Clients.Identity.Blazor.Shared.Authentications;
using Microsoft.AspNetCore.Components;

namespace MadWorldNL.Clients.Identity.Blazor.Shared.Pages;

public partial class Login
{
[Inject] public IAuthenticationManager AuthenticationManager { get; set; } = null!;

private LoginProxyRequest _loginRequest = new();
private bool _showError = false;

private string TempLoginMessage = string.Empty;

public async Task LoginAsync()
{
var response = await AuthenticationManager.LoginAsync(_loginRequest);

if (!response.IsSuccess)
{
_showError = true;

return;
}

_showError = false;

TempLoginMessage = response.Expiration.ToString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@inject NavigationManager Navigation

@code {
protected override void OnInitialized()
{
Navigation.NavigateToLogin("/Login");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@if (Show)
{
<RadzenAlert AlertStyle="AlertStyle.Danger" Variant="Variant.Flat" Shade="Shade.Lighter">
@Message
</RadzenAlert>
}

@code {
[Parameter]
public bool Show { get; set; }

[Parameter]
public string Message { get; set; } = string.Empty;
}
5 changes: 4 additions & 1 deletion sources/Clients.Identity.Blazor.Shared/_Imports.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
@using Microsoft.AspNetCore.Components.Web
@using Microsoft.AspNetCore.Components.Authorization
@using Microsoft.AspNetCore.Components.Web

@using MadWorldNL.Clients.Identity.Blazor.Shared.RadzenExtra

@using Radzen
@using Radzen.Blazor
3 changes: 0 additions & 3 deletions sources/Clients.Identity.Blazor.Shared/pages/Login.razor

This file was deleted.

6 changes: 0 additions & 6 deletions sources/Clients.Identity.Blazor.Shared/pages/Login.razor.cs

This file was deleted.

7 changes: 4 additions & 3 deletions sources/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageVersion Include="MadWorldNL.Common.AspNetCore" Version="1.0.3" />
<PackageVersion Include="MadWorldNL.Common.Grpc" Version="1.0.3" />
<PackageVersion Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.3" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebAssembly.Authentication" Version="8.0.3" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.3" />
<PackageVersion Include="Microsoft.AspNetCore.Identity.UI" Version="8.0.3" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.3" />
Expand All @@ -25,14 +26,14 @@
<PackageVersion Include="Microsoft.Extensions.Http" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="8.0.2" />
<PackageVersion Include="Microsoft.IdentityModel.Tokens" Version="7.5.0" />
<PackageVersion Include="Microsoft.IdentityModel.Tokens" Version="7.5.1" />
<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.29.1" />
<PackageVersion Include="Radzen.Blazor" Version="4.29.2" />
<PackageVersion Include="Respawn" Version="6.2.1" />
<PackageVersion Include="Shouldly" Version="4.2.1" />
<PackageVersion Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="7.5.0" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="7.5.1" />
<PackageVersion Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit 15912aa

Please sign in to comment.