AWS Cognito + Amplify: Accessing Auth.currentSession from Another App

Viewed 418

I have an existing Cognito user pool that I'd like to authenticate against. I'm following the Re-use existing authentication resource instructions in the Amplify docs

I'm signing in through a NextJS applicaiton running on port 3000 (authenticating there works well and I can see I'm authenticated by hitting http://localhost:3000/api/auth/session or by viewing my cookies. The name of the authenticated cookie is next-auth.session-token.

How can configure Amplify to retrieve the session using this cookie? When I make a call to currentSession() below I'm getting a No current user error

Amplify.configure({
  region: "eu-west-1",
  userPoolId: "eu-west-1_XXXXXXXXX",
  userPoolWebClientId: "XXXXXXXXXXXXXXXXXXXXXXXXXX",

  // Cognito Hosted UI configuration
  oauth: {
    domain: "XXXXXXXXXXX.auth.eu-west-1.amazoncognito.com",
    scope: ['email', 'profile', 'openid'],
    redirectSignIn: "http://localhost:4200/",
    redirectSignOut: "http://localhost:3000/",
    responseType: 'token',
  },
  cookieStorage: {
    domain: 'localhost',
    secure: false,
    sameSite: 'lax',
    path: '/',
  }
});

Auth.currentSession()
  .then((response) => {
    console.log('response=', response);
  })
  .catch((error) => {
    // error below is "No current user"
    console.error('error=', error);
  });

Both my app for logging in and the actual app I'm trying to get the user's session from are running locally

  • logging in: port 3000
  • check users session with Amplify: port 4200
0 Answers
Related