1

I am using nuxt + aws amplify for the web app and I need to check if the user is authenticated to load the page details depending on that. I configured amplify so the user credentials are stored in the cookie instead of the local storage so all the information about the user can be retrieved from req.headers.cookie, but the code below

async nuxtServerInit (props, ctx) {
  const { Auth } = withSSRContext(ctx)
  try {
    const cognitoUser = await Auth.currentAuthenticatedUser()
    console.log(cognitoUser)
  } catch(error) {
    console.error(error)
  }
},

gives The user is not authenticated

My amplify config:

Amplify.configure({
    sst: true,
    Auth: {
        identityPoolId: process.env.IDENTITY_POOL_ID,
        userPoolId: process.env.USER_POOL_ID,
        userPoolWebClientId: process.env.USER_POOL_CLIENT_ID,
        region: 'eu-central-1',
        mandatorySignIn: false,
        cookieStorage: {
            domain: isDev ? 'localhost;' : '.website.my',
            path: '/',
            expires: 1,
            sameSite: 'strict',
            secure: !isDev
        },
        authenticationFlowType: 'USER_PASSWORD_AUTH'
    }
})

Login and auth work fine on client-side.

kissu
  • 40,416
  • 14
  • 65
  • 133
Rob Minasyan
  • 390
  • 2
  • 14

2 Answers2

1

It looks like you've configured Amplify correctly. I'd recommend going through the following guides and double check your configuration...

  1. https://docs.amplify.aws/start/getting-started/data-model/q/integration/next#api-with-server-side-rendering-ssr

  2. AWS Cognito cookie storage

  3. https://github.com/aws-amplify/amplify-js/issues/1735

Alex
  • 952
  • 7
  • 12
1

I found out that the issue was in the configuration, when using ssr: true you are not allowed to add manual configuration for cookies. So the solution here is just removing this block

cookieStorage: {
  domain: isDev ? 'localhost;' : '.website.my',
  path: '/',
  expires: 1,
  sameSite: 'strict',
  secure: !isDev
},
Rob Minasyan
  • 390
  • 2
  • 14