question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API Gateway not receiving auth info when using withSSRContext

See original GitHub issue

Describe the bug

I have an nextjs app that uses Cognito and Amplify to login and out using the hosted pages (using <AmplifyAuthenticator usernameAlias=“email”>). I’m only using email login, I don’t have any federated logins

all works fine client side, I use Hub to listen for auth events and have no problem logging in and out, calling the API Gatway works fine and I receive the auth information on the backend (mapping context.identity.cognitoIdentityId).

When I try to render serverside though, I do not get the Auth information

for example, in the following I this always throws a No current user exception for currentAuthenticatedUser and no cognitoIdentityId gets passed through the API Gateway.

WinePage.getInitialProps = async (context) => {

  const {API, Auth} = withSSRContext(context)
  try {
    const currentUser = await Auth.currentAuthenticatedUser();
    console.log("logged in: " + currentUser.username)
  } catch (error) {
    console.log("not logged in")
  }

  const wineId = context.query.wineId;

  let apiName = 'vinotroveapi';
  let path = '/wine/' + wineId;
  try {
    let response = await API.get(apiName, path);

....

Expected behavior

From the examples, I was expecting that I would get the auth information and that information would be passed through to the API Gateway

Screenshots If applicable, add screenshots to help explain your problem.

What is Configured?

Amplify is configured manually

const awsconfig = {
  Auth: {
    // REQUIRED only for Federated Authentication - Amazon Cognito Identity Pool ID
    identityPoolId: 'us-east-1:xxxxxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',

    // REQUIRED - Amazon Cognito Region
    region: 'us-east-1',

    // OPTIONAL - Amazon Cognito User Pool ID
    userPoolId: 'us-east-XXXXXXX',

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

    // OPTIONAL - Enforce user authentication prior to accessing AWS resources or not
    mandatorySignIn: false,
  },
  API: {
    endpoints: [
      {
        name: 'vinotroveapi',
        endpoint: 'https://devgateway.vinotrove.com',
        region: 'us-east-1',
      },
    ]
  },
  Storage: {
    AWSS3: {
      bucket: XXXX,
      region: XXXX,
    }
  }
};

export default awsconfig;

and configured with

Amplify.configure({...awsconfig, ssr: true });
  • If applicable, provide more configuration data, for example for Amazon Cognito, run aws cognito-idp describe-user-pool --user-pool-id us-west-2_xxxxxx (Be sure to remove any sensitive data)
Environment
$ npx envinfo --system --binaries --browsers --npmPackages --npmGlobalPackages
npx: installed 1 in 1.448s

  System:
    OS: macOS 10.15.7
    CPU: (8) x64 Intel(R) Core(TM) i7-4850HQ CPU @ 2.30GHz
    Memory: 522.36 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 12.15.0 - ~/.nvm/versions/node/v12.15.0/bin/node
    Yarn: 1.21.1 - ~/.yarn/bin/yarn
    npm: 6.14.7 - ~/.nvm/versions/node/v12.15.0/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Browsers:
    Brave Browser: 71.0.57.18
    Chrome: 87.0.4280.67
    Firefox: 46.0.1
    Safari: 14.0
  npmPackages:
    @aws-amplify/ui-react: ^0.2.27 => 0.2.27 
    @babel/plugin-transform-flow-strip-types: ^7.12.1 => 7.12.1 
    @fortawesome/fontawesome-pro: ^5.15.1 => 5.15.1 
    @fortawesome/fontawesome-svg-core: ^1.2.32 => 1.2.32 
    @fortawesome/free-brands-svg-icons: ^5.15.1 => 5.15.1 
    @fortawesome/free-regular-svg-icons: ^5.15.1 => 5.15.1 
    @fortawesome/free-solid-svg-icons: ^5.15.1 => 5.15.1 
    @fortawesome/pro-duotone-svg-icons: ^5.15.1 => 5.15.1 
    @fortawesome/pro-light-svg-icons: ^5.15.1 => 5.15.1 
    @fortawesome/pro-regular-svg-icons: ^5.15.1 => 5.15.1 
    @fortawesome/pro-solid-svg-icons: ^5.15.1 => 5.15.1 
    @fortawesome/react-fontawesome: ^0.1.12 => 0.1.12 
    @zeit/next-css: ^1.0.1 => 1.0.1 
    aws-amplify: ^3.3.7 => 3.3.7 
    bootstrap: ^4.5.3 => 4.5.3 
    caniuse-lite: ^1.0.30001156 => 1.0.30001156 
    express: ^4.17.1 => 4.17.1 
    jquery: ^3.5.1 => 3.5.1 
    moment: ^2.29.1 => 2.29.1 
    next: ^10.0.1 => 10.0.1 
    popper.js: ^1.16.1 => 1.16.1 
    prop-types: ^15.7.2 => 15.7.2 
    react: ^16.14.0 => 16.14.0 
    react-bootstrap: ^1.4.0 => 1.4.0 
    react-bootstrap-typeahead: ^5.1.2 => 5.1.2 
    react-csv: ^2.0.3 => 2.0.3 
    react-dom: ^16.14.0 => 16.14.0 
    react-markdown: ^4.3.1 => 4.3.1 
    react-medium-image-zoom: ^4.3.1 => 4.3.1 
    sharp: ^0.26.3 => 0.26.3 
  npmGlobalPackages:
    expo-cli: 3.27.12
    npm: 6.14.7
    sharp-cli: 1.13.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

5reactions
lukasalvarezdevcommented, Jan 26, 2021

@carlhaynes And how did you solve it?

0reactions
github-actions[bot]commented, Jun 2, 2022

This issue has been automatically locked since there hasn’t been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels or Discussions for those types of questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - API Gateway not receiving auth info when using ...
Describe the bug. I have an nextjs app that uses Cognito and Amplify to login and out using the hosted pages (using <AmplifyAuthenticator ......
Read more >
SSR Support for AWS Amplify JavaScript Libraries
Using the Auth class to check and determine user authentication status in an API route before returning data in the API response. Using...
Read more >
The Complete Guide to Next.js Authentication
Accessing the user session in an API route; Social sign-in (OAuth); Deploying the app using the Next.js Serverless Component. Getting started.
Read more >
@aws-amplify/auth | Yarn - Package Manager
If you are using exported paths within your Amplify JS application, (e.g. import from "@aws-amplify/analytics/lib/Analytics" ) this will now break and no longer ......
Read more >
amplify-js
Sigv4 signing and AWS auth for API Gateway and other REST endpoints. GraphQL API · AWS AppSync, Interact with your GraphQL or AWS...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found