From 684ab909502a3a3126c0389318cba1357c672bf1 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Thu, 18 Apr 2024 15:36:43 +0800 Subject: [PATCH] Add authentication-state-listener.js --- .../AbpAspNetCoreMvcUiThemeSharedModule.cs | 8 ---- .../SharedThemeGlobalScriptContributor.cs | 3 +- .../AbpAuthenticationStateViewComponent.cs | 11 ----- .../AbpAuthenticationState/Default.cshtml | 48 ------------------- .../authentication-state-listener.js | 30 ++++++++++++ 5 files changed, 32 insertions(+), 68 deletions(-) delete mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Pages/Shared/Components/AbpAuthenticationState/AbpAuthenticationStateViewComponent.cs delete mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Pages/Shared/Components/AbpAuthenticationState/Default.cshtml create mode 100644 framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/authentication-state/authentication-state-listener.js diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/AbpAspNetCoreMvcUiThemeSharedModule.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/AbpAspNetCoreMvcUiThemeSharedModule.cs index 5f4ef0d192..b62d3f27ba 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/AbpAspNetCoreMvcUiThemeSharedModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/AbpAspNetCoreMvcUiThemeSharedModule.cs @@ -3,11 +3,8 @@ using Volo.Abp.AspNetCore.Mvc.UI.Bootstrap; using Volo.Abp.AspNetCore.Mvc.UI.Bundling; using Volo.Abp.AspNetCore.Mvc.UI.Packages; using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Bundling; -using Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Shared.Components.AbpAuthenticationState; -using Volo.Abp.AspNetCore.Mvc.UI.Theming; using Volo.Abp.AspNetCore.Mvc.UI.Widgets; using Volo.Abp.Modularity; -using Volo.Abp.Ui.LayoutHooks; using Volo.Abp.VirtualFileSystem; namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared; @@ -34,11 +31,6 @@ public class AbpAspNetCoreMvcUiThemeSharedModule : AbpModule options.FileSets.AddEmbedded("Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared"); }); - Configure(options => - { - options.Add(LayoutHooks.Body.Last, typeof(AbpAuthenticationStateViewComponent)); - }); - Configure(options => { options diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Bundling/SharedThemeGlobalScriptContributor.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Bundling/SharedThemeGlobalScriptContributor.cs index 27f3760000..5d8c15ba40 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Bundling/SharedThemeGlobalScriptContributor.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Bundling/SharedThemeGlobalScriptContributor.cs @@ -49,7 +49,8 @@ public class SharedThemeGlobalScriptContributor : BundleContributor "/libs/abp/aspnetcore-mvc-ui-theme-shared/datatables/datatables-extensions.js", "/libs/abp/aspnetcore-mvc-ui-theme-shared/sweetalert2/abp-sweetalert2.js", "/libs/abp/aspnetcore-mvc-ui-theme-shared/toastr/abp-toastr.js", - "/libs/abp/aspnetcore-mvc-ui-theme-shared/date-range-picker/date-range-picker-extensions.js" + "/libs/abp/aspnetcore-mvc-ui-theme-shared/date-range-picker/date-range-picker-extensions.js", + "/libs/abp/aspnetcore-mvc-ui-theme-shared/authentication-state/authentication-state-listener.js" }); } } diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Pages/Shared/Components/AbpAuthenticationState/AbpAuthenticationStateViewComponent.cs b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Pages/Shared/Components/AbpAuthenticationState/AbpAuthenticationStateViewComponent.cs deleted file mode 100644 index f48c63b6d5..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Pages/Shared/Components/AbpAuthenticationState/AbpAuthenticationStateViewComponent.cs +++ /dev/null @@ -1,11 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared.Pages.Shared.Components.AbpAuthenticationState; - -public class AbpAuthenticationStateViewComponent : AbpViewComponent -{ -public virtual IViewComponentResult Invoke() -{ - return View("~/Pages/Shared/Components/AbpAuthenticationState/Default.cshtml"); -} -} diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Pages/Shared/Components/AbpAuthenticationState/Default.cshtml b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Pages/Shared/Components/AbpAuthenticationState/Default.cshtml deleted file mode 100644 index f9112b1de7..0000000000 --- a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Pages/Shared/Components/AbpAuthenticationState/Default.cshtml +++ /dev/null @@ -1,48 +0,0 @@ - \ No newline at end of file diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/authentication-state/authentication-state-listener.js b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/authentication-state/authentication-state-listener.js new file mode 100644 index 0000000000..1b6ec69602 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/authentication-state/authentication-state-listener.js @@ -0,0 +1,30 @@ +(function () { + + const stateKey = 'authentication-state-id'; + + window.addEventListener('load', function () { + if (!abp || !abp.currentUser) { + return; + } + + if (!abp.currentUser.isAuthenticated){ + localStorage.removeItem(stateKey); + }else{ + localStorage.setItem(stateKey, abp.currentUser.id); + } + + window.addEventListener('storage', function (event) { + + if (event.key !== stateKey || event.oldValue === event.newValue) { + return; + } + + if(event.oldValue || !event.newValue) { + window.location.reload(); + }else{ + location.assign('/') + } + }); + }); + +}()); \ No newline at end of file