刷新令牌功能

时间:2020-02-26 05:50:00

标签: spartacus-storefront

如何使刷新令牌功能正常工作? 我在本地存储中看不到刷新令牌,这是因为core在storemodule config中没有它

export function authStoreConfigFactory(): StateConfig {
  // if we want to reuse AUTH_FEATURE const in config, we have to use factory instead of plain object
  const config: StateConfig = {
    state: {
      storageSync: {
        keys: {
          'auth.userToken.token.access_token': StorageSyncType.LOCAL_STORAGE,
          'auth.userToken.token.token_type': StorageSyncType.LOCAL_STORAGE,
          'auth.userToken.token.expires_in': StorageSyncType.LOCAL_STORAGE,
          'auth.userToken.token.expiration_time': StorageSyncType.LOCAL_STORAGE,
          'auth.userToken.token.scope': StorageSyncType.LOCAL_STORAGE,
          'auth.userToken.token.userId': StorageSyncType.LOCAL_STORAGE,
        },
      },
    },
  };
  return config;
}

正确的使用方式是什么?

1 个答案:

答案 0 :(得分:1)

出于安全原因,默认存储同步配置中故意省略了刷新令牌。

但是,如果需要,您始终可以像下面的示例一样提供自己的存储同步配置。它不会覆盖默认配置,但会与其结合使用。要从默认配置中排除某些内容,可以与excludeKeys配置中的keys属性类似地使用storageSync

import { NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { translationChunksConfig, translations } from "@spartacus/assets";
import { ConfigModule, StateConfig, StorageSyncType } from "@spartacus/core";
import { B2cStorefrontModule } from "@spartacus/storefront";
import { AppRoutingModule } from "./app-routing.module";
import { AppComponent } from "./app.component";

export function refreshTokenConfigFactory(): StateConfig {
  const config: StateConfig = {
    state: {
      storageSync: {
        keys: {
          "auth.userToken.token.refresh_token": StorageSyncType.LOCAL_STORAGE
        }
      }
    }
  };
  return config;
}

@NgModule({
  declarations: [AppComponent],
  imports: [
    BrowserModule,
    AppRoutingModule,
    B2cStorefrontModule.withConfig({
      backend: {
        occ: {
          baseUrl: "http://localhost:9002",
          prefix: "/rest/v2/"
        }
      },
      context: {
        baseSite: ["electronics-spa"]
      },
      i18n: {
        resources: translations,
        chunks: translationChunksConfig,
        fallbackLang: "en"
      },
      features: {
        level: "1.5",
        anonymousConsents: true
      }
    }),
    ConfigModule.withConfigFactory(refreshTokenConfigFactory),
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}