Angular 如何在刷新时在AuthenticationGuard中获取用户和购物车数据?

Angular 如何在刷新时在AuthenticationGuard中获取用户和购物车数据?,angular,authentication,checkout,spartacus-storefront,angular-router-guards,Angular,Authentication,Checkout,Spartacus Storefront,Angular Router Guards,我对spartacus店面上的CheckoutAuthGuard有问题。当在结账过程中的任何页面上按下浏览器刷新按钮时,guard会将我重定向到登录,因为缺少用户和购物车信息 我使用的CustomCheckoutAuthGuard如下: import { Injectable } from '@angular/core'; import {CanActivate, Router} from '@angular/router'; import { ActiveCartService, Au

我对spartacus店面上的
CheckoutAuthGuard
有问题。当在结账过程中的任何页面上按下浏览器刷新按钮时,guard会将我重定向到登录,因为缺少用户和购物车信息

我使用的
CustomCheckoutAuthGuard
如下:

import { Injectable } from '@angular/core';
import {CanActivate, Router} from '@angular/router';
import {
  ActiveCartService,
  AuthRedirectService,
  AuthService,
  RoutingService,
  User,
  UserToken,
} from '@spartacus/core';
import { combineLatest, Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import {CheckoutConfigService} from '@spartacus/storefront';

@Injectable()
export class CustomCheckoutAuthGuard implements CanActivate {
  constructor(
    protected routingService: RoutingService,
    protected authService: AuthService,
    protected authRedirectService: AuthRedirectService,
    protected checkoutConfigService: CheckoutConfigService,
    protected activeCartService: ActiveCartService,
    private router: Router
  ) {}

  canActivate(): Observable<boolean> {
    return combineLatest([
      this.authService.getUserToken(),
      this.activeCartService.getAssignedUser(),
    ]).pipe(
      map(([token, user]: [UserToken, User]) => {
        if (!token.access_token) {
          if (this.activeCartService.isGuestCart()) {
            return Boolean(user);
          }
          if (this.checkoutConfigService.isGuestCheckout()) {
            this.router.navigate(['checkout-login']);
          } else {
            this.routingService.go({ cxRoute: 'login' });
          }
          this.authRedirectService.reportAuthGuard();
        }
        return !!token.access_token;
      })
    );
  }
}
但在刷新时,
ActiveCartService
中的用户是
undefined
,并且
this.ActiveCartService.isGuestCart()
返回
undefined

这是所有的客人结帐,不幸的是,我不能告诉什么是登录用户的行为。这是因为我们使用CDC,目前CDC存在一个错误,任何刷新都会重定向到主页,所以完全绕过了这个防护。更多关于这个问题


有没有人能给我一些建议,在这里什么是最好的尝试?可能某些购物车状态需要与本地存储同步,或者
combinelatetest
这里的
rxjs
功能错误?还是别的什么?提前谢谢

这是一个已知问题,在Spartacus 3.0版中已修复。您可以看到我们打开的问题。目前还没有计划将这一更改移植到3.0之前的版本

该问题是由于在装载购物车之前运行了警卫检查

如果您在3.0之前需要此解决方案,我建议您使用它的自定义版本覆盖开箱即用的
CheckoutAuthGuard
。您应该将
this.activeCartService.isStable()
值添加到
combinelatetest
流中,并在“购物车稳定”上添加
filter
。你明白我的意思了

以下是您应如何提供自定义防护,以替换默认防护:

(在app.module.ts中或在AppModule中导入的其他模块中)

    CheckoutOrchestrator: {
      component: CheckoutOrchestratorComponent,
      guards: [CustomCheckoutAuthGuard, CartNotEmptyGuard, CheckoutGuard],
    },
    CheckoutProgressMobileTop: {
      component: CheckoutProgressMobileTopComponent,
      guards: [CustomCheckoutAuthGuard, CartNotEmptyGuard],
    },
    CheckoutProgressMobileBottomComponent: {
      component: CheckoutProgressMobileBottomComponent,
      guards: [CustomCheckoutAuthGuard, CartNotEmptyGuard]
    },
    CheckoutProgress: {
      component: CheckoutProgressComponent,
      guards: [CustomCheckoutAuthGuard, CartNotEmptyGuard]
    },
    CheckoutDeliveryMode: {
      component: DeliveryModeComponent,
      guards: [CustomCheckoutAuthGuard, CartNotEmptyGuard, ShippingAddressSetGuard]
    },
    CheckoutPlaceOrder: {
      component: PlaceOrderComponent,
      guards: [CustomCheckoutAuthGuard, CartNotEmptyGuard]
    },
    CheckoutReviewOrder: {
      component: ReviewSubmitComponent,
      guards: [CustomCheckoutAuthGuard, CartNotEmptyGuard]
    },
    CheckoutShippingAddress: {
      component: ShippingAddressComponent,
      guards: [CustomCheckoutAuthGuard, CartNotEmptyGuard, CheckoutDetailsLoadedGuard]
    }
providers: [
     ...,
     {
      provide: CheckoutAuthGuard,
      useClass: CustomCheckoutAuthGuard, //or whatever your guard class is named
    }
]