aws-amplify / docs

AWS Amplify Framework Documentation
https://docs.amplify.aws
Apache License 2.0
482 stars 1.04k forks source link

Not possible to extend cli generated aws-export.js with cookieStorage config #5890

Closed quarryman closed 1 year ago

quarryman commented 1 year ago

Describe the bug Once amplify project is initialized with amplify cli, aws-export.js is autogenerated based on prompts you answer. File is clearly not meant for editing as it should remain under cli control and can be overriden.

Once one needs to add extra configuration for Auth module, like setting storage to cookies instead of localstorage, there is no clear option how to do it, as file is uneditable and CLI doesnt provide prompt to config this particular value when you add Auth module using 'amplify add auth'

Along with that authgenerated file structure is much different from one that is referenced in documentation

autogenerated file structure:


    "aws_project_region": "eu-west-1",
    "aws_cognito_region": "eu-west-1",
    "aws_user_pools_id": "xxxx",
    "aws_user_pools_web_client_id": "xxxx",
    "oauth": {},
    "aws_cognito_username_attributes": [
        "EMAIL"
    ],
    "aws_cognito_social_providers": [],
    "aws_cognito_signup_attributes": [
        "EMAIL"
    ],
    "aws_cognito_mfa_configuration": "OPTIONAL",
    "aws_cognito_mfa_types": [
        "SMS"
    ],
    "aws_cognito_password_protection_settings": {
        "passwordPolicyMinLength": 8,
        "passwordPolicyCharacters": []
    },
    "aws_cognito_verification_mechanisms": [
        "EMAIL"
    ]
};```

file structure referenced in docs:
```{
  Auth: {
    // REQUIRED only for Federated Authentication - Amazon Cognito Identity Pool ID
    identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab',

    // REQUIRED - Amazon Cognito Region
    region: 'XX-XXXX-X',

    // OPTIONAL - Amazon Cognito Federated Identity Pool Region
    // Required only if it's different from Amazon Cognito Region
    identityPoolRegion: 'XX-XXXX-X',

    // OPTIONAL - Amazon Cognito User Pool ID
    userPoolId: 'XX-XXXX-X_abcd1234',

    // OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
    userPoolWebClientId: 'a1b2c3d4e5f6g7h8i9j0k1l2m3',

    // OPTIONAL - Enforce user authentication prior to accessing AWS resources or not
    mandatorySignIn: false,

    // OPTIONAL - This is used when autoSignIn is enabled for Auth.signUp
    // 'code' is used for Auth.confirmSignUp, 'link' is used for email link verification
    signUpVerificationMethod: 'code', // 'code' | 'link'

    // OPTIONAL - Configuration for cookie storage
    // Note: if the secure flag is set to true, then the cookie transmission requires a secure protocol
    cookieStorage: {
      // REQUIRED - Cookie domain (only required if cookieStorage is provided)
      domain: '.yourdomain.com',
      // OPTIONAL - Cookie path
      path: '/',
      // OPTIONAL - Cookie expiration in days
      expires: 365,
      // OPTIONAL - See: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
      sameSite: 'strict' | 'lax',
      // OPTIONAL - Cookie secure flag
      // Either true or false, indicating if the cookie transmission requires a secure protocol (https).
      secure: true,
    },

    // OPTIONAL - customized storage object
    storage: MyStorage,

    // OPTIONAL - Manually set the authentication flow type. Default is 'USER_SRP_AUTH'
    authenticationFlowType: 'USER_PASSWORD_AUTH',

    // OPTIONAL - Manually set key value pairs that can be passed to Cognito Lambda Triggers
    clientMetadata: {myCustomKey: 'myCustomValue'},
  },```

What is a proper way to add cookieStorage config to authgenerated file using cli or any other approach?

**To Reproduce**
Steps to reproduce the behavior:
1. Initialize new amplify project
2. Add Auth module
3. invoke amplify push
4. Follow prompts and answer them
5. get aws-export generated file
6. Unclear how to add extra configuration like cookieStorage

**Expected behavior**
A way to add configuration using amplify-cli

**Screenshots**

**Desktop (please complete the following information):**
not related

**Smartphone (please complete the following information):**
not related

**Additional context**
josefaidt commented 1 year ago

Hey @quarryman :wave: thanks for raising this! Unfortunately we do not support extending the CLI-generated aws-exports.js file, however modifications can be applied when passing these exports to Amplify.configure:

import { Amplify } from 'aws-amplify'
import awsExports from './aws-exports'

Amplify.configure({
  ...awsExports,
  Auth: {
    cookieStorage: { /* some modifications */ }
  }
})
quarryman commented 1 year ago

Thanx, that solves my case completely.