aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

fetchAuthSession throws [TooManyRequestsException: Rate exceeded] #13608

Closed Mohammed-Fayaz closed 2 months ago

Mohammed-Fayaz commented 3 months ago

Before opening, please confirm:

JavaScript Framework

Next.js

Amplify APIs

REST API

Amplify Version

v6

Amplify Categories

api

Backend

Amplify Gen 2 (Preview)

Environment information

``` # Put output below this line System: OS: macOS 14.5 CPU: (8) x64 Apple M2 Memory: 27.26 MB / 8.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.9.0 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 10.1.0 - /usr/local/bin/npm pnpm: 8.14.3 - ~/Library/pnpm/pnpm bun: 1.0.7 - /usr/local/bin/bun Browsers: Chrome: 126.0.6478.182 Safari: 17.5 npmPackages: %name%: 0.1.0 @ampproject/toolbox-optimizer: undefined () @aws-amplify/adapter-nextjs: ^1.0.17 => 1.0.17 @aws-amplify/adapter-nextjs/api: undefined () @aws-amplify/adapter-nextjs/data: undefined () @aws-amplify/backend: 0.12.1 => 0.12.1 @aws-amplify/backend-cli: 0.11.1 => 0.11.1 @aws-amplify/ui-react: ^6.1.0 => 6.1.0 @aws-amplify/ui-react-internal: undefined () @aws-sdk/client-cognito-identity-provider: ^3.521.0 => 3.521.0 @aws-sdk/client-s3: ^3.574.0 => 3.574.0 @aws-sdk/s3-request-presigner: ^3.574.0 => 3.574.0 @babel/core: undefined () @babel/runtime: 7.22.5 @edge-runtime/cookies: 4.1.1 @edge-runtime/ponyfill: 2.4.2 @edge-runtime/primitives: 4.1.0 @hapi/accept: undefined () @heathmont/moon-core-tw: ^10.13.2 => 10.13.3 @heathmont/moon-table-tw: ^10.13.3 => 10.13.3 @heathmont/moon-themes-tw: ^10.13.2 => 10.13.2 @mswjs/interceptors: undefined () @napi-rs/triples: undefined () @next/font: undefined () @openapitools/openapi-generator-cli: ^2.13.1 => 2.13.1 @opentelemetry/api: undefined () @radix-ui/react-slot: ^1.0.2 => 1.0.2 (1.0.0) @radix-ui/react-tabs: ^1.0.4 => 1.0.4 @react-hook/window-size: ^3.1.1 => 3.1.1 @react-hook/window-size-throttled: 2.2.0 @tanstack/query-codemods: 4.24.3 @tanstack/react-query: ^5.37.1 => 5.37.1 @trpc/client: ^11.0.0-rc.374 => 11.0.0-rc.374+5027209bc @trpc/next: ^11.0.0-rc.374 => 11.0.0-rc.374+5027209bc @trpc/react-query: ^11.0.0-rc.374 => 11.0.0-rc.374+5027209bc @trpc/server: ^11.0.0-rc.374 => 11.0.0-rc.374+5027209bc @types/file-saver: ^2.0.7 => 2.0.7 @types/formidable: ^3.4.5 => 3.4.5 @types/json2csv: ^5.0.7 => 5.0.7 @types/node: ^20 => 20.10.8 (18.19.39) @types/pg: ^8.11.2 => 8.11.2 @types/react: ^18 => 18.2.47 @types/react-dom: ^18 => 18.2.18 @vercel/nft: undefined () @vercel/og: 0.6.2 acorn: undefined () amphtml-validator: undefined () anser: undefined () arg: undefined () assert: undefined () async-retry: undefined () async-sema: undefined () autoprefixer: ^10.4.16 => 10.4.16 aws-amplify: ^6.0.10 => 6.0.10 aws-amplify/adapter-core: undefined () aws-amplify/analytics: undefined () aws-amplify/analytics/kinesis: undefined () aws-amplify/analytics/kinesis-firehose: undefined () aws-amplify/analytics/personalize: undefined () aws-amplify/analytics/pinpoint: undefined () aws-amplify/api: undefined () aws-amplify/api/server: undefined () aws-amplify/auth: undefined () aws-amplify/auth/cognito: undefined () aws-amplify/auth/cognito/server: undefined () aws-amplify/auth/enable-oauth-listener: undefined () aws-amplify/auth/server: undefined () aws-amplify/datastore: undefined () aws-amplify/in-app-messaging: undefined () aws-amplify/in-app-messaging/pinpoint: undefined () aws-amplify/push-notifications: undefined () aws-amplify/push-notifications/pinpoint: undefined () aws-amplify/storage: undefined () aws-amplify/storage/s3: undefined () aws-amplify/storage/s3/server: undefined () aws-amplify/storage/server: undefined () aws-amplify/utils: undefined () aws-cdk: ^2.110.1 => 2.132.1 aws-cdk-lib: ^2.110.1 => 2.132.1 babel-packages: undefined () browserify-zlib: undefined () browserslist: undefined () buffer: undefined () bytes: undefined () ci-info: undefined () class-variance-authority: ^0.7.0 => 0.7.0 cli-select: undefined () client-only: 0.0.1 clsx: ^2.1.0 => 2.1.0 (2.0.0) commander: undefined () comment-json: undefined () compression: undefined () conf: undefined () constants-browserify: undefined () constructs: ^10.3.0 => 10.3.0 content-disposition: undefined () content-type: undefined () cookie: undefined () cross-spawn: undefined () crypto-browserify: undefined () css.escape: undefined () csv-parse: ^5.5.5 => 5.5.5 data-uri-to-buffer: undefined () date-fns: ^3.6.0 => 3.6.0 (2.30.0) debug: undefined () devalue: undefined () domain-browser: undefined () drizzle-kit: ^0.21.1 => 0.21.1 drizzle-orm: ^0.29.4 => 0.29.4 edge-runtime: undefined () eslint: ^8 => 8.57.0 eslint-config-next: 14.0.4 => 14.0.4 eslint-plugin-react-hooks: ^4.6.2 => 4.6.2 events: undefined () file-saver: ^2.0.5 => 2.0.5 find-cache-dir: undefined () find-up: undefined () formidable: ^3.5.1 => 3.5.1 fresh: undefined () get-orientation: undefined () glob: undefined () gzip-size: undefined () http-proxy: undefined () http-proxy-agent: undefined () https-browserify: undefined () https-proxy-agent: undefined () icss-utils: undefined () ignore-loader: undefined () image-size: undefined () is-animated: undefined () is-docker: undefined () is-wsl: undefined () jest-worker: undefined () json2csv: ^6.0.0-alpha.2 => 6.0.0-alpha.2 json5: undefined () jsonwebtoken: undefined () loader-runner: undefined () loader-utils: undefined () lodash.curry: undefined () lru-cache: undefined () lucide: ^0.311.0 => 0.311.0 lucide-react: ^0.378.0 => 0.378.0 mini-css-extract-plugin: undefined () nanoid: undefined () native-url: undefined () neo-async: undefined () next: ^14.2.4 => 14.2.4 node-fetch: undefined () node-html-parser: undefined () ora: undefined () os-browserify: undefined () p-limit: undefined () path-browserify: undefined () pg: ^8.11.3 => 8.11.3 picomatch: undefined () platform: undefined () postcss: ^8.4.33 => 8.4.33 (8.4.31) postcss-flexbugs-fixes: undefined () postcss-modules-extract-imports: undefined () postcss-modules-local-by-default: undefined () postcss-modules-scope: undefined () postcss-modules-values: undefined () postcss-preset-env: undefined () postcss-safe-parser: undefined () postcss-scss: undefined () postcss-value-parser: undefined () posthog-js: ^1.142.1 => 1.142.1 posthog-js-react: 1.0.0 pptxgenjs: ^3.12.0 => 3.12.0 prettier: ^3.2.5 => 3.2.5 (2.3.2, 2.8.8, 1.19.1, 3.0.3) prettier-plugin-tailwindcss: ^0.5.12 => 0.5.12 process: undefined () punycode: undefined () querystring-es3: undefined () raw-body: undefined () react: ^18 => 18.2.0 react-builtin: undefined () react-dom: ^18 => 18.2.0 react-dom-builtin: undefined () react-dom-experimental-builtin: undefined () react-experimental-builtin: undefined () react-is: 18.2.0 react-refresh: 0.12.0 react-select: ^5.8.0 => 5.8.0 react-server-dom-turbopack-builtin: undefined () react-server-dom-turbopack-experimental-builtin: undefined () react-server-dom-webpack-builtin: undefined () react-server-dom-webpack-experimental-builtin: undefined () react-spinners: ^0.13.8 => 0.13.8 readable-numbers: ^1.0.8 => 1.0.8 recharts: ^2.12.4 => 2.12.4 recharts-to-png: ^2.3.2 => 2.3.2 regenerator-runtime: 0.13.4 sass: ^1.71.1 => 1.71.1 sass-loader: undefined () scheduler-builtin: undefined () scheduler-experimental-builtin: undefined () schema-utils: undefined () semver: undefined () send: undefined () server-only: 0.0.1 setimmediate: undefined () shell-quote: undefined () source-map: undefined () source-map08: undefined () stacktrace-parser: undefined () stream-browserify: undefined () stream-http: undefined () string-hash: undefined () string_decoder: undefined () strip-ansi: undefined () superjson: ^2.2.1 => 2.2.1 superstruct: undefined () tailwind-merge: ^2.2.0 => 2.2.0 (1.14.0) tailwindcss: ^3.4.1 => 3.4.1 tailwindcss-animate: ^1.0.7 => 1.0.7 tar: undefined () terser: undefined () text-table: undefined () timers-browserify: undefined () ts-to-zod: ^3.8.5 => 3.8.5 tty-browserify: undefined () typescript: ^5.3.3 => 5.3.3 (5.4.5, 4.4.4) ua-parser-js: undefined () unistore: undefined () util: undefined () vm-browserify: undefined () watchpack: undefined () web-vitals: undefined () webpack: undefined () webpack-sources: undefined () ws: undefined () zod: ^3.23.8 => 3.23.8 (3.22.4, ) npmGlobalPackages: bun: 1.0.7 corepack: 0.20.0 npm: 10.1.0 yarn: 1.22.19 ```

