mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
Add github-authentication-sample
This commit is contained in:
63
github-authentication-sample/src/credentials.ts
Normal file
63
github-authentication-sample/src/credentials.ts
Normal file
@ -0,0 +1,63 @@
|
||||
import * as vscode from 'vscode';
|
||||
import * as Octokit from '@octokit/rest';
|
||||
|
||||
const GITHUB_AUTH_PROVIDER_ID = 'github';
|
||||
// The GitHub Authentication Provider accepts the scopes described here:
|
||||
// https://developer.github.com/apps/building-oauth-apps/understanding-scopes-for-oauth-apps/
|
||||
const SCOPES = ['user:email'];
|
||||
|
||||
export class Credentials {
|
||||
private octokit: Octokit.Octokit | undefined;
|
||||
|
||||
async initialize(context: vscode.ExtensionContext): Promise<void> {
|
||||
this.registerListeners(context);
|
||||
this.setOctokit();
|
||||
}
|
||||
|
||||
private async setOctokit() {
|
||||
/**
|
||||
* By passing the `createIfNone` flag, a numbered badge will show up on the accounts activity bar icon.
|
||||
* An entry for the sample extension will be added under the menu to sign in. This allows quietly
|
||||
* prompting the user to sign in.
|
||||
* */
|
||||
const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: false });
|
||||
|
||||
if (session) {
|
||||
this.octokit = new Octokit.Octokit({
|
||||
auth: session.accessToken
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
this.octokit = 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.setOctokit();
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
async getOctokit(): Promise<Octokit.Octokit> {
|
||||
if (this.octokit) {
|
||||
return this.octokit;
|
||||
}
|
||||
|
||||
/**
|
||||
* When the `createIfNone` flag is passed, a modal dialog will be shown asking the user to sign in.
|
||||
* Note that this can throw if the user clicks cancel.
|
||||
*/
|
||||
const session = await vscode.authentication.getSession(GITHUB_AUTH_PROVIDER_ID, SCOPES, { createIfNone: true });
|
||||
this.octokit = new Octokit.Octokit({
|
||||
auth: session.accessToken
|
||||
});
|
||||
|
||||
return this.octokit;
|
||||
}
|
||||
}
|
||||
28
github-authentication-sample/src/extension.ts
Normal file
28
github-authentication-sample/src/extension.ts
Normal file
@ -0,0 +1,28 @@
|
||||
// The module 'vscode' contains the VS Code extensibility API
|
||||
// Import the module and reference it with the alias vscode in your code below
|
||||
import * as vscode from 'vscode';
|
||||
import { Credentials } from './credentials';
|
||||
|
||||
|
||||
export async function activate(context: vscode.ExtensionContext) {
|
||||
const credentials = new Credentials();
|
||||
await credentials.initialize(context);
|
||||
|
||||
const disposable = vscode.commands.registerCommand('extension.getGitHubUser', async () => {
|
||||
/**
|
||||
* Octokit (https://github.com/octokit/rest.js#readme) is a library for making REST API
|
||||
* calls to GitHub. It provides convenient typings that can be helpful for using the API.
|
||||
*
|
||||
* Documentation on GitHub's REST API can be found here: https://docs.github.com/en/rest
|
||||
*/
|
||||
const octokit = await credentials.getOctokit();
|
||||
const userInfo = await octokit.users.getAuthenticated();
|
||||
|
||||
vscode.window.showInformationMessage(`Logged into GitHub as ${userInfo.data.login}`);
|
||||
});
|
||||
|
||||
context.subscriptions.push(disposable);
|
||||
}
|
||||
|
||||
// this method is called when your extension is deactivated
|
||||
export function deactivate() {}
|
||||
Reference in New Issue
Block a user