Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logout command #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@
"command": "mermaidChart.viewMermaidChart",
"title": "View Diagram"
},
{
"command": "mermaidChart.logout",
"title": "MermaidChart Logout"
},
{
"command": "mermaidChart.editMermaidChart",
"title": "Edit Diagram"
Expand Down
6 changes: 6 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export async function activate(context: vscode.ExtensionContext) {
const mcAPI = new MermaidChartVSCode();
await mcAPI.initialize(context);

context.subscriptions.push(
vscode.commands.registerCommand('mermaidChart.logout', async () => {
mcAPI.logout(context);
})
);

const mermaidChartProvider: MermaidChartProvider = new MermaidChartProvider(
mcAPI
);
Expand Down
15 changes: 15 additions & 0 deletions src/mermaidChartAuthenticationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ export class MermaidChartAuthenticationProvider
>();
private _uriHandler = new UriEventHandler();

private static instance: MermaidChartAuthenticationProvider | null = null;

static getInstance(
mcAPI: MermaidChartVSCode,
context: ExtensionContext
): MermaidChartAuthenticationProvider {
if (!MermaidChartAuthenticationProvider.instance) {
MermaidChartAuthenticationProvider.instance = new MermaidChartAuthenticationProvider(
mcAPI,
context
);
}
return MermaidChartAuthenticationProvider.instance;
}

constructor(
private readonly mcAPI: MermaidChartVSCode,
private readonly context: ExtensionContext
Expand Down
21 changes: 19 additions & 2 deletions src/mermaidChartVSCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ export class MermaidChartVSCode extends MermaidChart {
await this.setupAPI();
}

public async logout(context: vscode.ExtensionContext): Promise<void> {
const session = await vscode.authentication.getSession(
MermaidChartAuthenticationProvider.id,
[],
{ silent: true }
);

if (session) {
const authProvider = MermaidChartAuthenticationProvider.getInstance(this, context);
await authProvider.removeSession(session.id);
vscode.window.showInformationMessage(`You have successfully signed out from ${session.account.id}.`);
} else {
vscode.window.showInformationMessage('No active session found. You are already signed out.');
}
}


private async registerListeners(context: vscode.ExtensionContext) {
/**
* Register the authentication provider with VS Code.
Expand All @@ -26,7 +43,7 @@ export class MermaidChartVSCode extends MermaidChart {
vscode.authentication.registerAuthenticationProvider(
MermaidChartAuthenticationProvider.id,
MermaidChartAuthenticationProvider.providerName,
new MermaidChartAuthenticationProvider(this, context)
MermaidChartAuthenticationProvider.getInstance(this, context)
)
);

Expand Down Expand Up @@ -81,4 +98,4 @@ export function getBaseUrl(): string | undefined {
// If baseURL was not set, set it to default
config.update("baseUrl", defaultBaseURL, true);
return defaultBaseURL;
}
}