Skip to content

Commit 6abcbb8

Browse files
authored
Merge pull request #216 from pticostaricags/development
Fixing authorization issue for MAUI app
2 parents 17690ee + 8b37d41 commit 6abcbb8

File tree

4 files changed

+47
-39
lines changed

4 files changed

+47
-39
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,54 @@
11
using FairPlayTube.ClientServices.KiotaClient;
2+
using FairPlayTube.ClientServices.KiotaClient.Models;
3+
using FairPlayTube.MAUI.Helpers;
24
using Microsoft.AspNetCore.Components.Authorization;
35
using System.Security.Claims;
46

57
namespace FairPlayTube.MAUI.Authentication
68
{
79
public class CustomAuthenticationStateProvider(
8-
[FromKeyedServices("AuthenticatedApiClient")]
9-
ApiClient authenticatedClient
10-
10+
IServiceProvider serviceProvider,
11+
[FromKeyedServices("AnonymousApiClient")]
12+
ApiClient anonymousApiClient
1113
) : AuthenticationStateProvider
1214
{
13-
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
15+
private ClaimsPrincipal currentUser = new ClaimsPrincipal(new ClaimsIdentity());
16+
17+
public Task LoginAsync(LoginRequest loginRequest)
1418
{
15-
ClaimsIdentity identity = new();
16-
if (UserContext.IsAuthenticated) {
17-
var response = await authenticatedClient.Identity.GetMyRoles.GetAsync();
18-
foreach (var singleUserRole in response!)
19+
20+
var loginTask = LogInAsyncCore();
21+
NotifyAuthenticationStateChanged(loginTask);
22+
23+
return loginTask;
24+
25+
async Task<AuthenticationState> LogInAsyncCore()
26+
{
27+
var result = await anonymousApiClient!.Login.PostAsync(loginRequest);
28+
UserContext.AccessToken = result.AccessToken;
29+
UserContext.AccessTokenExpiresIn = result.ExpiresIn;
30+
UserContext.RefreshToken = result.RefreshToken;
31+
UserContext.TokenExpiraton = DateTimeOffset.UtcNow.AddMinutes(result!.ExpiresIn!.Value);
32+
using var scope = serviceProvider.CreateScope();
33+
var authenticatedClient = scope.ServiceProvider
34+
.GetRequiredKeyedService<ApiClient>("AuthenticatedApiClient");
35+
36+
ClaimsIdentity identity = new();
37+
if (UserContext.IsAuthenticated)
1938
{
20-
identity.AddClaim(new Claim(ClaimTypes.Role, singleUserRole));
39+
var response = await authenticatedClient.Identity.GetMyRoles.GetAsync();
40+
foreach (var singleUserRole in response!)
41+
{
42+
identity.AddClaim(new Claim(ClaimTypes.Role, singleUserRole));
43+
}
2144
}
45+
this.currentUser = new ClaimsPrincipal(identity);
46+
var authenticationState = new AuthenticationState(this.currentUser);
47+
return authenticationState;
2248
}
23-
var user = new ClaimsPrincipal(identity);
24-
var result = new AuthenticationState(user);
25-
return result;
2649
}
50+
51+
public override Task<AuthenticationState> GetAuthenticationStateAsync() =>
52+
Task.FromResult(new AuthenticationState(currentUser));
2753
}
2854
}

src/FairPlayCombinedSln/FairPlayTube.MAUI/Components/Pages/Login.razor

+4-17
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,16 @@
3030
private ApiClient? apiClient { get; set; }
3131
[Inject]
3232
private NavigationManager? navingationManager { get; set; }
33+
[Inject] AuthenticationStateProvider? authenticationStateProvider { get; set; }
3334
private readonly LoginRequest loginRequest = new();
3435

3536
private async Task OnValidSubmitAsync()
3637
{
3738
try
3839
{
39-
var result = await this.apiClient!.Login.PostAsync(this.loginRequest);
40-
if (!String.IsNullOrWhiteSpace(result!.AccessToken))
41-
{
42-
UserContext.AccessToken = result.AccessToken;
43-
UserContext.AccessTokenExpiresIn = result.ExpiresIn;
44-
UserContext.RefreshToken = result.RefreshToken;
45-
UserContext.TokenExpiraton = DateTimeOffset.UtcNow.AddMinutes(result!.ExpiresIn!.Value);
46-
ReloadService.IsReloading = true;
47-
StateHasChanged();
48-
this.navingationManager!.NavigateTo("/", false);
49-
ReloadService.IsReloading = false;
50-
StateHasChanged();
51-
}
52-
else
53-
{
54-
await Application.Current!.Windows[0].Page!.DisplayAlert("Error", "Unknown error", "OK");
55-
}
40+
var customAuthProvider = authenticationStateProvider as CustomAuthenticationStateProvider;
41+
await customAuthProvider!.LoginAsync(this.loginRequest);
42+
StateHasChanged();
5643
}
5744
catch (ApiException apiEx)
5845
{

src/FairPlayCombinedSln/FairPlayTube.MAUI/MauiProgram.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using FairPlayCombined.Common.Identity;
2-
using FairPlayCombined.Interfaces.FairPlayTube;
1+
using FairPlayCombined.Interfaces.FairPlayTube;
32
using FairPlayTube.ClientServices;
43
using FairPlayTube.ClientServices.CustomLocalization;
54
using FairPlayTube.ClientServices.KiotaClient;
@@ -8,7 +7,7 @@
87
using Microsoft.AspNetCore.Components;
98
using Microsoft.AspNetCore.Components.Authorization;
109
using Microsoft.AspNetCore.Components.Infrastructure;
11-
using Microsoft.AspNetCore.Identity;
10+
using Microsoft.Extensions.DependencyInjection.Extensions;
1211
using Microsoft.Extensions.Localization;
1312
using Microsoft.Extensions.Logging;
1413
using Microsoft.FluentUI.AspNetCore.Components;
@@ -46,9 +45,8 @@ public static MauiApp CreateMauiApp()
4645
builder.Services.AddSingleton<IStringLocalizer, ApiLocalizer>();
4746
builder.Services.AddLocalization();
4847
builder.Services.AddOptions();
48+
builder.Services.TryAddScoped<AuthenticationStateProvider, CustomAuthenticationStateProvider>();
4949
builder.Services.AddAuthorizationCore();
50-
builder.Services.AddCascadingAuthenticationState();
51-
builder.Services.AddScoped<AuthenticationStateProvider, CustomAuthenticationStateProvider>();
5250
builder.Services.AddScoped<LocalizationMessageHandler>();
5351
builder.Services.AddSingleton<IAccessTokenProvider, CustomAccessTokenAuthenticationProvider>();
5452
builder.Services.AddKeyedSingleton<ApiClient>("AnonymousApiClient",
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
@using FairPlayTube.MAUI.Helpers
22
<Router AppAssembly="@typeof(MauiProgram).Assembly" AdditionalAssemblies="FairPlayTube.MAUI.AdditionalSetup.AdditionalAssemblies">
33
<Found Context="routeData">
4-
@if (!ReloadService.IsReloading)
5-
{
6-
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(Components.Layout.MainLayout)" />
7-
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
8-
}
4+
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(Components.Layout.MainLayout)" />
5+
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
96
</Found>
107
</Router>

0 commit comments

Comments
 (0)