-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
AuthPiece.tsx
186 lines (164 loc) · 4.87 KB
/
AuthPiece.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
179
180
181
182
183
184
185
186
/*
* Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
* the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import React from 'react';
import { Keyboard } from 'react-native';
import { Auth, Logger, I18n } from 'aws-amplify';
import { isEmpty } from '@aws-amplify/core';
import AmplifyTheme, { AmplifyThemeType } from '../AmplifyTheme';
import AmplifyMessageMap from '../AmplifyMessageMap';
import { FormField, PhoneField } from '../AmplifyUI';
import TEST_ID from '../AmplifyTestIDs';
import { OnStateChangeType, UsernameAttributesType } from '../../types';
import { setTestId } from '../Utils';
const logger = new Logger('AuthPiece');
const labelMap = {
email: 'Email',
phone_number: 'Phone Number',
username: 'Username',
};
export interface IAuthPieceProps {
authData?: any;
authState?: string;
errorMessage?: string;
messageMap?: any;
onStateChange?: OnStateChangeType;
theme?: AmplifyThemeType;
track?: () => void;
usernameAttributes?: UsernameAttributesType;
}
export interface IAuthPieceState {
email?: string;
error?: string | null;
phone_number?: string;
username?: string;
}
export default class AuthPiece<Props extends IAuthPieceProps, State extends IAuthPieceState> extends React.Component<
Props,
State
> {
_isHidden: boolean;
_validAuthStates: String[];
constructor(props) {
super(props);
this._isHidden = true;
this._validAuthStates = [];
this.changeState = this.changeState.bind(this);
this.error = this.error.bind(this);
this.getUsernameFromInput = this.getUsernameFromInput.bind(this);
this.renderUsernameField = this.renderUsernameField.bind(this);
}
getUsernameFromInput() {
const { usernameAttributes = 'username' } = this.props;
switch (usernameAttributes) {
case 'email':
return this.state.email;
case 'phone_number':
return this.state.phone_number;
default:
return this.state.username;
}
}
renderUsernameField(theme: AmplifyThemeType) {
const value = this.getUsernameFromInput();
const { usernameAttributes = [] } = this.props;
if (usernameAttributes === 'email') {
return (
<FormField
theme={theme}
onChangeText={(text) => this.setState({ email: text })}
label={I18n.get('Email')}
placeholder={I18n.get('Enter your email')}
required={true}
{...setTestId(TEST_ID.AUTH.EMAIL_INPUT)}
value={value}
/>
);
} else if (usernameAttributes === 'phone_number') {
return (
<PhoneField
theme={theme}
key={'phone_number'}
onChangeText={(text) => this.setState({ phone_number: text })}
label={I18n.get('Phone Number')}
placeholder={I18n.get('Enter your phone number')}
keyboardType="phone-pad"
required={true}
{...setTestId(TEST_ID.AUTH.PHONE_INPUT)}
value={value}
/>
);
} else {
return (
<FormField
theme={theme}
onChangeText={(text) => this.setState({ username: text })}
label={I18n.get(this.getUsernameLabel())}
placeholder={I18n.get('Enter your username')}
required={true}
{...setTestId(TEST_ID.AUTH.USERNAME_INPUT)}
value={value}
/>
);
}
}
getUsernameLabel() {
const { usernameAttributes = 'username' } = this.props;
return labelMap[usernameAttributes] || usernameAttributes;
}
changeState(state: string, data?: any) {
if (this.props.onStateChange) {
this.props.onStateChange(state, data);
}
}
checkContact(user: any) {
Auth.verifiedContact(user).then((data) => {
logger.debug('verified user attributes', data);
if (!isEmpty(data.verified)) {
this.changeState('signedIn', user);
} else {
user = Object.assign(user, data);
this.changeState('verifyContact', user);
}
});
}
error(err: any) {
logger.debug(err);
let msg = '';
if (typeof err === 'string') {
msg = err;
} else if (err.message) {
msg = err.message;
} else {
msg = JSON.stringify(err);
}
const map = this.props.errorMessage || this.props.messageMap || AmplifyMessageMap;
msg = typeof map === 'string' ? map : map(msg);
this.setState({ error: msg });
Keyboard.dismiss();
}
render() {
if (!this._validAuthStates.includes(this.props.authState)) {
this._isHidden = true;
return null;
}
if (this._isHidden) {
const { track } = this.props;
if (track) track();
}
this._isHidden = false;
return this.showComponent(this.props.theme || AmplifyTheme);
}
showComponent(theme): any {
throw "You must implement showComponent(theme) and don't forget to set this._validAuthStates.";
}
}