Add Authentication to the SPA using Amazon Cognito User Pools
In this exercise, we will make use of the Cognito User Pool and Identity Pool, and mark one of our API endpoints as requiring authentication via Cognito User Pools.
webapp-configuration.js
in the scripts
directory of the web site bundle you have downloaded and exploded. This file has the following contents:(function () {
'use strict';
angular
.module('app')
.constant('COGNITO_IDENTITY_POOL_ID', 'REPLACE_WITH_COGNITO_IDENTITY_POOL_ID')
.constant('COGNITO_USER_POOL', 'REPLACE_WITH_COGNITO_USER_POOL')
.constant('COGNITO_USER_POOL_CLIENT_ID','REPLACE_WITH_COGNITO_USER_POOL_CLIENT_ID')
.constant('COGNITO_APP_WEB_DOMAIN', 'REPLACE_WITH_S3_BUCKET_WWW')
.constant('AWS_REGION', 'ap-northeast-1')
.constant('APP_BANNER', 'TravelBuddy')
})();
The SPA is implemented using the Angular framework, which allows you to define global constants that are used as configuration variables throughout your application. This file defines the values of the configuration variables that you need to set based on your AWS Account’s ARNs and Ids for the various Cognito resources. Open the webapp-configuration.js
file in a text editor and keep it open while we make changes in the following steps.
cognito
in the filter box. Press Enter.REPLACE_WITH_COGNITO_USER_POOL
in the webapp-configuration.js
file with the value.ID
value beneath the App client TravelBuddyWebApp heading and copy it to your clipboard. For example:REPLACE_WITH_COGNITO_USER_POOL_CLIENT_ID
in the webapp-configuration.js
file with the value of the App client ID.ap-northeast-1:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeee
Make sure you copy the whole entry, including the region specifier.
REPLACE_WITH_COGNITO_IDENTITY_POOL_ID
in the webapp-configuration.js
file with the value of the Identity Pool Id.S3BucketWWW
which is the S3 bucket name for the web hosting bucket you are using to serve your SPA.REPLACE_WITH_S3_BUCKET_WWW
in the webapp-configuration.js
file with the value of the S3BucketWWW
from the Additional Info panel in lab.webapp-configuration.js
file will look something like this:(function () {
'use strict';
angular
.module('app')
.constant('COGNITO_IDENTITY_POOL_ID', 'ap-northeast-1:429c1d1c-7a5e-4d53-be18-177b806c53a4')
.constant('COGNITO_USER_POOL', 'ap-northeast-1_YSRyht4ck')
.constant('COGNITO_USER_POOL_CLIENT_ID','3mh9486bds2f2cr3gio8bdjoik')
.constant('COGNITO_APP_WEB_DOMAIN', 'devax-m6-s3bucketwebsite18ddbcd4-19ewci3z6hece')
.constant('AWS_REGION', 'ap-northeast-1')
.constant('APP_BANNER', 'TravelBuddy')
})();
webapp-configuration.js
file. Save the file, and upload it to the S3BucketWWW
in the scripts folder, to overwrite the existing stubbed-out file. You can perform the upload using ath S3 console, or the commandline.