You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 14, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: Auth/1_UserAuthentication/README.md
+90-75Lines changed: 90 additions & 75 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -160,7 +160,9 @@ You will need to create a Cognito Identity Pool linked to the Cognito User Pool
160
160
161
161
1. Choose **Allow** to allow Cognito Identity Pools to setup IAM roles for your application's users. Permissions and settings of these roles can be customized later.
162
162
163
-
1. Copy/paste the *Identity Pool ID*, highlighted in red within the code sample in the Get AWS Credentials section, into your Cloud9 scatchpad editor tab. Make sure not to copy the quotation marks, but include the region code and ":" character!
163
+
1. Copy/paste the *Identity Pool ID*, highlighted in red within the code sample in the Get AWS Credentials section, into your Cloud9 scatchpad editor tab.
164
+
165
+
> Do not copy the quotation marks, but include the region code and ":" character.
164
166
165
167

166
168
@@ -193,7 +195,7 @@ You'll need to complete the implementation of the onSubmitForm and onSubmitVerif
193
195
194
196
> Be sure to fill in the **'' blanks** with your config values. You do not need to modify the example values shown in the comments as they are just for reference and not leveraged by your application.
195
197
196
-
1. Be sure to **save your changes** to the config file so your new Amplify settings take effect. Any unsaved changes to a file are indicated by a dot icon in the tab of the editor so if you see a gray dot next to the file name, you may have forgotten to save.
198
+
1. **Save your changes** to the Amplify config file so your new settings take effect. Any unsaved changes to a file are indicated by a dot icon in the tab of the editor so if you see a gray dot next to the file name, you may have forgotten to save.
197
199
198
200
1. Next, edit the *website/src/index.js* file to add the following lines to the **top of the file** **(but below all the other imports)** to configure Amplify then save your changes:
199
201
@@ -203,6 +205,12 @@ You'll need to complete the implementation of the onSubmitForm and onSubmitVerif
203
205
204
206
Amplify.configure(awsConfig);
205
207
```
208
+
209
+
> After making this changes, your imports should be in the following order:
1. **Save your changes** to the *website/src/index.js* file.
206
214
207
215
1. Next, we need to ensure our application evaluates the user's authenticated state. In the same */website/src/index.js* file, find and replace the **isAuthenticated method** with the code below to use our Amplify library's built-in user session to check this status.
208
216
@@ -212,7 +220,7 @@ You'll need to complete the implementation of the onSubmitForm and onSubmitVerif
212
220
213
221
1. **Save your changes** to the */website/src/index.js* file.
214
222
215
-
1. Now that we've imported the Amplify and configured the Amplify library, we need to update our application's code to sign-up users using Amplify and Cognito User Pools by finding and replacing the following methods within the */website/src/auth/SignUp.js* file with the code below **then save your changes**.
223
+
1. Now that we've imported the Amplify and configured the Amplify library, we need to update our application's code to sign-up users using Amplify and Cognito User Pools by finding and replacing the following methods within the */website/src/auth/SignUp.js* file with the following code.
216
224
217
225
> You only need to replace these two methods. The rest of the SignUp.js file should not be modified.
218
226
@@ -222,22 +230,26 @@ You'll need to complete the implementation of the onSubmitForm and onSubmitVerif
222
230
223
231
```
224
232
async onSubmitForm(e) {
225
-
e.preventDefault();
226
-
try {
227
-
const params = {
228
-
username: this.state.email.replace(/[@.]/g, '|'),
229
-
password: this.state.password,
230
-
attributes: {
231
-
email: this.state.email,
232
-
phone_number: this.state.phone
233
-
},
234
-
validationData: []
235
-
};
236
-
const data = await Auth.signUp(params);
237
-
console.log(data);
238
-
this.setState({ stage: 1 });
239
-
} catch (err) {
240
-
if (err.message === "User already exists") {
233
+
e.preventDefault();
234
+
try {
235
+
const params = {
236
+
username: this.state.email.replace(/[@.]/g, '|'),
237
+
password: this.state.password,
238
+
attributes: {
239
+
email: this.state.email,
240
+
phone_number: this.state.phone
241
+
},
242
+
validationData: []
243
+
};
244
+
const data = await Auth.signUp(params);
245
+
console.log(data);
246
+
this.setState({ stage: 1 });
247
+
} catch (err) {
248
+
if (err === "No userPool") {
249
+
// User pool not defined in Amplify config file
250
+
console.error("User Pool not defined");
251
+
alert("User Pool not defined. Amplify config must be updated with user pool config");
252
+
} else if (err.message === "User already exists") {
241
253
// Setting state to allow user to proceed to enter verification code
242
254
this.setState({ stage: 1 });
243
255
} else {
@@ -246,27 +258,29 @@ You'll need to complete the implementation of the onSubmitForm and onSubmitVerif
246
258
console.error("Exception from Auth.signUp: ", err);
console.error("Exception from Auth.confirmSignUp: ", err);
275
+
alert(err.message);
276
+
console.error("Exception from Auth.confirmSignUp: ", err);
265
277
}
266
278
}
267
279
```
280
+
281
+
1. **Save your changes** to the */website/src/auth/SignUp.js* file.
268
282
269
-
1. You additionally need to integrate the sign-in capability to use AWS Amplify and Cognito by finding and replacing the following methods within the */website/src/auth/SignIn.js* file with the code below **then save your changes**.
283
+
1. You additionally need to integrate the sign-in capability to use AWS Amplify and Cognito by finding and replacing the following methods within the */website/src/auth/SignIn.js* file with the code below.
270
284
271
285
> You only need to replace these two methods. The rest of the SignIn.js file should not be modified.
272
286
@@ -275,52 +289,53 @@ You'll need to complete the implementation of the onSubmitForm and onSubmitVerif
275
289
> The onSubmitVerification method is used to submit a verification code whenever multi-factor authentication is required to authenticate. For this workshop, this method will not be invoked since you did not require multi-factor authentication earlier when configuring your Cognito User Pool.
276
290
277
291
```
278
-
async onSubmitForm(e) {
279
-
e.preventDefault();
280
-
try {
281
-
const userObject = await Auth.signIn(
282
-
this.state.email.replace(/[@.]/g, '|'),
283
-
this.state.password
284
-
);
285
-
console.log('userObject', userObject);
286
-
if (userObject.challengeName) {
287
-
// Auth challenges are pending prior to token issuance
288
-
this.setState({ userObject, stage: 1 });
289
-
} else {
290
-
// No remaining auth challenges need to be satisfied
291
-
const session = await Auth.currentSession();
292
-
// console.log('Cognito User Access Token:', session.getAccessToken().getJwtToken());
293
-
console.log('Cognito User Identity Token:', session.getIdToken().getJwtToken());
294
-
// console.log('Cognito User Refresh Token', session.getRefreshToken().getToken());
0 commit comments