Skip to content

Commit b81f89e

Browse files
committed
feat(aws-amplify-react): allow user to sign up with email or phone number
1 parent 0fc3fa2 commit b81f89e

File tree

3 files changed

+105
-37
lines changed

3 files changed

+105
-37
lines changed

packages/aws-amplify-react/src/Auth/SignUp.jsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
} from '../Amplify-UI/Amplify-UI-Components-React';
3434

3535
import countryDialCodes from './common/country-dial-codes.js';
36-
import defaultSignUpFields from './common/default-sign-in-fields'
36+
import signUpWithUsernameFields, { signUpWithEmailFields, signUpWithPhoneNumberFields } from './common/default-sign-up-fields'
3737
import { valid } from 'semver';
3838

3939
const logger = new Logger('SignUp');
@@ -48,8 +48,17 @@ export default class SignUp extends AuthPiece {
4848
this.sortFields = this.sortFields.bind(this);
4949
this.getDefaultDialCode = this.getDefaultDialCode.bind(this);
5050
this.checkCustomSignUpFields = this.checkCustomSignUpFields.bind(this);
51-
this.defaultSignUpFields = defaultSignUpFields;
5251
this.needPrefix = this.needPrefix.bind(this);
52+
53+
const { signUpConfig={} } = this.props || {};
54+
if (signUpConfig.emailAsUsername) {
55+
this.defaultSignUpFields = signUpWithEmailFields;
56+
} else if (signUpConfig.phoneNumberAsUsername) {
57+
this.defaultSignUpFields = signUpWithPhoneNumberFields;
58+
} else {
59+
this.defaultSignUpFields = signUpWithUsernameFields;
60+
}
61+
5362
this.header = (this.props &&
5463
this.props.signUpConfig &&
5564
this.props.signUpConfig.header) ? this.props.signUpConfig.header : 'Create a new account';
@@ -173,7 +182,7 @@ export default class SignUp extends AuthPiece {
173182
throw new Error('No Auth module found, please ensure @aws-amplify/auth is imported');
174183
}
175184

176-
let signup_info = {
185+
const signup_info = {
177186
username: this.inputs.username,
178187
password: this.inputs.password,
179188
attributes: {
@@ -194,7 +203,14 @@ export default class SignUp extends AuthPiece {
194203
}
195204
}
196205
});
197-
206+
207+
const { emailAsUsername, phoneNumberAsUsername } = this.props.signUpConfig || {};
208+
if (emailAsUsername) {
209+
signup_info.username = signup_info.attributes['email'];
210+
} else if (phoneNumberAsUsername) {
211+
signup_info.username = signup_info.attributes['phone_number'];
212+
}
213+
198214
Auth.signUp(signup_info).then((data) => {
199215
this.changeState('confirmSignUp', data.user.username)
200216
})

packages/aws-amplify-react/src/Auth/common/default-sign-in-fields.js

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
export default [
3+
{
4+
label: 'Username',
5+
key: 'username',
6+
required: true,
7+
placeholder: 'Username',
8+
displayOrder: 1,
9+
},
10+
{
11+
label: 'Password',
12+
key: 'password',
13+
required: true,
14+
placeholder: 'Password',
15+
type: 'password',
16+
displayOrder: 2,
17+
},
18+
{
19+
label: 'Email',
20+
key: 'email',
21+
required: true,
22+
placeholder: 'Email',
23+
type: 'email',
24+
displayOrder: 3
25+
},
26+
{
27+
label: 'Phone Number',
28+
key: 'phone_number',
29+
placeholder: 'Phone Number',
30+
required: true,
31+
displayOrder: 4
32+
}
33+
];
34+
35+
export const signUpWithEmailFields = [
36+
{
37+
label: 'Email',
38+
key: 'email',
39+
required: true,
40+
placeholder: 'Email',
41+
type: 'email',
42+
displayOrder: 1
43+
},
44+
{
45+
label: 'Password',
46+
key: 'password',
47+
required: true,
48+
placeholder: 'Password',
49+
type: 'password',
50+
displayOrder: 2,
51+
},
52+
{
53+
label: 'Phone Number',
54+
key: 'phone_number',
55+
placeholder: 'Phone Number',
56+
required: true,
57+
displayOrder: 3
58+
}
59+
];
60+
61+
export const signUpWithPhoneNumberFields = [
62+
{
63+
label: 'Phone Number',
64+
key: 'phone_number',
65+
placeholder: 'Phone Number',
66+
required: true,
67+
displayOrder: 1
68+
},
69+
{
70+
label: 'Password',
71+
key: 'password',
72+
required: true,
73+
placeholder: 'Password',
74+
type: 'password',
75+
displayOrder: 2,
76+
},
77+
{
78+
label: 'Email',
79+
key: 'email',
80+
required: true,
81+
placeholder: 'Email',
82+
type: 'email',
83+
displayOrder: 3
84+
},
85+
];

0 commit comments

Comments
 (0)