Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting undefined currentUserSession while running runWithAmplifyServerContext on server side #13767

Closed
3 tasks done
prabhat-sunai opened this issue Aug 29, 2024 · 4 comments
Closed
3 tasks done
Assignees
Labels
Auth Related to Auth components/category Next.js question General question

Comments

@prabhat-sunai
Copy link

prabhat-sunai commented Aug 29, 2024

Before opening, please confirm:

JavaScript Framework

Next.js

Amplify APIs

Authentication

Amplify Version

v6

Amplify Categories

auth

Backend

None

Environment information

System:
    OS: Windows 11 10.0.22631
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i5-1155G7 @ 2.50GHz
    Memory: 4.80 GB / 15.76 GB
  Binaries:
    Node: 20.15.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.7.0 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (127.0.2651.74)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    0no-co-graphqlsp-api:  0.0.0
    @0no-co/graphqlsp: ^1.10.0 => 1.12.10
    @ampproject/toolbox-optimizer:  undefined ()
    @apollo/client: ^3.10.8 => 3.10.8
    @apollo/client/cache:  undefined ()
    @apollo/client/core:  undefined ()
    @apollo/client/dev:  undefined ()
    @apollo/client/errors:  undefined ()
    @apollo/client/link/batch:  undefined ()
    @apollo/client/link/batch-http:  undefined ()
    @apollo/client/link/context:  undefined ()
    @apollo/client/link/core:  undefined ()
    @apollo/client/link/error:  undefined ()
    @apollo/client/link/http:  undefined ()
    @apollo/client/link/persisted-queries:  undefined ()
    @apollo/client/link/remove-typename:  undefined ()
    @apollo/client/link/retry:  undefined ()
    @apollo/client/link/schema:  undefined ()
    @apollo/client/link/subscriptions:  undefined ()
    @apollo/client/link/utils:  undefined ()
    @apollo/client/link/ws:  undefined ()
    @apollo/client/react:  undefined ()
    @apollo/client/react/components:  undefined ()
    @apollo/client/react/context:  undefined ()
    @apollo/client/react/hoc:  undefined ()
    @apollo/client/react/hooks:  undefined ()
    @apollo/client/react/internal:  undefined ()
    @apollo/client/react/parser:  undefined ()
    @apollo/client/react/ssr:  undefined ()
    @apollo/client/testing:  undefined ()
    @apollo/client/testing/core:  undefined ()
    @apollo/client/testing/experimental:  undefined ()
    @apollo/client/utilities:  undefined ()
    @apollo/client/utilities/globals:  undefined ()
    @apollo/client/utilities/subscriptions/relay:  undefined ()
    @apollo/client/utilities/subscriptions/urql:  undefined ()
    @apollo/experimental-nextjs-app-support: ^0.11.2 => 0.11.2
    @aws-amplify/adapter-nextjs: ^1.2.4 => 1.2.6
    @aws-amplify/adapter-nextjs/api:  undefined ()
    @aws-amplify/adapter-nextjs/data:  undefined ()
    @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
    @eslint/js: ^9.6.0 => 9.6.0 (8.57.0)
    @googlemaps/js-api-loader: ^1.16.6 => 1.16.6 (1.16.2)
    @hapi/accept:  undefined ()
    @hookform/resolvers: ^3.3.4 => 3.9.0
    @hookform/resolvers/ajv:  1.0.0
    @hookform/resolvers/arktype:  2.0.0
    @hookform/resolvers/class-validator:  1.0.0
    @hookform/resolvers/computed-types:  1.0.0
    @hookform/resolvers/effect-ts:  1.0.0
    @hookform/resolvers/fluentvalidation-ts:  1.0.0
    @hookform/resolvers/io-ts:  1.0.0
    @hookform/resolvers/joi:  1.0.0
    @hookform/resolvers/nope:  1.0.0
    @hookform/resolvers/superstruct:  1.0.0
    @hookform/resolvers/typanion:  1.0.0
    @hookform/resolvers/typebox:  1.0.0
    @hookform/resolvers/typeschema:  1.0.0
    @hookform/resolvers/valibot:  1.0.0
    @hookform/resolvers/vest:  1.0.0
    @hookform/resolvers/vine:  1.0.0
    @hookform/resolvers/yup:  1.0.0
    @hookform/resolvers/zod:  1.0.0
    @ianvs/prettier-plugin-sort-imports: ^4.3.0 => 4.3.0
    @mswjs/interceptors:  undefined ()
    @napi-rs/triples:  undefined ()
    @next/bundle-analyzer: ^14.1.0 => 14.2.4
    @next/eslint-plugin-next: ^14.2.4 => 14.2.4 (14.1.0)
    @next/font:  undefined ()
    @next/third-parties: ^14.2.1 => 14.2.4
    @nivo/line: ^0.87.0 => 0.87.0
    @opentelemetry/api:  undefined ()
    @react-google-maps/api: ^2.19.3 => 2.19.3
    @t3-oss/env-nextjs: ^0.10.1 => 0.10.1
    @tanstack/react-table: ^8.17.3 => 8.19.2
    @types/eslint__js: ^8.42.3 => 8.42.3
    @types/google.maps: ^3.55.11 => 3.55.11 (3.55.2)
    @types/node: ^20.12.7 => 20.14.9
    @types/react: ^18.2.78 => 18.3.3
    @types/react-dom: ^18.2.25 => 18.3.0
    @vercel/nft:  undefined ()
    @vercel/og:  0.6.2
    @yudiel/react-qr-scanner: ^2.0.4 => 2.0.4
    acorn:  undefined ()
    amphtml-validator:  undefined ()
    anser:  undefined ()
    arg:  undefined ()
    assert:  undefined ()
    async-retry:  undefined ()
    async-sema:  undefined ()
    autoprefixer: ^10.4.19 => 10.4.19
    aws-amplify: ^6.5.3 => 6.5.3
    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/data:  undefined ()
    aws-amplify/data/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 ()
    babel-packages:  undefined ()
    browserify-zlib:  undefined ()
    browserslist:  undefined ()
    buffer:  undefined ()
    bytes:  undefined ()
    ci-info:  undefined ()
    cli-select:  undefined ()
    client-only:  0.0.1
    commander:  undefined ()
    comment-json:  undefined ()
    compression:  undefined ()
    conf:  undefined ()
    constants-browserify:  undefined ()
    content-disposition:  undefined ()
    content-type:  undefined ()
    cookie:  undefined ()
    cross-spawn:  undefined ()
    crypto-browserify:  undefined ()
    css.escape:  undefined ()
    data-uri-to-buffer:  undefined ()
    date-fns: ^3.6.0 => 3.6.0
    debug:  undefined ()
    devalue:  undefined ()
    domain-browser:  undefined ()
    edge-runtime:  undefined ()
    embla-carousel-react: ^8.0.4 => 8.1.6
    eslint: ^8.57.0 => 8.57.0
    eslint-config-next: 14.1.0 => 14.1.0
    eslint-config-prettier: ^9.1.0 => 9.1.0
    eslint-plugin-import: ^2.29.1 => 2.29.1
    eslint-plugin-prettier: ^5.1.3 => 5.1.3
    events:  undefined ()
    find-cache-dir:  undefined ()
    find-up:  undefined ()
    fresh:  undefined ()
    get-orientation:  undefined ()
    glob:  undefined ()
    gql-tada-cli:  0.0.0
    gql-tada-ts-plugin:  0.0.0
    gql.tada: ^1.6.3 => 1.8.0
    graphql: ^16.8.1 => 16.9.0 (15.8.0)
    gzip-size:  undefined ()
    http-proxy:  undefined ()
    http-proxy-agent:  undefined ()
    https-browserify:  undefined ()
    https-proxy-agent:  undefined ()
    husky: ^9.0.11 => 9.0.11
    icss-utils:  undefined ()
    ignore-loader: ^0.1.2 => undefined (0.1.2, )
    image-size:  undefined ()
    is-animated:  undefined ()
    is-docker:  undefined ()
    is-wsl:  undefined ()
    jest-worker:  undefined ()
    jiti: ^1.21.6 => 1.21.6
    json5:  undefined ()
    jsonwebtoken:  undefined ()
    lint-staged: ^15.2.2 => 15.2.7
    loader-runner:  undefined ()
    loader-utils:  undefined ()
    lodash.curry:  undefined ()
    lru-cache:  undefined ()
    lucide-react: ^0.408.0 => 0.408.0
    mini-css-extract-plugin:  undefined ()
    nanoid:  undefined ()
    native-url:  undefined ()
    neo-async:  undefined ()
    next: ^14.2.3 => 14.2.4
    next-themes: ^0.2.1 => 0.2.1 (0.3.0)
    node-fetch:  undefined ()
    node-html-parser:  undefined ()
    ora:  undefined ()
    os-browserify:  undefined ()
    p-limit:  undefined ()
    path-browserify:  undefined ()
    picomatch:  undefined ()
    pino: ^8.20.0 => 8.21.0 (6.14.0)
    pino-logflare: ^0.4.2 => 0.4.2
    platform:  undefined ()
    postcss: ^8.4.38 => 8.4.39 (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 ()
    prettier: 3.2.5 => 3.2.5
    prettier-plugin-tailwindcss: ^0.5.13 => 0.5.14
    process:  undefined ()
    punycode:  undefined ()
    querystring-es3:  undefined ()
    raw-body:  undefined ()
    react: ^18 => 18.3.1
    react-builtin:  undefined ()
    react-dom: ^18 => 18.3.1
    react-dom-builtin:  undefined ()
    react-dom-experimental-builtin:  undefined ()
    react-experimental-builtin:  undefined ()
    react-hook-form: ^7.50.1 => 7.52.1
    react-is:  18.2.0
    react-refresh:  0.12.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-virtualized-auto-sizer: ^1.0.24 => 1.0.24
    regenerator-runtime:  0.13.4
    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 ()
    sonner: ^1.5.0 => 1.5.0
    source-map:  undefined ()
    source-map08:  undefined ()
    stacktrace-parser:  undefined ()
    stream-browserify:  undefined ()
    stream-http:  undefined ()
    string-hash:  undefined ()
    string_decoder:  undefined ()
    strip-ansi:  undefined ()
    superstruct:  undefined ()
    tailwindcss: ^3.4.3 => 3.4.4
    tar:  undefined ()
    terser:  undefined ()
    text-table:  undefined ()
    timers-browserify:  undefined ()
    transport:  0.0.1
    tty-browserify:  undefined ()
    typescript: ^5.5.3 => 5.5.3
    typescript-eslint: ^7.15.0 => 7.15.0
    ua-parser-js:  undefined ()
    unistore:  undefined ()
    use-debounce: ^10.0.1 => 10.0.1
    util:  undefined ()
    vm-browserify:  undefined ()
    watchpack:  undefined ()
    web-vitals:  undefined ()
    webpack:  undefined ()
    webpack-sources:  undefined ()
    ws:  undefined ()
    zod: ^3.22.4 => 3.23.8 ()
    zustand: ^4.5.2 => 4.5.4
  npmGlobalPackages:
    @aws-amplify/cli: 12.12.6
    corepack: 0.28.1
    npm: 10.7.0



Describe the bug

While running runWithAmplifyServerContext on the server side, it doesn't provide me with the current user session, even when the user is logged in. However, the code works fine when deployed but the issue occurs on my localhost.

async function getSession() {
  try {
    const currentUserSession = await runWithAmplifyServerContext({
      nextServerContext: { cookies },
      operation: async (contextSpec) => {
        return (await fetchAuthSession(contextSpec)).tokens
      }
    });
    console.log(currentUserSession)
    return currentUserSession;
  } catch (error) {
    logger.error(error, "Session Fetch Error on Server Component");
    return {};
  }
}

Expected behavior

It should give user access token and id token

{
  accessToken: {
    toString: [Function: toString],
    payload: {
      sub: xxxx,
      iss: xxxx,
      client_id: xxxx,
      origin_jti: xxxx,
      event_id: xxxx,
      token_use: 'access',
      scope: xxxx,
      auth_time: xxxx,
      exp: xxxx,
      iat: xxxx,
      jti: xxxx,
      username: xxxx
    }
  },
  idToken: {
    toString: [Function: toString],
    payload: {
      sub: xxxx,
      email_verified: true,
      iss: xxxx,
      'cognito:username': xxxx
      origin_jti: xxxx,
      aud: xxxx,
      event_id: xxxx,
      token_use: 'id',
      auth_time: xxxx
      exp: xxxx,
      iat: xxxx,
      jti: xxxx,
      email: xxxx
    }
  },
  signInDetails: { loginId: xxxx, authFlowType: xxxx }
}

Reproduction steps

add this code to server side function

npm run dev

Code Snippet

// Put your code below this line.

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

@prabhat-sunai prabhat-sunai added the pending-triage Issue is pending triage label Aug 29, 2024
@cwomack cwomack added Auth Related to Auth components/category Next.js labels Aug 29, 2024
@cwomack cwomack self-assigned this Aug 29, 2024
@HuiSF
Copy link
Member

HuiSF commented Aug 30, 2024

Hi @prabhat-sunai thank you for opening this issue.

Since you can only reproduce this issue locally, could you do the following for further investigation?

  • Open the developer tool, and inspect whether the cookie store contain the auth tokens after signing in an end user
  • If the auth tokens present in the cookie store, could you inspect the network requests that were sending to your local Next.js dev server contains a cookie header that includes the auth tokens you saw in the cookie store?

Also are you using Safari for testing locally?

@cwomack cwomack added question General question pending-response and removed pending-triage Issue is pending triage labels Aug 30, 2024
@prabhat-sunai
Copy link
Author

prabhat-sunai commented Sep 2, 2024

The cookie store contains auth tokens after signing in and it is also present in network requests that were sent to local Next JS server. Also, the client side query is fine and access token is being shown in client side query. I also checked the cookie object from next/header and all the tokens are shown there. I am using Google Chrome for testing locally

@HuiSF
Copy link
Member

HuiSF commented Sep 5, 2024

Thanks for providing more information @prabhat-sunai I have no clue what's happening here reading your description if the cookies presents in all the locations.

When you are saying:

it doesn't provide me with the current user session, even when the user is logged in

Did fetchAuthSession() call throw, or it returned undefined for .tokens? If you log the ensure result returned by the API call, what did it look like?

Also, is it possible to create a minimal reproducing app so I can also help inspect?

@github-actions github-actions bot added the pending-maintainer-response Issue is pending a response from the Amplify team. label Sep 5, 2024
@HuiSF HuiSF removed the pending-maintainer-response Issue is pending a response from the Amplify team. label Sep 5, 2024
@cwomack cwomack added pending-response pending-community-response Issue is pending a response from the author or community. and removed pending-response labels Sep 9, 2024
@cwomack
Copy link
Member

cwomack commented Oct 22, 2024

Closing this issue as we have not heard back from you. If you are still experiencing this, please feel free to reply back and provide any information previously requested and we'd be happy to re-open the issue.

Thank you!

@cwomack cwomack closed this as not planned Won't fix, can't repro, duplicate, stale Oct 22, 2024
@github-actions github-actions bot removed the pending-community-response Issue is pending a response from the author or community. label Oct 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auth Related to Auth components/category Next.js question General question
Projects
None yet
Development

No branches or pull requests

3 participants