@@ -7,13 +7,25 @@ import {Info, Intro, Separator} from '../../components/miscComponent';
7
7
import { createForm } from '../../components/form/formComponent' ;
8
8
import { deepDefaults } from '../../helpers/deepDefaults' ;
9
9
import phoneNumberField from '../../components/form/fields/phoneNumberField' ;
10
+ import { withTheme } from '../../components/widget/widgetContext' ;
11
+ import styled from 'styled-components' ;
10
12
11
13
12
14
const EmailRegisteringCredentialForm = createForm ( {
13
15
prefix : 'r5-mfa-credentials-email-' ,
14
16
submitLabel : 'mfa.register.email'
15
17
} ) ;
16
18
19
+ const EmailCredentialRemovalForm = createForm ( {
20
+ prefix : 'r5-mfa-credentials-email-removal-' ,
21
+ submitLabel : 'mfa.remove.email'
22
+ } )
23
+
24
+ const PhoneNumberCredentialRemovalForm = createForm ( {
25
+ prefix : 'r5-mfa-credentials-phone-number-removal-' ,
26
+ submitLabel : 'mfa.remove.phoneNumber'
27
+ } )
28
+
17
29
const VerificationCodeForm = createForm ( {
18
30
prefix : 'r5-mfa-credentials-verification-code-' ,
19
31
fields : [
@@ -33,6 +45,11 @@ const PhoneNumberRegisteringCredentialForm = config => createForm({
33
45
submitLabel : 'mfa.register.phoneNumber'
34
46
} )
35
47
48
+ const DivCredentialBlock = withTheme ( styled . div `
49
+ margin-left: ${ props => props . theme . get ( '_blockInnerHeight' ) } px;
50
+ margin-bottom: 5em;
51
+ ` ) ;
52
+
36
53
class MainView extends React . Component {
37
54
onEmailRegistering = _ => {
38
55
return this . props . apiClient . startMfaEmailRegistration ( {
@@ -48,21 +65,64 @@ class MainView extends React.Component {
48
65
} )
49
66
}
50
67
68
+ onEmailRemoval = _ => {
69
+ return this . props . apiClient . removeMfaEmail ( {
70
+ accessToken : this . props . accessToken
71
+ } )
72
+ }
73
+
74
+ onPhoneNumberRemoval = data => {
75
+ return this . props . apiClient . removeMfaPhoneNumber ( {
76
+ accessToken : this . props . accessToken ,
77
+ phoneNumber : data . phoneNumber
78
+ } )
79
+ }
80
+
51
81
render ( ) {
52
- const { i18n, showIntro, config } = this . props
82
+ const { i18n, showIntro, config, showRemoveMfaCredentials , credentials } = this . props
53
83
const PhoneNumberInputForm = PhoneNumberRegisteringCredentialForm ( config ) ;
84
+ const phoneNumberCredentialRegistered = credentials . find ( credential => credential . type === 'sms' )
85
+ const isEmailCredentialRegistered = credentials . some ( credential => credential . type === 'email' )
54
86
return (
55
87
< div >
56
- < div >
57
- { config . mfaEmailEnabled && showIntro && < Intro > { i18n ( 'mfa.email.explain' ) } </ Intro > }
58
- { config . mfaEmailEnabled && < EmailRegisteringCredentialForm handler = { this . onEmailRegistering } onSuccess = { data => this . props . goTo ( 'verification-code' , { ...data , registrationType : 'email' } ) } /> }
59
- </ div >
60
- { config . mfaEmailEnabled && config . mfaSmsEnabled && < Separator /> }
61
- < div >
62
- { config . mfaSmsEnabled && < Intro > { showIntro && < Intro > { i18n ( 'mfa.phoneNumber.explain' ) } </ Intro > } </ Intro > }
63
- { config . mfaSmsEnabled && < PhoneNumberInputForm
64
- handler = { this . onPhoneNumberRegistering } onSuccess = { data => this . props . goTo ( 'verification-code' , { ...data , registrationType : 'sms' } ) } /> }
65
- </ div >
88
+ < DivCredentialBlock >
89
+ { config . mfaEmailEnabled &&
90
+ < div >
91
+ { showIntro && < Intro > { i18n ( 'mfa.email.explain' ) } </ Intro > }
92
+ < EmailRegisteringCredentialForm handler = { this . onEmailRegistering } onSuccess = { data => this . props . goTo ( 'verification-code' , { ...data , registrationType : 'email' } ) } />
93
+ </ div >
94
+ }
95
+
96
+ { config . mfaEmailEnabled && config . mfaSmsEnabled && < Separator /> }
97
+ { config . mfaSmsEnabled &&
98
+ < div >
99
+ { showIntro && < Intro > { i18n ( 'mfa.phoneNumber.explain' ) } </ Intro > }
100
+ < PhoneNumberInputForm
101
+ handler = { this . onPhoneNumberRegistering } onSuccess = { data => this . props . goTo ( 'verification-code' , { ...data , registrationType : 'sms' } ) } />
102
+ </ div >
103
+ }
104
+ </ DivCredentialBlock >
105
+ < DivCredentialBlock >
106
+ { showRemoveMfaCredentials &&
107
+ config . mfaEmailEnabled &&
108
+ isEmailCredentialRegistered &&
109
+ < div >
110
+ { showIntro && < Intro > { i18n ( 'mfa.email.remove.explain' ) } </ Intro > }
111
+ < EmailCredentialRemovalForm handler = { this . onEmailRemoval }
112
+ onSuccess = { _ => this . props . goTo ( 'credential-removed' , { credentialType : 'email' } ) } />
113
+ </ div >
114
+ }
115
+ { showRemoveMfaCredentials && config . mfaEmailEnabled && config . mfaSmsEnabled && phoneNumberCredentialRegistered && isEmailCredentialRegistered && < Separator /> }
116
+ { showRemoveMfaCredentials &&
117
+ config . mfaSmsEnabled &&
118
+ phoneNumberCredentialRegistered &&
119
+ < div >
120
+ { showIntro && < Intro > { i18n ( 'mfa.phoneNumber.remove.explain' ) } </ Intro > }
121
+ < PhoneNumberCredentialRemovalForm handler = { data => this . onPhoneNumberRemoval ( { ...data , ...phoneNumberCredentialRegistered } ) }
122
+ onSuccess = { _ => this . props . goTo ( 'credential-removed' , { credentialType : 'sms' } ) } />
123
+ </ div >
124
+ }
125
+ </ DivCredentialBlock >
66
126
</ div >
67
127
)
68
128
}
@@ -102,17 +162,27 @@ const CredentialRegisteredView = ({ i18n, registrationType}) => <div>
102
162
{ registrationType === 'sms' && < Info > { i18n ( 'mfa.phoneNumber.registered' ) } </ Info > }
103
163
</ div >
104
164
165
+ const CredentialRemovedView = ( { i18n, credentialType} ) => < div >
166
+ { credentialType === 'email' && < Info > { i18n ( 'mfa.email.removed' ) } </ Info > }
167
+ { credentialType === 'sms' && < Info > { i18n ( 'mfa.phoneNumber.removed' ) } </ Info > }
168
+ </ div >
169
+
105
170
export default createMultiViewWidget ( {
106
171
initialView : 'main' ,
107
172
views : {
108
173
'main' : MainView ,
109
174
'credential-registered' : CredentialRegisteredView ,
110
- 'verification-code' : VerificationCodeView
175
+ 'verification-code' : VerificationCodeView ,
176
+ 'credential-removed' : CredentialRemovedView
111
177
} ,
112
- prepare : ( options ) => {
113
- return deepDefaults ( {
114
- showIntro : true ,
115
- ...options
116
- }
117
- ) }
178
+ prepare : ( options , { apiClient } ) => {
179
+ return apiClient . listMfaCredentials ( options . accessToken ) . then ( credentials => {
180
+ return deepDefaults ( {
181
+ showIntro : true ,
182
+ showRemoveMfaCredentials : true ,
183
+ ...options ,
184
+ ...credentials
185
+ } )
186
+ } )
187
+ }
118
188
} )
0 commit comments