Describe the bug

Getting this error from the middleware in next.js

[TooManyRequestsException: Rate exceeded] { name: 'TooManyRequestsException', $metadata: { attempts: 1, httpStatusCode: 400, requestId: 'e8c4a2b8-a9f6-4814-8a2b-1d0ddc3ac6fa', extendedRequestId: undefined, cfId: undefined }

Expected behavior

Should not be rate limited

Reproduction steps

Set up aws-amplify using the gen 2 docs for next.js (middleware setup from here - https://docs.amplify.aws/nextjs/build-a-backend/server-side-rendering/nextjs-app-router-server-components/) Middleware will throw rate-limit errors sporadically.

Code Snippet

// Put your code below this line.
// middleware.ts
import { NextRequest, NextResponse } from "next/server";

import { fetchAuthSession } from "aws-amplify/auth/server";

import { runWithAmplifyServerContext } from "@/utils/amplify-utils";

export async function middleware(request: NextRequest) {
  const response = NextResponse.next();

  const authenticated = await runWithAmplifyServerContext({
    nextServerContext: { request, response },
    operation: async (contextSpec) => {
      try {
        const session = await fetchAuthSession(contextSpec, {});
        return session.tokens !== undefined;
      } catch (error) {
        console.log("authError", error);
        return false;
      }
    },
  });

  if (authenticated) {
    return response;
  }

  return NextResponse.redirect(new URL("/login", request.url));
}

export const config = {
  matcher: [
    /*
     * Match all request paths except for the ones starting with:
     * - api (API routes)
     * - _next/static (static files)
     * - _next/image (image optimization files)
     * - favicon.ico (favicon file)
     * - login
     */
    "/((?!api|_next/static|_next/image|favicon.ico|login).*)",
  ],
};

Log output

``` // Put your logs below this line ```

aws-exports.js

No response

Manual configuration

No response

Additional configuration

No response

Mobile Device

No response

Mobile Operating System

No response

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

chrisbonifacio commented 3 months ago

Hi @Mohammed-Fayaz 👋 thanks for raising this issue. This sounds very similar to another issue that was opened not too long ago regarding hitting the rate limit for calls to Cognito. I would recommend checking out this other issue, particularly this comment. You may be able to request a request quota increase.

cwomack commented 2 months ago

Closing this issue as we have not heard back from you. If you are still experiencing this, please review this comment in the related (but now closed/resolved) issue or see if a quota increase helps resolve the problem. If either of those don't help, feel free to reply back and provide any relevant information/errors and we'd be happy to re-open the issue.

Thank you!