Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions extensions/ql-vscode/src/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,24 @@ export class Credentials {
static async initialize(context: vscode.ExtensionContext): Promise<Credentials> {
const c = new Credentials();
c.registerListeners(context);
await c.initializeOctokit(false);
await c.createOctokit(false);
Comment thread
shati-patel marked this conversation as resolved.
Outdated
return c;
}

private async initializeOctokit(createIfNone: boolean) {
// If `createIfNone` is true, a dialog pops up asking the user to authenticate as soon as the extension starts.
// Initializing with `createIfNone: false` for now, so we can have a more quiet prompt, i.e. a numbered label on
// the "accounts" icon in the activity bar.
private async createOctokit(createIfNone: boolean): Promise<Octokit.Octokit | undefined> {
const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: createIfNone });
Comment thread
shati-patel marked this conversation as resolved.
Outdated

if (session) {
this.octokit = new Octokit.Octokit({
return session
? new Octokit.Octokit({
auth: session.accessToken
});
} else {
this.octokit = undefined;
}
}) : undefined;
}

registerListeners(context: vscode.ExtensionContext): void {
// Sessions are changed when a user logs in or logs out.
context.subscriptions.push(vscode.authentication.onDidChangeSessions(async e => {
if (e.provider.id === GITHUB_AUTH_PROVIDER_ID) {
await this.initializeOctokit(false);
this.octokit = await this.createOctokit(false);
}
}));
}
Expand All @@ -54,11 +48,11 @@ export class Credentials {
return this.octokit;
}

await this.initializeOctokit(true);
this.octokit = await this.createOctokit(true);
// octokit shouldn't be undefined, since we've set "createIfNone: true".
// The following block is mainly here to prevent a compiler error.
Comment thread
shati-patel marked this conversation as resolved.
if (!this.octokit) {
throw new Error('Failed to initialize Octokit.');
throw new Error('Did not initialize Octokit.');
}
Comment thread
shati-patel marked this conversation as resolved.
return this.octokit;
}
Expand Down