Use this file to discover all available pages before exploring further.
The Authentication API allows extensions to request authentication sessions from authentication providers. This enables extensions to securely authenticate users and access protected resources on their behalf.
Represents an authentication session with an access token and associated account information:
AuthenticationSession Interface
interface AuthenticationSession { /** * The identifier of the authentication session. */ readonly id: string; /** * The access token. This token should be used to authenticate requests to a service. */ readonly accessToken: string; /** * The ID token. This token contains identity information about the user. */ readonly idToken?: string; /** * The account associated with the session. */ readonly account: AuthenticationSessionAccountInformation; /** * The permissions granted by the session's access token. */ readonly scopes: readonly string[];}
Information about the account associated with a session:
interface AuthenticationSessionAccountInformation { /** * The unique identifier of the account. */ readonly id: string; /** * The human-readable name of the account. */ readonly label: string;}
Whether login should be performed if there is no matching session. If true, a modal dialog will be shown. If false, a numbered badge will be shown on the accounts menu.
Only request the scopes your extension actually needs. Users are more likely to grant narrower permissions.
// Good: Request only what you needconst session = await vscode.authentication.getSession('github', ['repo']);// Bad: Request unnecessary permissionsconst session = await vscode.authentication.getSession('github', ['repo', 'admin:org', 'delete_repo']);
Handle Authentication Failures Gracefully
Users may deny authentication or close the dialog. Always check for undefined sessions.
const session = await vscode.authentication.getSession('github', ['repo'], { createIfNone: true});if (!session) { vscode.window.showWarningMessage('Authentication is required to use this feature'); return;}
Use Silent Mode for Background Operations
When checking for existing sessions in the background, use silent mode to avoid disturbing the user.
// Check for existing session without showing UIconst session = await vscode.authentication.getSession('github', ['repo'], { silent: true});if (session) { // Proceed with authenticated operation} else { // Show sign-in button in UI}
Cache Sessions
Cache the session in your extension to avoid repeated authentication calls.
let cachedSession: vscode.AuthenticationSession | undefined;async function getSession(): Promise<vscode.AuthenticationSession | undefined> { if (cachedSession) { return cachedSession; } cachedSession = await vscode.authentication.getSession('github', ['repo'], { silent: true }); return cachedSession;}// Clear cache when sessions changevscode.authentication.onDidChangeSessions(() => { cachedSession = undefined;});