11import React from "react" ;
22import { defineExtension } from "@webiny/project/defineExtension/index.js" ;
33import { Api , Admin , Infra } from "@webiny/project-aws" ;
4+ import { Await } from "@webiny/react-properties" ;
45import { z } from "zod" ;
56
67const identityProviderSchema = z . object ( {
@@ -28,11 +29,23 @@ export const Cognito = defineExtension({
2829 paramsSchema : z . object ( {
2930 apiConfig : z . string ( ) . describe ( "Path to API configuration." ) . optional ( ) ,
3031 adminConfig : z . string ( ) . describe ( "Path to Admin configuration." ) . optional ( ) ,
31- federation : federationSchema . optional ( ) ,
32+ federation : z
33+ . union ( [
34+ federationSchema ,
35+ z . custom < ( ) => Promise < z . infer < typeof federationSchema > > > (
36+ val => typeof val === "function"
37+ )
38+ ] )
39+ . optional ( ) ,
3240 mfa : z . boolean ( ) . describe ( "Enable TOTP MFA for all users." ) . default ( false )
3341 } ) ,
3442 render : props => {
35- const federation = props . federation ;
43+ const federationProp = props . federation ;
44+ const federationFn = federationProp
45+ ? typeof federationProp === "function"
46+ ? federationProp
47+ : ( ) => Promise . resolve ( federationProp )
48+ : null ;
3649
3750 return (
3851 < >
@@ -51,43 +64,47 @@ export const Cognito = defineExtension({
5164 { props . adminConfig ? < Admin . Extension src = { props . adminConfig } /> : null }
5265
5366 { /* Federation infra + admin config */ }
54- { federation ? (
55- < >
56- { /* Pulumi: create User Pool Domain, IdP resources, OAuth client */ }
57- < Infra . EnvVar
58- varName = { "COGNITO_FEDERATION_INFRA_CONFIG" }
59- value = { JSON . stringify ( {
60- domain : federation . domain ,
61- callbackUrls : federation . callbackUrls ,
62- logoutUrls : federation . logoutUrls ,
63- identityProviders : federation . identityProviders . map ( idp => ( {
64- type : idp . type ,
65- name : idp . name ,
66- providerDetails : idp . providerDetails ,
67- idpIdentifiers : idp . idpIdentifiers ,
68- attributeMapping : idp . attributeMapping
69- } ) )
70- } ) }
71- />
72- < Infra . Core . Pulumi
73- src = { import . meta. dirname + "/infra/CognitoFederationPulumi.js" }
74- />
75-
76- { /* Admin: pass federation config as build param */ }
77- < Admin . BuildParam
78- paramName = { "cognitoFederation" }
79- value = { {
80- callbackUrls : federation . callbackUrls ,
81- logoutUrls : federation . logoutUrls || federation . callbackUrls ,
82- responseType : federation . responseType ,
83- allowCredentialsLogin : federation . allowCredentialsLogin ,
84- providers : federation . identityProviders . map ( idp => ( {
85- name : idp . name || idp . type ,
86- label : idp . label
87- } ) )
88- } }
89- />
90- </ >
67+ { federationFn ? (
68+ < Await fn = { federationFn } >
69+ { federation => (
70+ < >
71+ < Infra . EnvVar
72+ varName = { "COGNITO_FEDERATION_INFRA_CONFIG" }
73+ value = { JSON . stringify ( {
74+ domain : federation . domain ,
75+ callbackUrls : federation . callbackUrls ,
76+ logoutUrls : federation . logoutUrls ,
77+ identityProviders : federation . identityProviders . map (
78+ idp => ( {
79+ type : idp . type ,
80+ name : idp . name ,
81+ providerDetails : idp . providerDetails ,
82+ idpIdentifiers : idp . idpIdentifiers ,
83+ attributeMapping : idp . attributeMapping
84+ } )
85+ )
86+ } ) }
87+ />
88+ < Infra . Core . Pulumi
89+ src = { import . meta. dirname + "/infra/CognitoFederationPulumi.js" }
90+ />
91+ < Admin . BuildParam
92+ paramName = { "cognitoFederation" }
93+ value = { {
94+ callbackUrls : federation . callbackUrls ,
95+ logoutUrls :
96+ federation . logoutUrls || federation . callbackUrls ,
97+ responseType : federation . responseType ,
98+ allowCredentialsLogin : federation . allowCredentialsLogin ,
99+ providers : federation . identityProviders . map ( idp => ( {
100+ name : idp . name || idp . type ,
101+ label : idp . label
102+ } ) )
103+ } }
104+ />
105+ </ >
106+ ) }
107+ </ Await >
91108 ) : null }
92109 </ >
93110 ) ;
0 commit comments