@spartacus/cdc
Version:
Customer Data Cloud Integration library for Spartacus
288 lines (279 loc) • 12.8 kB
JavaScript
import * as i0 from '@angular/core';
import { Injectable, PLATFORM_ID, Inject, APP_INITIALIZER, NgModule } from '@angular/core';
import * as i1 from '@spartacus/core';
import { Config, facadeFactory, provideDefaultConfigFactory, ConfigInitializerService } from '@spartacus/core';
import { LogoutGuard } from '@spartacus/storefront';
import { take, tap } from 'rxjs/operators';
import * as i2 from '@angular/router';
import { isPlatformBrowser } from '@angular/common';
import { ReplaySubject, Subscription, combineLatest } from 'rxjs';
import * as i4 from '@spartacus/user/profile/root';
const CDC_FEATURE = 'cdc';
const CDC_CORE_FEATURE = 'cdcCore';
/**
* @override
*
* CDC version of logout guard. In addition to token revocation we invoke logout method from CDC JS lib.
*/
class CdcLogoutGuard extends LogoutGuard {
constructor(auth, cms, semanticPathService, protectedRoutes, router, winRef, authRedirectService) {
super(auth, cms, semanticPathService, protectedRoutes, router, authRedirectService);
this.auth = auth;
this.cms = cms;
this.semanticPathService = semanticPathService;
this.protectedRoutes = protectedRoutes;
this.router = router;
this.winRef = winRef;
this.authRedirectService = authRedirectService;
}
/**
* Logout user from CDC
*/
logoutFromCdc() {
var _a, _b, _c;
(_c = (_b = (_a = this.winRef.nativeWindow) === null || _a === void 0 ? void 0 : _a['gigya']) === null || _b === void 0 ? void 0 : _b.accounts) === null || _c === void 0 ? void 0 : _c.logout();
}
/**
* @override
* @returns promise to resolve after complete logout
*/
logout() {
return Promise.all([super.logout(), this.logoutFromCdc()]);
}
}
CdcLogoutGuard.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcLogoutGuard, deps: [{ token: i1.AuthService }, { token: i1.CmsService }, { token: i1.SemanticPathService }, { token: i1.ProtectedRoutesService }, { token: i2.Router }, { token: i1.WindowRef }, { token: i1.AuthRedirectService }], target: i0.ɵɵFactoryTarget.Injectable });
CdcLogoutGuard.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcLogoutGuard, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcLogoutGuard, decorators: [{
type: Injectable,
args: [{
providedIn: 'root',
}]
}], ctorParameters: function () { return [{ type: i1.AuthService }, { type: i1.CmsService }, { type: i1.SemanticPathService }, { type: i1.ProtectedRoutesService }, { type: i2.Router }, { type: i1.WindowRef }, { type: i1.AuthRedirectService }]; } });
class CdcConfig {
}
CdcConfig.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcConfig, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
CdcConfig.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcConfig, providedIn: 'root', useExisting: Config });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcConfig, decorators: [{
type: Injectable,
args: [{
providedIn: 'root',
useExisting: Config,
}]
}] });
class CdcAuthFacade {
}
CdcAuthFacade.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcAuthFacade, deps: [], target: i0.ɵɵFactoryTarget.Injectable });
CdcAuthFacade.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcAuthFacade, providedIn: 'root', useFactory: () => facadeFactory({
facade: CdcAuthFacade,
feature: CDC_CORE_FEATURE,
methods: ['loginWithCustomCdcFlow', 'loginWithToken'],
async: true,
}) });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcAuthFacade, decorators: [{
type: Injectable,
args: [{
providedIn: 'root',
useFactory: () => facadeFactory({
facade: CdcAuthFacade,
feature: CDC_CORE_FEATURE,
methods: ['loginWithCustomCdcFlow', 'loginWithToken'],
async: true,
}),
}]
}] });
class CdcJsService {
constructor(cdcConfig, baseSiteService, languageService, scriptLoader, winRef, cdcAuth, auth, zone, userProfileFacade, platform) {
this.cdcConfig = cdcConfig;
this.baseSiteService = baseSiteService;
this.languageService = languageService;
this.scriptLoader = scriptLoader;
this.winRef = winRef;
this.cdcAuth = cdcAuth;
this.auth = auth;
this.zone = zone;
this.userProfileFacade = userProfileFacade;
this.platform = platform;
this.loaded$ = new ReplaySubject(1);
this.errorLoading$ = new ReplaySubject(1);
this.subscription = new Subscription();
}
/**
* Initialize CDC script
*/
initialize() {
this.loadCdcJavascript();
}
/**
* Returns observable with the information if CDC script is loaded.
*/
didLoad() {
return this.loaded$.asObservable();
}
/**
* Returns observable with the information if CDC script failed to load.
*/
didScriptFailToLoad() {
return this.errorLoading$.asObservable();
}
/**
* Method which loads the CDC Script
*/
loadCdcJavascript() {
// Only load the script on client side (no SSR)
if (isPlatformBrowser(this.platform)) {
this.subscription.add(combineLatest([
this.baseSiteService.getActive(),
this.languageService.getActive(),
])
.pipe(take(1))
.subscribe(([baseSite, language]) => {
var _a;
const scriptForBaseSite = this.getJavascriptUrlForCurrentSite(baseSite);
if (scriptForBaseSite) {
const javascriptUrl = `${scriptForBaseSite}&lang=${language}`;
this.scriptLoader.embedScript({
src: javascriptUrl,
params: undefined,
attributes: { type: 'text/javascript' },
callback: () => {
this.registerEventListeners(baseSite);
this.loaded$.next(true);
},
errorCallback: () => {
this.errorLoading$.next(true);
},
});
if (((_a = this.winRef) === null || _a === void 0 ? void 0 : _a.nativeWindow) !== undefined) {
this.winRef.nativeWindow['__gigyaConf'] = {
include: 'id_token',
};
}
}
}));
}
}
getJavascriptUrlForCurrentSite(baseSite) {
var _a;
const filteredConfigs = ((_a = this.cdcConfig.cdc) !== null && _a !== void 0 ? _a : []).filter((conf) => conf.baseSite === baseSite);
if (filteredConfigs && filteredConfigs.length > 0) {
return filteredConfigs[0].javascriptUrl;
}
return '';
}
/**
* Register login event listeners for CDC login
*
* @param baseSite
*/
registerEventListeners(baseSite) {
this.addCdcEventHandlers(baseSite);
}
/**
* Method to register CDC event handlers
*
* @param baseSite
*/
addCdcEventHandlers(baseSite) {
var _a, _b, _c;
(_c = (_b = (_a = this.winRef.nativeWindow) === null || _a === void 0 ? void 0 : _a['gigya']) === null || _b === void 0 ? void 0 : _b.accounts) === null || _c === void 0 ? void 0 : _c.addEventHandlers({
onLogin: (...params) => {
this.zone.run(() => this.onLoginEventHandler(baseSite, ...params));
},
});
}
/**
* Trigger login to Commerce once an onLogin event is triggered by CDC Screen Set.
*
* @param baseSite
* @param response
*/
onLoginEventHandler(baseSite, response) {
if (response) {
this.cdcAuth.loginWithCustomCdcFlow(response.UID, response.UIDSignature, response.signatureTimestamp, response.id_token !== undefined ? response.id_token : '', baseSite);
}
}
/**
* Updates user details using the existing User API
*
* @param response
*/
onProfileUpdateEventHandler(response) {
if (response) {
const userDetails = {};
userDetails.firstName = response.profile.firstName;
userDetails.lastName = response.profile.lastName;
this.userProfileFacade.update(userDetails);
}
}
ngOnDestroy() {
if (this.subscription) {
this.subscription.unsubscribe();
}
}
}
CdcJsService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcJsService, deps: [{ token: CdcConfig }, { token: i1.BaseSiteService }, { token: i1.LanguageService }, { token: i1.ScriptLoader }, { token: i1.WindowRef }, { token: CdcAuthFacade }, { token: i1.AuthService }, { token: i0.NgZone }, { token: i4.UserProfileFacade }, { token: PLATFORM_ID }], target: i0.ɵɵFactoryTarget.Injectable });
CdcJsService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcJsService, providedIn: 'root' });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcJsService, decorators: [{
type: Injectable,
args: [{
providedIn: 'root',
}]
}], ctorParameters: function () { return [{ type: CdcConfig }, { type: i1.BaseSiteService }, { type: i1.LanguageService }, { type: i1.ScriptLoader }, { type: i1.WindowRef }, { type: CdcAuthFacade }, { type: i1.AuthService }, { type: i0.NgZone }, { type: i4.UserProfileFacade }, { type: undefined, decorators: [{
type: Inject,
args: [PLATFORM_ID]
}] }]; } });
function cdcJsFactory(cdcJsService, configInit) {
const func = () => configInit
.getStable('context', 'cdc')
.pipe(tap(() => {
cdcJsService.initialize();
}))
.toPromise();
return func;
}
function defaultCdcComponentsConfig() {
const config = {
featureModules: {
[CDC_FEATURE]: {
cmsComponents: ['GigyaRaasComponent'],
},
// by default core is bundled together with components
[CDC_CORE_FEATURE]: CDC_FEATURE,
},
};
return config;
}
class CdcRootModule {
}
CdcRootModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcRootModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
CdcRootModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcRootModule });
CdcRootModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcRootModule, providers: [
provideDefaultConfigFactory(defaultCdcComponentsConfig),
{ provide: LogoutGuard, useExisting: CdcLogoutGuard },
{
provide: APP_INITIALIZER,
useFactory: cdcJsFactory,
deps: [CdcJsService, ConfigInitializerService],
multi: true,
},
] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.5", ngImport: i0, type: CdcRootModule, decorators: [{
type: NgModule,
args: [{
providers: [
provideDefaultConfigFactory(defaultCdcComponentsConfig),
{ provide: LogoutGuard, useExisting: CdcLogoutGuard },
{
provide: APP_INITIALIZER,
useFactory: cdcJsFactory,
deps: [CdcJsService, ConfigInitializerService],
multi: true,
},
],
}]
}] });
/**
* Generated bundle index. Do not edit.
*/
export { CDC_CORE_FEATURE, CDC_FEATURE, CdcAuthFacade, CdcConfig, CdcJsService, CdcLogoutGuard, CdcRootModule, cdcJsFactory, defaultCdcComponentsConfig };
//# sourceMappingURL=spartacus-cdc-root.js.map