Cognito and amplify in my html/css landing page

0

Tools Used:

AWS Amplify CLI: For configuring and managing backend resources on AWS. Git: For version control of the project. GitHub: As the remote repository to store the code. AWS CLI: For configuring AWS credentials and regions. Node.js and npm: For managing project dependencies and scripts. Process Undertaken:

Initial Configuration: Configured AWS CLI with access credentials (AWS Access Key ID and AWS Secret Access Key) and set the default region to us-east-1. Initialized the Amplify project locally and linked it to the provided AppID. Project Setup: Updated the project-config.json file to include the correct AppID and added build and start scripts in the package.json. Attempted to deploy the project using AWS Amplify CLI.

Issues with Amplify Gen 2: The primary issue is that the CLI keeps reverting to Amplify Gen 1 instead of using Amplify Gen 2, which is the recommended version. During the initialization process, selecting Gen 2 fails or defaults back to Gen 1, which is preventing proper deployment. Version Control and Deployment: Initialized the local Git repository, connected it to GitHub, and pushed changes to the main branch. Deployment was attempted through Amplify, but issues with the AppID persisted, along with environment configuration issues and the incorrect use of Amplify Gen 1.

Errors Encountered:

Amplify Gen 2 Problem: The initialization process does not work correctly for Amplify Gen 2 and keeps defaulting to Gen 1. AppID Not Found: Amplify fails to locate the AppID during deployment attempts. Git Conflicts: Initial issues with syncing the local repository to the remote. Region Issues: Possible mismatch between the region configured in AWS CLI (us-east-1) and the region where Amplify resources are hosted.

Objective: The goal is to correctly set up the development and deployment environment using AWS Amplify Gen 2, ensuring that the local project is synchronized with GitHub and that the deployment to AWS occurs without errors, resulting in a functional application hosted on AWS.

1 Answer
0

To integrate Amazon Cognito and AWS Amplify into your HTML/CSS landing page, you'll need to make some adjustments to your current setup and approach. Here's a revised process to help you achieve your objective:

  1. Amplify CLI Setup: Ensure you have the latest version of the Amplify CLI installed. Run npm install -g @aws-amplify/cli to update if necessary.

  2. Project Initialization: Instead of trying to link to an existing AppID, start fresh with a new Amplify project. In your project directory, run amplify init and follow the prompts. When asked about the type of app you're building, select "javascript" and then "react" (even though you're using HTML/CSS, this will set up the necessary configurations).

  3. Adding Authentication: After initialization, add authentication to your project by running amplify add auth. Choose the default configuration for a quick setup, or customize as needed.

  4. Amplify Configuration: In your HTML file, add the following script tags to include the Amplify library:

<script src="https://cdn.jsdelivr.net/npm/aws-amplify/dist/aws-amplify.min.js"></script>

Then, in a separate JavaScript file or within a <script> tag in your HTML, configure Amplify:

Amplify.configure({
Auth: {
region: 'us-east-1', // your region
userPoolId: 'YOUR_USER_POOL_ID', // you'll get this after running amplify push
userPoolWebClientId: 'YOUR_USER_POOL_WEB_CLIENT_ID' // you'll get this after running amplify push
}
});
  1. Implementing Authentication: Use Amplify's authentication methods in your JavaScript code. For example:
// To sign up a user
Auth.signUp({ username, password, attributes: { email } })
.then(data => console.log(data))
.catch(err => console.log(err));

// To sign in a user
Auth.signIn(username, password)
.then(user => console.log(user))
.catch(err => console.log(err));
  1. Deploying Your Project: Run amplify push to provision the backend resources in AWS. This will create the necessary Cognito User Pool and Identity Pool.

  2. Hosting: If you want to host your static site with Amplify, run amplify add hosting and choose the hosting option that suits your needs.

  3. Version Control: Commit your changes to Git and push to your GitHub repository. Make sure to include the amplify directory in your repository, but exclude the amplify/team-provider-info.json file (add it to .gitignore).

  4. Continuous Deployment: In the Amplify Console, connect your GitHub repository and set up continuous deployment. This will automatically deploy your site when you push changes to your repository.

By following these steps, you should be able to successfully integrate Cognito authentication into your HTML/CSS landing page using Amplify, and deploy it to AWS. Remember to replace placeholder values (like region, userPoolId, and userPoolWebClientId) with your actual values after running amplify push.

If you encounter any specific errors during this process, make sure to check the Amplify CLI version, ensure your AWS credentials are correctly set up, and verify that you're in the correct directory when running Amplify commands.
Sources
Managing the hosted UI and authorization server - Amazon Cognito
Integrating Amazon Cognito authentication and authorization with web and mobile apps - Amazon Cognito
Create a React app by using AWS Amplify and add authentication with Amazon Cognito - AWS Prescriptive Guidance
Authenticate existing React application users by using Amazon Cognito and AWS Amplify UI - AWS Prescriptive Guidance

profile picture
answered 2 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions