-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(singleton): allow oauth listener to be called with the latest config #13642
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,14 +13,14 @@ import { | |
} from './types'; | ||
import { AuthClass } from './Auth'; | ||
import { ADD_OAUTH_LISTENER } from './constants'; | ||
import { OAuthListener } from './Auth/types'; | ||
|
||
export class AmplifyClass { | ||
private oAuthListener: | ||
| ((authConfig: AuthConfig['Cognito']) => void) | ||
| undefined = undefined; | ||
private oAuthListener: OAuthListener | undefined = undefined; | ||
|
||
resourcesConfig: ResourcesConfig; | ||
libraryOptions: LibraryOptions; | ||
private listenerTimeout?: ReturnType<typeof setTimeout>; | ||
|
||
/** | ||
* Cross-category Auth utilities. | ||
|
@@ -53,7 +53,6 @@ export class AmplifyClass { | |
libraryOptions?: LibraryOptions, | ||
): void { | ||
const resolvedResourceConfig = parseAmplifyConfig(resourcesConfig); | ||
|
||
this.resourcesConfig = resolvedResourceConfig; | ||
|
||
if (libraryOptions) { | ||
|
@@ -75,7 +74,10 @@ export class AmplifyClass { | |
AMPLIFY_SYMBOL, | ||
); | ||
|
||
this.notifyOAuthListener(); | ||
const config = this.resourcesConfig.Auth?.Cognito; | ||
if (config?.loginWith?.oauth && this.oAuthListener) { | ||
this.notifyOAuthListener(this, config, this.oAuthListener); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -98,17 +100,27 @@ export class AmplifyClass { | |
} | ||
} | ||
|
||
private notifyOAuthListener() { | ||
if ( | ||
!this.resourcesConfig.Auth?.Cognito.loginWith?.oauth || | ||
!this.oAuthListener | ||
) { | ||
return; | ||
private notifyOAuthListener( | ||
amplify: AmplifyClass, | ||
config: AuthConfig['Cognito'], | ||
oAuthListener: OAuthListener, | ||
) { | ||
if (amplify.listenerTimeout) { | ||
amplify.clearListenerTimeout(); | ||
} | ||
// The setTimeout will only be called as long as an oAuthLister and oauth config | ||
// are defined. The code executed by the time out will be only fired once. | ||
Comment on lines
+111
to
+112
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sounds like it only works with certain preconditions, e.g. assuming With Amplify Gen2, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have to echo this sentiment. When I looked at this PR this was my initial concern as well. Code after the configure call that may have previously executed after the |
||
amplify.listenerTimeout = setTimeout(() => { | ||
oAuthListener(config); | ||
// the listener should only be notified once with a valid oauth config | ||
amplify.oAuthListener = undefined; | ||
amplify.clearListenerTimeout(); | ||
}); | ||
} | ||
|
||
this.oAuthListener(this.resourcesConfig.Auth?.Cognito); | ||
// the listener should only be notified once with a valid oauth config | ||
this.oAuthListener = undefined; | ||
private clearListenerTimeout() { | ||
clearTimeout(this.listenerTimeout); | ||
this.listenerTimeout = undefined; | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making this pure would make sense if it were removed completely from the class, but since it is a private member of the class, this seems confusing. For example, it's not really clear to me why
oAuthListener
is passed separately fromthis
. TheclearListenerTimeout
is then also tacked onto the instance but it's obfuscated here.