Browse Source

Add authentication-state-listener.js

pull/19569/head
liangshiwei 2 years ago
parent
commit
684ab90950
  1. 8
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/AbpAspNetCoreMvcUiThemeSharedModule.cs
  2. 3
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Bundling/SharedThemeGlobalScriptContributor.cs
  3. 11
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Pages/Shared/Components/AbpAuthenticationState/AbpAuthenticationStateViewComponent.cs
  4. 48
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Pages/Shared/Components/AbpAuthenticationState/Default.cshtml
  5. 30
      framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/wwwroot/libs/abp/aspnetcore-mvc-ui-theme-shared/authentication-state/authentication-state-listener.js

8
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<AbpAspNetCoreMvcUiThemeSharedModule>("Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared");
});
Configure<AbpLayoutHookOptions>(options =>
{
options.Add(LayoutHooks.Body.Last, typeof(AbpAuthenticationStateViewComponent));
});
Configure<AbpBundlingOptions>(options =>
{
options

3
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"
});
}
}

11
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Pages/Shared/Components/AbpAuthenticationState/AbpAuthenticationStateViewComponent.cs

@ -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");
}
}

48
framework/src/Volo.Abp.AspNetCore.Mvc.UI.Theme.Shared/Pages/Shared/Components/AbpAuthenticationState/Default.cshtml

@ -1,48 +0,0 @@
<script type="text/javascript">
(function () {
const stateKey = 'authentication-state-id';
if (!abp || !abp.currentUser){
return;
}
abp.authenticationStateValidator = {
init: function () {
this.checkAuthenticationState();
this.setAuthenticationState()
},
setAuthenticationState: function () {
if (!abp.currentUser.isAuthenticated){
localStorage.removeItem(stateKey);
return;
}
localStorage.setItem(stateKey, abp.currentUser.id);
},
checkAuthenticationState: function () {
window.addEventListener('storage', function (event) {
if (event.key !== stateKey) {
return;
}
var previousState = event.oldValue
var state = event.newValue;
if(previousState === state) {
return;
}
if(previousState || !state) {
window.location.reload();
return;
}
location.assign('/')
});
}
};
abp.authenticationStateValidator.init();
})();
</script>

30
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('/')
}
});
});
}());
Loading…
Cancel
Save