Skip to content

Cookie is not available inside Amplify deployed Next APP

0

I successfully deployed my Next.js app using Amplify. The app renders all unprotected routes as expected. However, when I log in using my authentication service (which is deployed separately as an ECS instance on a different domain), I encounter an issue.

After authentication, the app is expected to redirect to the home page (a protected route). However, it fails because getServerSideProps checks for a cookie before granting access, and the cookie appears to be an empty object. Here’s the relevant code:

export const getServerSideProps = (context: GetServerSidePropsContext) => {
  const user: User = getUserFromCookie(context) as User;

  if (!user) {
    return {
      redirect: {
        destination: '/login',
      }
    }
  }
  return {
    props: {
      user
    }
  }
}

export const getUserFromCookie = (context: GetServerSidePropsContext) => {
  const cookies = nookies.get(context);

  console.log(context.req);
  console.log("Cookies", cookies)

  const user = decodeJWT(cookies.access_token ?? cookies.refresh_token ?? '');
  
  console.log("User", user)

  if (!user) {
    return null;
  }
  return user as User;
};

Despite trying different cookie settings like:

SameSite=None; Secure=False SameSite=None; Secure=True The cookie remains empty when accessed via getServerSideProps.

Could you help me understand what’s happening under the hood? Is this a CORS, cookie forwarding, or domain-specific issue with Amplify? Any guidance on resolving this would be greatly appreciated.

Thanks!

asked 2 years ago420 views

1 Answer
-2

The issue you're experiencing with cookies not being available in your Amplify-deployed Next.js app is likely related to how cookies are handled in server-side environments, particularly in the context of cross-domain authentication.

There are a few potential reasons for this:

  1. Cross-Origin Cookie Restrictions: Modern browsers have strict security policies for cross-origin cookies. If your authentication service is on a different domain than your Next.js app, the browser may not send the cookies set by the authentication service to your app's domain.

  2. Secure and HttpOnly Flags: If the cookies are set with the Secure flag, they will only be transmitted over HTTPS connections. Additionally, if they're set with the HttpOnly flag, they won't be accessible via client-side JavaScript, which is a security best practice but can complicate certain implementations.

  3. SameSite Cookie Attribute: The SameSite attribute affects how cookies are sent in cross-site requests. Setting it to 'None' allows cross-site cookie sharing, but requires the Secure flag to be set as well.

  4. Amplify's Server-Side Rendering Environment: Amplify's server-side rendering environment might have specific configurations or limitations regarding cookie handling that differ from a typical Next.js setup.

To address this issue, consider the following approaches:

  1. Ensure your authentication service is setting cookies correctly for cross-domain use. This typically involves setting SameSite=None and Secure=True.

  2. Instead of relying on cookies for authentication in server-side props, consider using a different authentication method that works well with Amplify and Next.js server-side rendering. For example, you could use JSON Web Tokens (JWTs) passed in headers or as query parameters.

  3. If you must use cookies, you might need to implement a custom solution using Amplify's server-side data clients. Amplify offers specialized data clients for Next.js server runtimes that can help with authentication and data fetching in server-side contexts.

  4. Double-check your CORS settings both on your authentication service and in your Amplify configuration to ensure they allow the necessary cross-origin requests.

  5. Consider using Amplify's built-in authentication services if possible, as they are designed to work seamlessly with Amplify-hosted applications.

Remember, when dealing with authentication and cookies, it's crucial to prioritize security. Ensure that your chosen solution doesn't introduce vulnerabilities, especially when working with cross-domain setups.

If these approaches don't resolve the issue, you may need to dig deeper into Amplify's specific handling of server-side rendering and cookie management, possibly reaching out to AWS support for Amplify-specific insights.

Sources
Next.js server runtime - React - AWS Amplify Gen 2 Documentation
Cookie Without Http Only Flag | Amazon Q, Detector Library
Insecure cookie | Amazon Q, Detector Library

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.