Skip to content

Commit

Permalink
Add support for pop-up based social sign-in (#9399)
Browse files Browse the repository at this point in the history
  • Loading branch information
ysmoradi committed Dec 5, 2024
1 parent 2c5ea07 commit 1fc420b
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public partial class ClientAppCoordinator : AppComponentBase
[AutoInject] private IPushNotificationService pushNotificationService = default!;
//#endif

private Action? unsubscribe;

protected override async Task OnInitAsync()
{
if (AppPlatform.IsBlazorHybrid)
Expand All @@ -45,6 +47,10 @@ protected override async Task OnInitAsync()

if (InPrerenderSession is false)
{
unsubscribe = PubSubService.Subscribe(ClientPubSubMessages.NAVIGATE_TO, async (uri) =>
{
NavigationManager.NavigateTo(uri!.ToString()!);
});
TelemetryContext.UserAgent = await navigator.GetUserAgent();
TelemetryContext.TimeZone = await jsRuntime.GetTimeZone();
TelemetryContext.Culture = CultureInfo.CurrentCulture.Name;
Expand Down Expand Up @@ -246,6 +252,8 @@ await storageService.GetItem("Culture") ?? // 2- User settings
private List<IDisposable> signalROnDisposables = [];
protected override async ValueTask DisposeAsync(bool disposing)
{
unsubscribe?.Invoke();

NavigationManager.LocationChanged -= NavigationManager_LocationChanged;
AuthManager.AuthenticationStateChanged -= AuthenticationStateChanged;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ public async Task ShowDiagnostic()
PubSubService.Publish(ClientPubSubMessages.SHOW_DIAGNOSTIC_MODAL);
}


/// <summary>
/// you can add any other method like this to utilize the bridge between js and .net code.
/// </summary>
[JSInvokable(nameof(PublishMessage))]
public async Task PublishMessage(string message, string? payload)
{
PubSubService.Publish(message, payload);
}

public void Dispose()
{
dotnetObj?.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,14 @@ public static IServiceCollection AddClientCoreProjectServices(this IServiceColle
services.AddTypedHttpClients();

//#if (signalR == true)
services.AddSingleton<SignalRInfinitiesRetryPolicy>();
services.AddSessioned(sp =>
{
var authManager = sp.GetRequiredService<AuthManager>();
var authTokenProvider = sp.GetRequiredService<IAuthTokenProvider>();
var absoluteServerAddressProvider = sp.GetRequiredService<AbsoluteServerAddressProvider>();

var hubConnection = new HubConnectionBuilder()
.WithAutomaticReconnect(sp.GetRequiredService<SignalRInfinitiesRetryPolicy>())
.WithStatefulReconnect()
.WithUrl(new Uri(absoluteServerAddressProvider.GetAddress(), "app-hub"), options =>
{
options.SkipNegotiation = false; // Required for Azure SignalR.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ class App {
public static registerJsBridge(dotnetObj: DotNetObject) {
// For additional details, see the JsBridge.cs file.
App.jsBridgeObj = dotnetObj;
window.addEventListener('message', e => {
// Enable publishing messages from JavaScript's `window.postMessage` to the C# `PubSubService`.
if (e.data.key === 'PUBLISH_MESSAGE') {
App.jsBridgeObj?.invokeMethodAsync('PublishMessage', e.data.message, e.data.payload);
}
});
}

public static showDiagnostic() {
Expand Down Expand Up @@ -70,7 +76,17 @@ interface DotNetObject {
dispose(): void;
}

window.addEventListener('load', setCssWindowSizes);
window.addEventListener('load', () => {
setCssWindowSizes();
if (window.opener != null) {
// The IExternalNavigationService is responsible for opening pages in a new window,
// such as during social sign-in flows. Once the external navigation is complete,
// and the user is redirected back to the newly opened window,
// the following code ensures that the original window is notified of where it should navigate next.
window.opener.postMessage({ key: 'PUBLISH_MESSAGE', message: 'NAVIGATE_TO', payload: window.location.href });
window.close();
}
});
window.addEventListener('resize', setCssWindowSizes);

function setCssWindowSizes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ public static partial class ClientPubSubMessages
public const string UPDATE_IDENTITY_HEADER_BACK_LINK = nameof(UPDATE_IDENTITY_HEADER_BACK_LINK);
public const string IDENTITY_HEADER_BACK_LINK_CLICKED = nameof(IDENTITY_HEADER_BACK_LINK_CLICKED);

/// <summary>
/// Supposed to be called using JavaScript to navigate between pages without reloading the app.
/// </summary>
public const string NAVIGATE_TO = nameof(NAVIGATE_TO);
public const string SHOW_DIAGNOSTIC_MODAL = nameof(SHOW_DIAGNOSTIC_MODAL);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@

public partial class DefaultExternalNavigationService : IExternalNavigationService
{
[AutoInject] private readonly Window window = default!;
[AutoInject] private readonly NavigationManager navigationManager = default!;

public async Task NavigateToAsync(string url)
{
navigationManager.NavigateTo(url, forceLoad: true, replace: true);
if (AppPlatform.IsBlazorHybrid)
{
navigationManager.NavigateTo(url, forceLoad: true, replace: true);
return;
}

if (await window.Open(url, "_blank", new WindowFeatures() { Popup = true, Height = 768, Width = 1024 }) is false // Let's try with popup first.
&& await window.Open(url, "_blank", new WindowFeatures() { Popup = false }) is false) // Let's try new tab
{
navigationManager.NavigateTo(url, forceLoad: true, replace: true); // If all else fails, let's try to navigate in the same tab.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

/// <summary>
/// Service for Publish/Subscribe pattern.
/// You could publish messages within blazor components, pages outside blazor components (Like MAUI Xaml pages), JavaScript
/// codes using window.postMessage or even from server side using SignalR (<see cref="SharedPubSubMessages.SESSION_REVOKED"/> as example.
/// </summary>
public partial class PubSubService
{
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PackageVersion Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="9.0.10" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebView.WindowsForms" Version="9.0.10" />
<PackageVersion Include="Microsoft.Web.WebView2" Version="1.0.2903.40" />
<PackageVersion Include="NWebsec.AspNetCore.Middleware" Version="3.0.0" />
<PackageVersion Include="Velopack" Version="0.0.942" />
<PackageVersion Include="Microsoft.Extensions.Logging.Configuration" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Debug" Version="9.0.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<PackageVersion Include="Microsoft.AspNetCore.Components.WebView.Maui" Version="8.0.100" />
<PackageVersion Include="Microsoft.AspNetCore.Components.WebView.WindowsForms" Version="8.0.100" />
<PackageVersion Include="Microsoft.Web.WebView2" Version="1.0.2903.40" />
<PackageVersion Include="NWebsec.AspNetCore.Middleware" Version="3.0.0" />
<PackageVersion Include="Velopack" Version="0.0.942" />
<PackageVersion Include="Microsoft.Extensions.Logging.Configuration" Version="8.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging.Debug" Version="8.0.1" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<PackageReference Condition=" '$(appInsights)' == 'true' OR '$(appInsights)' == '' " Include="Microsoft.ApplicationInsights.AspNetCore" />
<PackageReference Include="Humanizer" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" />
<PackageReference Include="NWebsec.AspNetCore.Middleware" />
<PackageReference Include="QRCoder" />
<PackageReference Include="Magick.NET-Q16-AnyCPU" />
<PackageReference Include="FluentEmail.Smtp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ private static void ConfigureMiddlewares(this WebApplication app)
{
app.UseHttpsRedirection();
app.UseResponseCompression();
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.

app.UseHsts();
app.UseXContentTypeOptions();
app.UseXXssProtection(options => options.EnabledWithBlockMode());
app.UseXfo(options => options.SameOrigin());
}

app.UseResponseCaching();
Expand Down Expand Up @@ -79,7 +82,7 @@ private static void ConfigureMiddlewares(this WebApplication app)
}).WithTags("Test");

//#if (signalR == true)
app.MapHub<SignalR.AppHub>("/app-hub");
app.MapHub<SignalR.AppHub>("/app-hub", options => options.AllowStatefulReconnects = true);
//#endif

app.MapControllers().RequireAuthorization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,19 @@ public static void ConfigureMiddlewares(this WebApplication app)

app.UseExceptionHandler("/", createScopeForErrors: true);

if (env.IsDevelopment())
if (env.IsDevelopment() && false)
{
app.UseWebAssemblyDebugging();
}
else
{
app.UseHttpsRedirection();
app.UseResponseCompression();
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.

app.UseHsts();
app.UseXContentTypeOptions();
app.UseXXssProtection(options => options.EnabledWithBlockMode());
app.UseXfo(options => options.SameOrigin());
}

app.UseResponseCaching();
Expand Down Expand Up @@ -140,7 +143,7 @@ public static void ConfigureMiddlewares(this WebApplication app)
// and use the Server.Web project solely as a Blazor Server or pre-rendering service provider.
throw new InvalidOperationException("Azure SignalR is not supported with Blazor Server and Auto");
}
app.MapHub<Api.SignalR.AppHub>("/app-hub");
app.MapHub<Api.SignalR.AppHub>("/app-hub", options => options.AllowStatefulReconnects = true);
//#endif

app.MapControllers().RequireAuthorization();
Expand Down

0 comments on commit 1fc420b

Please sign in to comment.