IT BLOG HTTP://TWITTER.COM/WHOLICS SINCE 2009

  • If you are getting AccessDeniedException

    An error occurred (AccessDeniedException) when calling the CreateUserPoolDomain operation: User: arn:aws:iam::<aws_account_id>:user/amplify-dev is not authorized to perform: cognito-idp:CreateUserPoolDomain on resource: arn:aws:cognito-idp:us-west-2:<aws_account_id>:userpool/us-west-2_EHNXjvL37 because no identity-based policy allows the cognito-idp:CreateUserPoolDomain action

    The error message you’re seeing indicates that the IAM user amplify-dev doesn’t have the necessary permissions to execute the CreateUserPoolDomain operation in AWS Cognito. To resolve this issue, you’ll need to modify the IAM policy associated with this user to include the necessary permissions.

    Here are the steps to do this:

    1. Go to the IAM section of the AWS Management Console.
    2. In the left navigation pane, click on “Users”.
    3. Find and click on the amplify-dev user.
    4. Under the “Permissions” tab, look for the “Add inline policy” button on the right and click it.
    5. In the “Create policy” interface, switch to the “JSON” tab and enter the following policy:
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "CognitoPermissions",
          "Effect": "Allow",
          "Action": [
            "cognito-idp:CreateUserPoolDomain",
            "cognito-idp:DescribeUserPoolDomain",
            "cognito-idp:DeleteUserPoolDomain"
          ],
          "Resource": "*"
        }
      ]
    }
    
    1. Click on “Review policy”.
    2. Enter a name for the policy and click “Create policy”.

    This policy grants the amplify-dev user the permissions to create, describe, and delete user pool domains in AWS Cognito.

    Please note that the "Resource": "*" in the policy allows these actions on all resources. If you want to restrict this to specific user pools, replace "*" with the ARNs of the user pools.

    After you’ve added the policy, try running your AWS CLI command again. If you’re still encountering issues, make sure your AWS CLI is configured with the correct credentials for the amplify-dev user.

    Or do it via awscli:

    To create and attach an IAM policy using the AWS CLI, you’ll need to follow these steps:

    1. Create the policy: Save the following policy document as a JSON file, such as cognito_permissions.json:
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Sid": "CognitoPermissions",
          "Effect": "Allow",
          "Action": [
            "cognito-idp:CreateUserPoolDomain",
            "cognito-idp:DescribeUserPoolDomain",
            "cognito-idp:DeleteUserPoolDomain"
          ],
          "Resource": "*"
        }
      ]
    }
    
    1. Create the policy using AWS CLI: Use the create-policy command to create the policy:
    aws iam create-policy --policy-name CognitoPermissionsPolicy --policy-document file://cognito_permissions.json
    

    This command will return a policy ARN (Amazon Resource Name) in the output.

    1. Attach the policy to the user: To attach this policy to your user (amplify-dev), use the attach-user-policy command with the policy ARN from the previous step:
    aws iam attach-user-policy --policy-arn "arn:aws:iam::aws:policy/CognitoPermissionsPolicy" --user-name amplify-dev
    

    Now, your amplify-dev user should have the necessary permissions to manage Cognito user pool domains. Make sure to replace the policy ARN and user name with your actual values.

    Also, please note that this policy allows these actions on all resources. If you want to restrict this to specific user pools, replace "*" with the ARNs of the user pools in the policy document.

    ¶¶¶¶¶

    ¶¶¶¶¶

    ¶¶¶¶¶

    ¶¶¶¶¶

    ¶¶¶¶¶

  • Step 6D: setup domain name app integration on aws Cognito via aws cli

    Let’s walk through setting up a domain name for your AWS Cognito User Pool. This domain name will be used for hosting the built-in sign-in and sign-up pages.

    1. Create a User Pool Domain: To create a domain for your user pool, you can use the create-user-pool-domain command.
       aws cognito-idp create-user-pool-domain --user-pool-id YOUR_USER_POOL_ID --domain YOUR_DOMAIN

    Replace YOUR_USER_POOL_ID with your user pool ID and YOUR_DOMAIN with your chosen domain.Note: The domain must be unique across all Cognito User Pools in a region.

    1. Verify the Domain: You can verify that your domain was created by using the describe-user-pool-domain command.
    2. aws cognito-idp describe-user-pool-domain –domain YOUR_DOMAIN

    Replace YOUR_DOMAIN with your chosen domain.You now have a domain for your Cognito User Pool that can be used for the hosted UI.

    Your hosted UI will be available at this URL: https://YOUR_DOMAIN.auth.YOUR_REGION.amazoncognito.com/For example, if your domain is myapp and your region is us-west-2, your hosted UI will be at https://myapp.auth.us-west-2.amazoncognito.com/

    You can customize the look and feel of your hosted UI in the AWS Management Console by navigating to your User Pool, selecting “App integration” > “UI customization”.

    Remember, the domain you choose will be public, so choose something that represents your app or organization.

    Please note that setting up a custom domain (a domain that you own, like auth.yourwebsite.com) involves more steps, including verifying domain ownership and setting up an SSL certificate. This can’t be done through the AWS CLI and requires using the AWS Management Console or AWS SDKs.

    ¶¶¶¶¶

    ¶¶¶¶¶

    ¶¶¶¶¶

    ¶¶¶¶¶

    ¶¶¶¶¶

  • Implement Authentication and Authorization for react typescript app and backend.

    Implementing authentication and authorization in a React TypeScript app involves a series of steps. For this guide, I’ll use AWS Cognito for user authentication, and JWT (JSON Web Tokens) for authorization. AWS Cognito is a user identity service, and it has built-in integrations for a React app.

    Set Up AWS Cognito:

    1. Create a User Pool in AWS Cognito: This will handle user registration, authentication, and account recovery.
    2. Create an App client in the User Pool: This will be used by your React app to communicate with Cognito.
    3. Note down the Pool Id and App client id: You’ll need these in your React app.

    Set Up the React TypeScript App:

    1. Install necessary libraries: Use npm or yarn to install AWS Amplify and AWS Amplify UI React libraries.
    npm install aws-amplify @aws-amplify/ui-react
    # or
    yarn add aws-amplify @aws-amplify/ui-react
    
    1. Configure Amplify: In your main TypeScript file (usually index.tsx), import and configure Amplify with the Cognito Pool Id and App client id.
    import Amplify from 'aws-amplify';
    
    Amplify.configure({
      Auth: {
        region: 'your-region',
        userPoolId: 'your-user-pool-id',
        userPoolWebClientId: 'your-app-client-id',
      },
    });
    
    1. Add Authentication UI: In your main App component, use the withAuthenticator higher-order component from AWS Amplify UI to add a pre-built authentication flow.
    import React from 'react';
    import { withAuthenticator } from '@aws-amplify/ui-react';
    
    const App: React.FC = () => (
      <div>
        {/* Your app components */}
      </div>
    );
    
    export default withAuthenticator(App);
    

    Backend Authorization:

    1. Include JWT in requests: When a user logs in via Cognito in your React app, they receive JWT tokens. Include these tokens in the Authorization header when making requests to your backend.
    2. Verify JWT in the backend: On your backend, verify the JWT token in each request. AWS Cognito provides public keys you can use to verify the signature of the JWT. After verifying the JWT, extract the user’s identity from the JWT claims.
    3. Check user permissions: Once you have the user’s identity, check if they are authorized to perform the action they are requesting. You might store user roles or permissions in your database to help with this.

    Role-Based Authorization (Optional):

    If you want to implement role-based authorization:

    1. Use Cognito Groups: In Cognito, use Groups to represent different roles. When creating or updating a user, assign them to a group.
    2. Check user role in JWT: In your backend, after decoding the JWT token, check the user’s group (role) from the token claims. Based on the role, determine if the user is authorized to perform the action.

    Note:

    Remember to handle session expiration and renewal, sign out, and other edge cases in your app. Additionally, keep your npm packages up to date, as outdated packages may have security vulnerabilities. Regularly check your app for any security issues, and fix them promptly.

    ¶¶¶¶¶

    ¶¶¶¶¶

    ¶¶¶¶¶

    ¶¶¶¶¶

    ¶¶¶¶¶

Create your own website, free on WordPress.com.Create your own website.
Start free