aws-amplify / amplify-ui

Amplify UI is a collection of accessible, themeable, performant React (and more!) components that can connect directly to the cloud.
https://ui.docs.amplify.aws
Apache License 2.0
889 stars 282 forks source link

[FR] Auto sign in for a multiple logins with the same user #4616

Closed dcp3450 closed 11 months ago

dcp3450 commented 11 months ago

Before creating a new issue, please confirm:

On which framework/platform are you having an issue?

Angular

Which UI component?

Authenticator

How is your app built?

custom-webpack 16

What browsers are you seeing the problem on?

No response

Which region are you seeing the problem in?

No response

Please describe your bug.

I have 5 applications that use the @aws-amplify/ui-angular package. Sign up / login works as expected. However, when a user logs into one application and attempts to view another they have to login again. All users are on the same userpool and identity pool. they are also on the same domain/subdomain as the route determines the app:

company.myapp.com/app1 company.myapp.com/app2 company.myapp.com/app3 ...

It was my understanding (based on an AWS doc I read somewhere) that if the user has logged in at one location via their cognito login and the domain was the same, they would be auto signed in.

If I need to grab an ID stored in a cookie and verify the user that way, that makes sense but the docs don't seem to suggest this.

What's the expected behaviour?

When the user logs in at one location with userpool creds and attempts to access a second location they don't have to login again. Their previous login at the same domain should allow them to go forward.

Help us reproduce the bug!

have to apps with the same domain/subdomain but different routes.

Code Snippet

// Put your code below this line.

Console log output

No response

Additional information and screenshots

No response

calebpollman commented 11 months ago

Hi @dcp3450. To clarify the ask here, can you confirm whether the expectation is that if an end user has two apps open in the same browser instance (App A, App B) that signing in to App A will "refresh" App B automatically?

dcp3450 commented 11 months ago

@calebpollman No, they don't need to talk in that way. I see instances of what I'm trying to do:

  1. They login to App A -> open App B in a new tab and they are auto authenticated and allowed access
  2. Both apps are open, they login to one of them, manual refresh the other and it lets them in since they are already logged in

What confuses me is all apps have the exact same subdomain/domain. So when the user logs in at one location (App A) it creates the local storage and cookies. When they access App B, it requires a log in even though they are the same domain and have access to the same local storage and cookies. Logging in at the second app creates a other set of storage and cookies.

I verify the user is logged in via:

  constructor(
    ...
    public authenticator: AuthenticatorService,
    ...
  ) {}

  ...
    this.authenticator.subscribe(() => {
      const { route } = this.authenticator;
      if (route === 'authenticated') {
      }
    });
  ...

This is called when the user attempts to access a page that requires auth. If they aren't authenticated, then it kicks them out to the login.

dcp3450 commented 11 months ago

@calebpollman I looked over my code again and noticed, this is the code checking if the user is already authenticated:

...
import Auth from '@aws-amplify/auth';
...
  canActivate(... ): Promise<boolean> {
    return Auth.currentAuthenticatedUser()
      .then(user => {
        ...
        return true;
      })
      .catch(() => {
        console.error('Must be signed in');
        ...
        return false;
      });
  }

The problem and assumptions still remain. The domains are the same just the code base at the route /appA , /appB, etc is different. If the domains are the same and each application has access to the same local storage and cookies, what magic sauce am I missing?

dcp3450 commented 11 months ago

All the apps had the same user pool and identity pool IDs but they had different web client IDs. I gave them the same web client ID and login works as expected.