chen
chen

Reputation: 4500

Can we use AWS amplify only for its react library (components) but not the backend cli?

We are trying to use amplify only for the react components in its library. But as we understand, Amplify is more of a serverless backend configuration tool amplify-cli, see https://aws-amplify.github.io/docs/js/react

Can we use our own backend configuration such as serverless.com

Upvotes: 1

Views: 350

Answers (1)

noetix
noetix

Reputation: 4933

Yes! Install the package using npm and follow the JavaScript documentation within your React app.

npm i aws-amplify --save

Here is my configuration to use Cognito and API Gateway:

import Amplify, { Auth } from 'aws-amplify'

Amplify.configure({
  Auth: {
    region: process.env.AWS_REGION,
    identityPoolId: process.env.IDENTITY_POOL_ID,
    userPoolId: process.env.USER_POOL_ID,
    userPoolWebClientId: process.env.USER_POOL_CLIENT_ID,
    mandatorySignIn: true
  },
  API: {
    endpoints: [
      {
        name: 'API',
        endpoint: process.env.API_URL,
        region: process.env.AWS_REGION,
        custom_header: async () => (
          { Authorization: `Bearer ${(await Auth.currentSession()).idToken.jwtToken}` }
        )
      }
    ]
  }
})

This lets you call your serverless backend endpoints like so:

import { API } from 'aws-amplify'

const data = await API.get('API', '/posts')

The JWT configuration allows your serverless endpoints to use the authorizer type COGNITO_USER_POOLS to authorize endpoint usage.

Upvotes: 3

Related Questions