Files
vscode-extension-samples/authenticationprovider-sample/src/extension.ts
Tyler James Leonhardt 2185d360c3 Couple AuthProvider Sample fixes (#1015)
1. remove comment about activation event, it doesn't apply anymore as we have implicit activation
2. aligned the ids
3. Fired removed change event which removes the account from the account menu
2024-05-03 13:53:27 -07:00

50 lines
2.2 KiB
TypeScript

// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import 'isomorphic-fetch';
import * as vscode from 'vscode';
import { AzureDevOpsAuthenticationProvider } from './authProvider';
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
console.log('Congratulations, your extension "vscode-authenticationprovider-sample" is now active!');
// Register our authentication provider. NOTE: this will register the provider globally which means that
// any other extension can use this provider via the `getSession` API.
context.subscriptions.push(vscode.authentication.registerAuthenticationProvider(
AzureDevOpsAuthenticationProvider.id,
'Azure Repos',
new AzureDevOpsAuthenticationProvider(context.secrets),
));
let disposable = vscode.commands.registerCommand('vscode-authenticationprovider-sample.login', async () => {
// Get our PAT session.
const session = await vscode.authentication.getSession(AzureDevOpsAuthenticationProvider.id, [], { createIfNone: true });
try {
// Make a request to the Azure DevOps API. Keep in mind that this particular API only works with PAT's with
// 'all organizations' access.
const req = await fetch('https://app.vssps.visualstudio.com/_apis/profile/profiles/me?api-version=6.0', {
headers: {
authorization: `Basic ${Buffer.from(`:${session.accessToken}`).toString('base64')}`,
// eslint-disable-next-line @typescript-eslint/naming-convention
'content-type': 'application/json',
},
});
if (!req.ok) {
throw new Error(req.statusText);
}
const res = await req.json() as { displayName: string };
vscode.window.showInformationMessage(`Hello ${res.displayName}`);
} catch (e: any) {
if (e.message === 'Unauthorized') {
vscode.window.showErrorMessage('Failed to get profile. You need to use a PAT that has access to all organizations. Please sign out and try again.');
}
throw e;
}
});
context.subscriptions.push(disposable);
}