From 816335a11699eb29ea8cd9109c617396253d3c1d Mon Sep 17 00:00:00 2001 From: Yaser Moradi Date: Mon, 2 Dec 2024 19:09:29 +0100 Subject: [PATCH] fix(templates): Fix small issues in Boilerplate #9384 (#9385) --- .github/workflows/todo-sample.cd.yml | 8 ++++---- .../ExceptionDelegatingHandler.cs | 4 ---- .../Client/Boilerplate.Client.Maui/MauiProgram.cs | 11 +++++++---- .../wwwroot/service-worker.published.js | 5 ++++- .../Client/Boilerplate.Client.Windows/Program.cs | 13 ++++++++----- .../Controllers/Identity/IdentityController.cs | 14 +++++--------- .../Extensions/SignInManagerExtensions.cs | 2 +- .../Services/PushNotificationService.cs | 15 +++++++-------- 8 files changed, 36 insertions(+), 36 deletions(-) diff --git a/.github/workflows/todo-sample.cd.yml b/.github/workflows/todo-sample.cd.yml index ad5400a129..4a0cc17bb1 100644 --- a/.github/workflows/todo-sample.cd.yml +++ b/.github/workflows/todo-sample.cd.yml @@ -42,7 +42,7 @@ jobs: cd src/Templates/Boilerplate && dotnet build -c Release dotnet pack -c Release -o . -p:ReleaseVersion=0.0.0 -p:PackageVersion=0.0.0 dotnet new install Bit.Boilerplate.0.0.0.nupkg - cd ../../../ && dotnet new bit-bp --name TodoSample --database PostgreSQL --sample Todo --appInsights --sentry --serverUrl ${{ env.SERVER_ADDRESS }} --filesStorage AzureBlobStorage --notification --captcha reCaptcha --framework net8.0 + cd ../../../ && dotnet new bit-bp --name TodoSample --database PostgreSQL --sample Todo --appInsights --sentry --serverUrl ${{ env.SERVER_ADDRESS }} --filesStorage AzureBlobStorage --notification --captcha reCaptcha --signalR --framework net8.0 - name: Update core appsettings.json uses: devops-actions/variable-substitution@v1.2 @@ -146,7 +146,7 @@ jobs: cd src\Templates\Boilerplate && dotnet build -c Release dotnet pack -c Release -o . -p:ReleaseVersion=0.0.0 -p:PackageVersion=0.0.0 dotnet new install Bit.Boilerplate.0.0.0.nupkg - cd ..\..\..\ && dotnet new bit-bp --name TodoSample --database PostgreSQL --sample Todo --windows --appInsights --sentry --serverUrl ${{ env.SERVER_ADDRESS }} --filesStorage AzureBlobStorage --captcha reCaptcha --framework net8.0 + cd ..\..\..\ && dotnet new bit-bp --name TodoSample --database PostgreSQL --sample Todo --windows --appInsights --sentry --serverUrl ${{ env.SERVER_ADDRESS }} --filesStorage AzureBlobStorage --captcha reCaptcha --signalR --framework net8.0 - name: Update core appsettings.json uses: devops-actions/variable-substitution@v1.2 @@ -202,7 +202,7 @@ jobs: cd src/Templates/Boilerplate && dotnet build -c Release dotnet pack -c Release -o . -p:ReleaseVersion=0.0.0 -p:PackageVersion=0.0.0 dotnet new install Bit.Boilerplate.0.0.0.nupkg - cd ../../../ && dotnet new bit-bp --name TodoSample --database PostgreSQL --sample Todo --appInsights --sentry --serverUrl ${{ env.SERVER_ADDRESS }} --filesStorage AzureBlobStorage --notification --captcha reCaptcha --framework net8.0 + cd ../../../ && dotnet new bit-bp --name TodoSample --database PostgreSQL --sample Todo --appInsights --sentry --serverUrl ${{ env.SERVER_ADDRESS }} --filesStorage AzureBlobStorage --notification --captcha reCaptcha --signalR --framework net8.0 - name: Extract Android signing key from env uses: timheuer/base64-to-file@v1.2 @@ -299,7 +299,7 @@ jobs: cd src/Templates/Boilerplate && dotnet build -c Release dotnet pack -c Release -o . -p:ReleaseVersion=0.0.0 -p:PackageVersion=0.0.0 dotnet new install Bit.Boilerplate.0.0.0.nupkg - cd ../../../ && dotnet new bit-bp --name TodoSample --database PostgreSQL --sample Todo --appInsights --serverUrl ${{ env.SERVER_ADDRESS }} --filesStorage AzureBlobStorage --notification --captcha reCaptcha --framework net8.0 + cd ../../../ && dotnet new bit-bp --name TodoSample --database PostgreSQL --sample Todo --appInsights --serverUrl ${{ env.SERVER_ADDRESS }} --filesStorage AzureBlobStorage --notification --captcha reCaptcha --signalR --framework net8.0 - name: Update core appsettings.json uses: devops-actions/variable-substitution@v1.2 diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/HttpMessageHandlers/ExceptionDelegatingHandler.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/HttpMessageHandlers/ExceptionDelegatingHandler.cs index 2f372cd049..44c34d4cf9 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/HttpMessageHandlers/ExceptionDelegatingHandler.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/HttpMessageHandlers/ExceptionDelegatingHandler.cs @@ -66,12 +66,9 @@ response.IsSuccessStatusCode is false && || exp is TaskCanceledException tcExp && tcExp.InnerException is TimeoutException || exp is HttpRequestException { StatusCode: HttpStatusCode.BadGateway or HttpStatusCode.GatewayTimeout or HttpStatusCode.ServiceUnavailable }) { - //#if (signalR != true) serverCommunicationSuccess = false; // Let's treat the server communication as failed if an exception is caught here. - //#endif throw new ServerConnectionException(localizer[nameof(AppStrings.ServerConnectionException)], exp); } - //#if (signalR != true) finally { if (isInternalRequest) @@ -79,6 +76,5 @@ response.IsSuccessStatusCode is false && pubSubService.Publish(ClientPubSubMessages.IS_ONLINE_CHANGED, serverCommunicationSuccess); } } - //#endif } } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.cs index fca485351c..eac5b46638 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.cs @@ -20,10 +20,10 @@ public static partial class MauiProgram { public static MauiApp CreateMauiApp() { - AppDomain.CurrentDomain.UnhandledException += (_, e) => LogException(e.ExceptionObject); + AppDomain.CurrentDomain.UnhandledException += (_, e) => LogException(e.ExceptionObject, reportedBy: nameof(AppDomain.UnhandledException)); TaskScheduler.UnobservedTaskException += (_, e) => { - if (LogException(e.Exception)) + if (LogException(e.Exception, nameof(TaskScheduler.UnobservedTaskException))) { e.SetObserved(); } @@ -196,12 +196,15 @@ public override void DecidePolicy(WKWebView webView, WKNavigationAction navigati } #endif - private static bool LogException(object? error) + private static bool LogException(object? error, string reportedBy) { var errorMessage = error?.ToString() ?? "Unknown error"; if (IPlatformApplication.Current?.Services is IServiceProvider services && error is Exception exp) { - services.GetRequiredService().Handle(exp); + services.GetRequiredService().Handle(exp, parameters: new() + { + { nameof(reportedBy), reportedBy } + }); return true; } return false; diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/wwwroot/service-worker.published.js b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/wwwroot/service-worker.published.js index 4a5d2adf37..e433b8a7ff 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/wwwroot/service-worker.published.js +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/wwwroot/service-worker.published.js @@ -24,7 +24,10 @@ self.assetsInclude = []; self.assetsExclude = [ /bit\.blazorui\.fluent\.css$/, /bit\.blazorui\.fluent-dark\.css$/, - /bit\.blazorui\.fluent-light\.css$/ + /bit\.blazorui\.fluent-light\.css$/, + // If a PDF viewer is needed in the PWA, remove these two lines: + /pdfjs-4\.7\.76\.js$/, + /pdfjs-4\.7\.76-worker\.js$/ ]; self.externalAssets = [ { diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Program.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Program.cs index dd642c5893..b191aceadf 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Program.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Program.cs @@ -12,9 +12,9 @@ public partial class Program [STAThread] public static void Main(string[] args) { - Application.ThreadException += (_, e) => LogException(e.Exception); - AppDomain.CurrentDomain.UnhandledException += (_, e) => LogException(e.ExceptionObject); - TaskScheduler.UnobservedTaskException += (_, e) => { LogException(e.Exception); e.SetObserved(); }; + Application.ThreadException += (_, e) => LogException(e.Exception, reportedBy: nameof(Application.ThreadException)); + AppDomain.CurrentDomain.UnhandledException += (_, e) => LogException(e.ExceptionObject, reportedBy: nameof(AppDomain.UnhandledException)); + TaskScheduler.UnobservedTaskException += (_, e) => { LogException(e.Exception, reportedBy: nameof(TaskScheduler.UnobservedTaskException)); e.SetObserved(); }; ApplicationConfiguration.Initialize(); @@ -122,12 +122,15 @@ public static void Main(string[] args) Application.Run(form); } - private static void LogException(object? error) + private static void LogException(object? error, string reportedBy) { var errorMessage = error?.ToString() ?? "Unknown error"; if (Services is not null && error is Exception exp) { - Services.GetRequiredService().Handle(exp); + Services.GetRequiredService().Handle(exp, parameters: new() + { + { nameof(reportedBy), reportedBy } + }); } else { diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Controllers/Identity/IdentityController.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Controllers/Identity/IdentityController.cs index 256ece5cf9..0df4e38872 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Controllers/Identity/IdentityController.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Controllers/Identity/IdentityController.cs @@ -367,19 +367,15 @@ public async Task SendTwoFactorToken(SignInRequestDto request, CancellationToken sendMessagesTasks.Add(phoneService.SendSms(message, user.PhoneNumber!, cancellationToken)); } - //#if (signalR == true) - if (firstStepAuthenticationMethod != "SignalR") - { - sendMessagesTasks.Add(appHubContext.Clients.User(user.Id.ToString()).SendAsync(SignalREvents.SHOW_MESSAGE, message, cancellationToken)); - } - //#endif - - //#if (notification == true) if (firstStepAuthenticationMethod != "Push") { + //#if (signalR == true) + sendMessagesTasks.Add(appHubContext.Clients.User(user.Id.ToString()).SendAsync(SignalREvents.SHOW_MESSAGE, message, cancellationToken)); + //#endif + //#if (notification == true) sendMessagesTasks.Add(pushNotificationService.RequestPush(message: message, userRelatedPush: true, customSubscriptionFilter: s => s.UserSession!.UserId == user.Id, cancellationToken: cancellationToken)); + //#endif } - //#endif await Task.WhenAll(sendMessagesTasks); } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Extensions/SignInManagerExtensions.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Extensions/SignInManagerExtensions.cs index 20f6b832e9..bdfcc0a7d2 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Extensions/SignInManagerExtensions.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Extensions/SignInManagerExtensions.cs @@ -17,7 +17,7 @@ public static partial class SignInManagerExtensions /// 5. When the browser is redirected to a magic link created after a social sign-in, to automatically authenticate the user. /// 6. When the user opts to sign in using a 6-digit code delivered via native push notification, web push or SignalR message (if configured). /// - /// It's important to clarify the authentication method (e.g., Social, SMS, Email, or Push Notification) + /// It's important to clarify the authentication method (e.g., Social, SMS, Email, or Push) /// to avoid sending a second step to the same communication channel: For successful two-step authentication, the user must use a different method for the second step. /// diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/PushNotificationService.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/PushNotificationService.cs index 33469d0166..211d9a102b 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/PushNotificationService.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/PushNotificationService.cs @@ -21,16 +21,15 @@ public async Task Subscribe([Required] PushNotificationSubscriptionDto dto, Canc var userSessionId = httpContextAccessor.HttpContext!.User.IsAuthenticated() ? httpContextAccessor.HttpContext.User.GetSessionId() : (Guid?)null; - var subscription = await dbContext.PushNotificationSubscriptions.FindAsync([dto.DeviceId], cancellationToken); + await dbContext.PushNotificationSubscriptions + .Where(s => s.DeviceId == dto.DeviceId || s.UserSessionId == userSessionId /* pushManager's subscription has been renewed. */) + .ExecuteDeleteAsync(cancellationToken); - if (subscription is null) + var subscription = dbContext.PushNotificationSubscriptions.Add(new() { - dbContext.PushNotificationSubscriptions.Add(subscription = new() - { - DeviceId = dto.DeviceId, - Platform = dto.Platform - }); - } + DeviceId = dto.DeviceId, + Platform = dto.Platform + }).Entity; dto.Patch(subscription);