diff --git a/extensions/github-authentication/src/extension.ts b/extensions/github-authentication/src/extension.ts index c9025e7373f..d2022806057 100644 --- a/extensions/github-authentication/src/extension.ts +++ b/extensions/github-authentication/src/extension.ts @@ -22,7 +22,7 @@ export async function activate(context: vscode.ExtensionContext) { return loginService.manuallyProvideToken(); })); - vscode.authentication.registerAuthenticationProvider({ + context.subscriptions.push(vscode.authentication.registerAuthenticationProvider({ id: 'github', label: 'GitHub', supportsMultipleAccounts: false, @@ -70,7 +70,7 @@ export async function activate(context: vscode.ExtensionContext) { throw e; } } - }); + })); return; } diff --git a/src/vs/workbench/services/authentication/browser/authenticationService.ts b/src/vs/workbench/services/authentication/browser/authenticationService.ts index 5b609209058..82a27ec5e0c 100644 --- a/src/vs/workbench/services/authentication/browser/authenticationService.ts +++ b/src/vs/workbench/services/authentication/browser/authenticationService.ts @@ -432,24 +432,46 @@ export class AuthenticationService extends Disposable implements IAuthentication } } - async getSessions(id: string): Promise> { - await this.extensionService.activateByEvent(getAuthenticationProviderActivationEvent(id)); + private async tryActivateProvider(providerId: string): Promise { + await this.extensionService.activateByEvent(getAuthenticationProviderActivationEvent(providerId)); + let provider = this._authenticationProviders.get(providerId); + if (provider) { + return provider; + } - const authProvider = this._authenticationProviders.get(id); - if (authProvider) { + // When activate has completed, the extension has made the call to `registerAuthenticationProvider`. + // However, activate cannot block on this, so the renderer may not have gotten the event yet. + const didRegister: Promise = new Promise((resolve, _) => { + this.onDidRegisterAuthenticationProvider(e => { + if (e.id === providerId) { + resolve(this._authenticationProviders.get(providerId)); + } + }); + }); + + const didTimeout: Promise = new Promise((_, reject) => { + setTimeout(() => { + reject(); + }, 2000); + }); + + return Promise.race([didRegister, didTimeout]); + } + + async getSessions(id: string): Promise> { + try { + const authProvider = this._authenticationProviders.get(id) || await this.tryActivateProvider(id); return await authProvider.getSessions(); - } else { + } catch (_) { throw new Error(`No authentication provider '${id}' is currently registered.`); } } async login(id: string, scopes: string[]): Promise { - await this.extensionService.activateByEvent(getAuthenticationProviderActivationEvent(id)); - - const authProvider = this._authenticationProviders.get(id); - if (authProvider) { - return authProvider.login(scopes); - } else { + try { + const authProvider = this._authenticationProviders.get(id) || await this.tryActivateProvider(id); + return await authProvider.login(scopes); + } catch (_) { throw new Error(`No authentication provider '${id}' is currently registered.`); } }