/
amplify-verify-contact.tsx
178 lines (156 loc) · 4.88 KB
/
amplify-verify-contact.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import { Auth } from '@aws-amplify/auth';
import { I18n, Logger } from '@aws-amplify/core';
import { Component, h, Prop, State, Host } from '@stencil/core';
import { AuthState, AuthStateHandler, CognitoUserInterface } from '../../common/types/auth-types';
import { NO_AUTH_MODULE_FOUND } from '../../common/constants';
import { dispatchAuthStateChangeEvent, dispatchToastHubEvent } from '../../common/helpers';
import { Translations } from '../../common/Translations';
const logger = new Logger('AmplifyVerifyContact');
@Component({
tag: 'amplify-verify-contact',
shadow: true,
})
export class AmplifyVerifyContact {
/** Authentication state handler */
@Prop() handleAuthStateChange: AuthStateHandler = dispatchAuthStateChangeEvent;
/** User with unverified contact information */
@Prop() user: CognitoUserInterface;
@State() verifyAttr: any;
@State() loading: boolean = false;
@State() code: string;
@State() contact: 'email' | 'phone_number';
private handleSubmit(event) {
event.preventDefault();
this.verifyAttr ? this.submit(this.code) : this.verify(this.contact);
}
private async submit(code) {
const attr = this.verifyAttr;
if (!Auth || typeof Auth.verifyCurrentUserAttributeSubmit !== 'function') {
throw new Error(NO_AUTH_MODULE_FOUND);
}
this.loading = true;
try {
const data = await Auth.verifyCurrentUserAttributeSubmit(attr, code);
logger.debug(data);
this.handleAuthStateChange(AuthState.SignedIn, this.user);
this.verifyAttr = null;
} catch (error) {
dispatchToastHubEvent(error);
logger.error(error);
} finally {
this.loading = false;
}
}
private async verify(contact: keyof CognitoUserInterface['unverified']) {
if (!contact) {
logger.error('Neither Email nor Phone Number selected');
return;
}
if (!Auth || typeof Auth.verifyCurrentUserAttribute !== 'function') {
throw new Error(NO_AUTH_MODULE_FOUND);
}
this.loading = true;
try {
const data = await Auth.verifyCurrentUserAttribute(contact);
logger.debug(data);
this.verifyAttr = contact;
} catch (error) {
dispatchToastHubEvent(error);
logger.error(error);
} finally {
this.loading = false;
}
}
private handleInputChange(event) {
const inputName = event.target.name;
if (inputName === 'code') {
this.code = event.target.value;
} else if (inputName === 'contact') {
this.contact = event.target.value;
}
}
private renderSubmit() {
return (
<div>
<amplify-input
inputProps={{
autocomplete: 'off',
'data-test': 'verify-contact-code-input',
}}
name="code"
placeholder={I18n.get(Translations.CODE_PLACEHOLDER)}
handleInputChange={event => this.handleInputChange(event)}
/>
</div>
);
}
private renderVerify() {
const user = this.user;
if (!user) {
logger.debug('No user to verify');
return null;
}
const { unverified } = user;
if (!unverified) {
logger.debug('Unverified variable does not exist on user');
return null;
}
const { email, phone_number } = unverified;
return (
<div>
{email && (
<amplify-radio-button
label={I18n.get(Translations.VERIFY_CONTACT_EMAIL_LABEL)}
key="email"
name="contact"
value="email"
handleInputChange={event => this.handleInputChange(event)}
inputProps={{
'data-test': 'verify-contact-email-radio',
}}
/>
)}
{phone_number && (
<amplify-radio-button
label={I18n.get(Translations.VERIFY_CONTACT_PHONE_LABEL)}
key="phone_number"
name="contact"
value="phone_number"
handleInputChange={event => this.handleInputChange(event)}
inputProps={{
'data-test': 'verify-contact-email-radio',
}}
/>
)}
</div>
);
}
render() {
return (
<Host>
<amplify-form-section
handleSubmit={event => this.handleSubmit(event)}
headerText={I18n.get(Translations.VERIFY_CONTACT_HEADER_TEXT)}
loading={this.loading}
secondaryFooterContent={
<span>
<amplify-button
variant="anchor"
onClick={() => this.handleAuthStateChange(AuthState.SignedIn, this.user)}
>
Skip
</amplify-button>
</span>
}
submitButtonText={
this.verifyAttr
? I18n.get(Translations.VERIFY_CONTACT_SUBMIT_LABEL)
: I18n.get(Translations.VERIFY_CONTACT_VERIFY_LABEL)
}
>
{this.verifyAttr ? this.renderSubmit() : this.renderVerify()}
</amplify-form-section>
</Host>
);
}
}