|
| 1 | +import { |
| 2 | + FlowDriver, |
| 3 | + ModalDriver, |
| 4 | + UserAuthMethodDriver, |
| 5 | +} from '@descope/sdk-component-drivers'; |
| 6 | +import { |
| 7 | + compose, |
| 8 | + createSingletonMixin, |
| 9 | + withMemCache, |
| 10 | +} from '@descope/sdk-helpers'; |
| 11 | +import { |
| 12 | + cookieConfigMixin, |
| 13 | + loggerMixin, |
| 14 | + modalMixin, |
| 15 | +} from '@descope/sdk-mixins'; |
| 16 | +import { stateManagementMixin } from '../../stateManagementMixin'; |
| 17 | +import { initWidgetRootMixin } from './initWidgetRootMixin'; |
| 18 | +import { getHasTotp } from '../../../state/selectors'; |
| 19 | +import { createFlowTemplate } from '../../helpers'; |
| 20 | +import { flowSyncThemeMixin } from '../../flowSyncThemeMixin'; |
| 21 | + |
| 22 | +export const initTotpUserAuthMethodMixin = createSingletonMixin( |
| 23 | + <T extends CustomElementConstructor>(superclass: T) => |
| 24 | + class TotpUserAuthMethodMixinClass extends compose( |
| 25 | + flowSyncThemeMixin, |
| 26 | + stateManagementMixin, |
| 27 | + loggerMixin, |
| 28 | + initWidgetRootMixin, |
| 29 | + cookieConfigMixin, |
| 30 | + modalMixin, |
| 31 | + )(superclass) { |
| 32 | + totpUserAuthMethod: UserAuthMethodDriver; |
| 33 | + |
| 34 | + #modal: ModalDriver; |
| 35 | + |
| 36 | + #flow: FlowDriver; |
| 37 | + |
| 38 | + #initModal() { |
| 39 | + if (!this.totpUserAuthMethod.flowId) return; |
| 40 | + |
| 41 | + this.#modal = this.createModal({ 'data-id': 'totp' }); |
| 42 | + this.#flow = new FlowDriver( |
| 43 | + () => this.#modal.ele?.querySelector('descope-wc'), |
| 44 | + { logger: this.logger }, |
| 45 | + ); |
| 46 | + this.#modal.afterClose = this.#initModalContent.bind(this); |
| 47 | + this.#initModalContent(); |
| 48 | + this.syncFlowTheme(this.#flow); |
| 49 | + } |
| 50 | + |
| 51 | + #initModalContent() { |
| 52 | + this.#modal.setContent( |
| 53 | + createFlowTemplate({ |
| 54 | + projectId: this.projectId, |
| 55 | + flowId: this.totpUserAuthMethod.flowId, |
| 56 | + baseUrl: this.baseUrl, |
| 57 | + baseStaticUrl: this.baseStaticUrl, |
| 58 | + baseCdnUrl: this.baseCdnUrl, |
| 59 | + refreshCookieName: this.refreshCookieName, |
| 60 | + }), |
| 61 | + ); |
| 62 | + this.#flow.onSuccess(() => { |
| 63 | + this.#modal.close(); |
| 64 | + this.actions.getMe(); |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + #initTotpAuthMethod() { |
| 69 | + this.totpUserAuthMethod = new UserAuthMethodDriver( |
| 70 | + () => |
| 71 | + this.shadowRoot?.querySelector( |
| 72 | + 'descope-user-auth-method[data-id="totp"]', |
| 73 | + ), |
| 74 | + { logger: this.logger }, |
| 75 | + ); |
| 76 | + |
| 77 | + this.totpUserAuthMethod.onButtonClick(() => { |
| 78 | + this.#modal?.open(); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + #onFulfilledUpdate = withMemCache( |
| 83 | + (hasTotp: ReturnType<typeof getHasTotp>) => { |
| 84 | + this.totpUserAuthMethod.fulfilled = hasTotp; |
| 85 | + }, |
| 86 | + ); |
| 87 | + |
| 88 | + async onWidgetRootReady() { |
| 89 | + await super.onWidgetRootReady?.(); |
| 90 | + |
| 91 | + this.#initTotpAuthMethod(); |
| 92 | + this.#initModal(); |
| 93 | + |
| 94 | + this.#onFulfilledUpdate(getHasTotp(this.state)); |
| 95 | + |
| 96 | + this.subscribe(this.#onFulfilledUpdate.bind(this), getHasTotp); |
| 97 | + } |
| 98 | + }, |
| 99 | +); |
0 commit comments