简体   繁体   English

Spartacus SSR 模式:在组件上应用 AuthGuard 后,虽然用户已经登录,但在刷新时会暂时显示登录屏幕

[英]Spartacus SSR mode :After Applying AuthGuard on Component , login screen is displayed momentarily on refresh though user is already logged in

I am applying Spartacus AuthGuard for some component so that only logged in user can access them, if not then it will redirect Guest to the Login page.我正在为某些组件应用 Spartacus AuthGuard ,这样只有登录的用户才能访问它们,如果没有,它会将访客重定向到登录页面。 It is working fine without SSR mode but when I serve the application using SSR mode then on Refresh, for the logged-in user it redirects to the Login page for a fraction of a second before redirecting to the requisite page.它在没有 SSR 模式的情况下工作正常,但是当我使用 SSR 模式为应用程序提供服务时,然后在刷新时,对于已登录的用户,它会在重定向到所需页面之前重定向到登录页面几分之一秒。

This problem persists even on Disable SSR mode for the component having AuthGuard.即使在为具有 AuthGuard 的组件禁用 SSR模式下,此问题仍然存在。 If I remove the AuthGuard from the component then this problem does not happen.如果我从组件中删除 AuthGuard,则不会发生此问题。

AuthGuard Code: AuthGuard 代码:

  canActivate(): Observable<boolean | UrlTree> {
    return this.authService.isUserLoggedIn().pipe(
      map((isLoggedIn) => {
        if (!isLoggedIn) {
          this.authRedirectrvice.reportAuthGuard();
          return this.router.parseUrl(this.semanticPathService.get('login'));
        }
        return isLoggedIn;
      })
    );
  }

I believe it is happening due to server-side rendering as it is never redirected from the browser side(I ensured it by debugging).我相信这是由于服务器端渲染而发生的,因为它永远不会从浏览器端重定向(我通过调试确保了它)。 So is there any solution to avoid redirection from the server-side for a logged user?那么有什么解决方案可以避免登录用户从服务器端重定向?

Thanks谢谢

The UI flickering is unavoidable, becasue SSR doesn't know about the user context, by design. UI 闪烁是不可避免的,因为 SSR 在设计上不了解用户上下文。 So in SSR you can't get the render of the logged in user.所以在 SSR 中你不能得到登录用户的渲染。 Only in CSR it's possible.只有在企业社会责任中才有可能。

But here described the workaround, how to avoid displaying the Login Page UI for a fraction of time, but some custom, different UI instead: https://github.com/SAP/spartacus/issues/12537但这里描述了解决方法,如何避免在一小部分时间内显示登录页面 UI,而是一些自定义的不同 UI: https://github.com/SAP/spartacus/issues/12537

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果用户已经登录,则隐藏“ FB登录”按钮 - Hide the FB Login button if user already logged in 仅当用户未登录时才显示登录屏幕 - Show login screen only if user is not logged in Laravel Ajax请求,Auth:login返回false! 但刷新后用户已登录 - Laravel Ajax request, Auth:login returns false! but user is logged in after refresh 显示登录屏幕如果用户未登录| jQuery的 - Show login screen If the user is not logged in | jQuery 即使在更改源之后,旧图像也会暂时显示 - old image is getting displayed momentarily even after changing the source Angular 2/4:如果用户已经登录,如何限制对登录路由的访问? - Angular 2/4: How to restrict access to Login Route if user is already logged in? 如果已经使用 Reactjs 登录,如何限制用户显示登录页面 - How to restrict user to display login page if already logged in using Reactjs 即使已经在另一个选项卡中登录,“新建”选项卡也会打开登录页面 - New tab opens login page even though already logged in another tab Firebase用户已登录 - Firebase User Already Logged In 用户登录后如何刷新Facebook窗口 - How to refresh Facebook window after user login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM