Week 3 Decentralised Authentication
0. Learning Materials
Andrew's repo: week-3-again
My branch repo: 03-decentralized-authentication
My commits:
Task List
Env Variables
backend
HONEYCOMB_API_KEY
OTEL_SERVICE_NAME
OTEL_EXPORTER_OTLP_ENDPOINT
OTEL_EXPORTER_OTLP_HEADERS
AWS_XRAY_URL
AWS_XRAY_DAEMON_ADDRESS
AWS_DEFAULT_REGION
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
ROLLBAR_ACCESS_TOKEN
frontend
REACT_APP_PROJECT_REGION
REACT_APP_AWS_COGNITO_REGION
REACT_APP_AWS_USER_POOLS_ID
REACT_APP_CLIENT_ID
x-ray daemon
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_REGION
1. Workflow
1.1. Create Cognito Userpool in AWS Cognito
You could tailor the Cognito userpool configurations during creation based on your needs. I have listed the configurations Andrew used for this Cruddur project beneath the image.
Step 1.
Step 2.
Password Policy
User account recovery
Step 3.
Attribute verification and user account confirmation
Cognito-assisted verification and confirmation
Required attributes:
Step 4
Step 5
User pool name:
cruddur-user-pool
Then create the pool.
1.2. Integrate Cognito to the Frontend
1.2.1. Setup: install packages
Install the aws-amplify
npm library in the frontend. Make sure that you are at the root of the frontend application.
--save
: save the library to thepackage.json
.
After installing, make sure that the package is included in the updated package.json
.
1.2.2. Implement AWS Amplify in the Frontend application.
lines 4-14:
process.env
is a way for JavaScript to access the env variables stored locally and load them into a React application. A bit more about it here.
1.2.3. Update the checkAuth function HomeFeedPage.js
Now, replace the existing checkAuth()
function that uses Cookie
with the following:
lines 9-12: (optional) which will be then used to populate the user's data.
1.2.4. Implement Cognito Auth in ProfileInfo.js
Replace the signOut()
function with Cookie with a new version that uses Cognito's Auth.signOut()
.
1.2.5. Implement Cognito Auth in SignIn.js
2. Discussion: AWS Amplify
2.1. Amplify
AWS Amplify is a comprehensive set of tools and services to simplify the process of building secure and scalable web and mobile applications with a wide range of features which include but not limited to:
Authentication (we are using this one in week 3)
API management
Analytics and monitoring
Hosting Frontend libraries and UI components
2.2. process.env
In JavaScript,
process.env
is an object that provides access to environment variables within a Node.js application.You have to create a separate file named
.env
and store your local variables there.Make sure that the
.env
file is located in the React project's root directory.You can name the
.env
files depending on your development phase as in:.env.development
.env.production
, etc.
REACT_APP_
: in order for React to auto-load the variables, we need this prefix before every variable we use.
2.3. package-lock.json
"
package-lock.json
is automatically generated for any operations where npm modifies either thenode_modules
tree, orpackage.json
. It describes the exact tree that was generated, such that subsequent installs are able to generate identical trees, regardless of intermediate dependency updates." (npm, package-lock.json)The
package-lock.json
file is automatically generated and updated bynpm
whenever a new package is installed or the versions of existing packages are changed.It's recommended to commit the
package-lock.json
file together with thepackage.json
file so we can save any team members (or your future self) some trouble.
Last updated