@@ -26,6 +26,9 @@ const Settings: NextPage = () => {
2626 const [ isOidc , setIsOidc ] = useState ( false ) ;
2727 const [ user , setUser ] = useState < any > ( null ) ;
2828 const [ activeAdminMessages , setActiveAdminMessages ] = useState < AdminMessage [ ] > ( [ ] ) ;
29+ const [ isOidcInvitation , setIsOidcInvitation ] = useState ( false ) ;
30+ const [ backButtonDisabled , setBackButtonDisabled ] = useState ( false ) ;
31+ const [ messages , setMessages ] = useState < any > ( null ) ;
2932 // Get ?flow=... from the URL
3033 const router = useRouter ( )
3134 const { flow : flowId , return_to : returnTo } = router . query
@@ -101,12 +104,50 @@ const Settings: NextPage = () => {
101104 setChangedFlow ( initialFlow )
102105
103106 //prevent password change option display if sso
104- if ( [ "microsoft" , "google" ] . includes ( initialFlow . identity . metadata_public ?. registration_scope ?. provider_id ) ) {
105- initialFlow . ui . nodes = initialFlow . ui . nodes . filter ( ( node : UiNode ) => node . group !== "password" ) ;
106- setIsOidc ( true ) ;
107+ requestAnimationFrame ( ( ) => {
108+ if ( [ "microsoft" , "google" ] . includes ( initialFlow . identity . metadata_public ?. registration_scope ?. provider_id ) ) {
109+ initialFlow . ui . nodes = initialFlow . ui . nodes . filter ( ( node : UiNode ) => node . group !== "password" ) ;
110+ setIsOidc ( true ) ;
111+ if ( initialFlow . identity . metadata_public ?. registration_scope ?. invitation_sso ) {
112+ setIsOidcInvitation ( true ) ;
113+ }
114+ const provider = initialFlow . identity . metadata_public ?. registration_scope ?. provider_id ;
115+ if ( provider === "google" ) {
116+ document . querySelector ( 'button[value="Microsoft"]' ) ?. setAttribute ( "class" , "hidden" ) ;
117+ } else if ( provider === "microsoft" ) {
118+ document . querySelector ( 'button[value="Google"]' ) ?. setAttribute ( "class" , "hidden" ) ;
119+ }
120+ }
121+ } ) ;
122+ } , [ initialFlow ] )
123+
124+ useEffect ( ( ) => {
125+ if ( ! changedFlow || ! initialFlow ) return ;
126+ const firstNameButtonVal = ( document . querySelector ( 'input[name="traits.name.first"]' ) as HTMLInputElement ) ?. value ;
127+ const lastNameButtonVal = ( document . querySelector ( 'input[name="traits.name.last"]' ) as HTMLInputElement ) ?. value ;
128+ if ( isOidc && isOidcInvitation ) {
129+ if ( firstNameButtonVal === "" || lastNameButtonVal === "" ) {
130+ setBackButtonDisabled ( true ) ;
131+ } else {
132+ setBackButtonDisabled ( false ) ;
133+ }
134+ } else {
135+ const emailButtonVal = ( document . querySelector ( 'input[name="traits.email"]' ) as HTMLInputElement ) ?. value ;
136+ const passwordButtonVal = ( document . querySelector ( 'input[name="password"]' ) as HTMLInputElement ) ?. value ;
137+ if ( firstNameButtonVal === "" || lastNameButtonVal === "" || emailButtonVal === "" || passwordButtonVal === "" ) {
138+ setBackButtonDisabled ( true ) ;
139+ } else {
140+ setBackButtonDisabled ( false ) ;
141+ }
107142 }
143+ } , [ isOidc , isOidcInvitation , initialFlow , changedFlow ] ) ;
108144
109- } , [ initialFlow ] )
145+ useEffect ( ( ) => {
146+ if ( ! changedFlow ) return ;
147+ if ( changedFlow . ui . messages ) {
148+ setMessages ( changedFlow . ui . messages ) ;
149+ }
150+ } , [ changedFlow ] )
110151
111152 const onSubmit = ( values : UpdateSettingsFlowBody ) =>
112153 ory
@@ -142,8 +183,8 @@ const Settings: NextPage = () => {
142183 < div id = "settings" >
143184 < h2 className = "title" > Profile management and security settings</ h2 >
144185 < div className = "form-container" >
186+ < Messages messages = { messages } />
145187 < h3 className = "subtitle" > Profile Settings</ h3 >
146- < Messages messages = { changedFlow ?. ui . messages } />
147188 < Flow
148189 hideGlobalMessages
149190 onSubmit = { onSubmit }
@@ -153,8 +194,7 @@ const Settings: NextPage = () => {
153194 </ div >
154195 { ! isOidc ?
155196 < div className = "form-container" >
156- < h3 className = "subtitle" > Change password</ h3 >
157- < Messages messages = { changedFlow ?. ui . messages } />
197+ < h3 className = "subtitle" > { flowId ? 'Set' : 'Change' } password</ h3 >
158198 < Flow
159199 hideGlobalMessages
160200 onSubmit = { onSubmit }
@@ -166,7 +206,6 @@ const Settings: NextPage = () => {
166206 { containsBackupCodes ? ( < div className = "form-container" >
167207 < h3 className = "subtitle" > Manage 2FA backup recovery codes</ h3 >
168208 < p > Recovery codes can be used in panic situations where you have lost access to your 2FA device.</ p >
169- < Messages messages = { changedFlow ?. ui . messages } />
170209 < Flow
171210 hideGlobalMessages
172211 onSubmit = { onSubmit }
@@ -184,7 +223,6 @@ const Settings: NextPage = () => {
184223 href = "https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2& hl = en & gl = US "
185224 target = "_blank" > Android</ a > ).
186225 </ p >
187- < Messages messages = { changedFlow ?. ui . messages } />
188226 < Flow
189227 hideGlobalMessages
190228 onSubmit = { onSubmit }
@@ -193,8 +231,19 @@ const Settings: NextPage = () => {
193231 />
194232 </ div > ) : ( < > </ > ) }
195233
234+ { isOidc && isOidcInvitation ? ( < div className = "form-container" >
235+ < Flow
236+ hideGlobalMessages
237+ onSubmit = { onSubmit }
238+ only = "oidc"
239+ flow = { changedFlow }
240+ />
241+ </ div > ) : ( < > </ > ) }
242+
196243 < div className = "link-container" >
197- < a className = "link" data-testid = "forgot-password" href = "/cognition" > Back</ a >
244+ < button className = "link disabled:opacity-50 disabled:cursor-not-allowed" data-testid = "forgot-password" disabled = { backButtonDisabled } onClick = { ( ) => {
245+ router . push ( "/cognition" )
246+ } } > Back</ button >
198247 </ div >
199248 </ div >
200249 </ div >
0 commit comments