OPEN TO WORK

Available for new opportunities! Let's build something amazing together.

Creating a reusable auth provider in Visual Studio Code

Development Visual Studio Code Authentication
post

This post is over a year old, some of this information may be out of date.

A couple of months ago, I wrote about how you could create an authentication provider for Visual Studio Code. In this article, we will take it further and make it reusable by other extensions.

The reason for creating a reusable authentication provider could differ per solution. Maybe you want to make one for other extensions to leverage, or your company wants to release multiple extensions that can all use the same type of authentication.

For me, the lather is why I created a reusable authentication provider. Instead of creating an auth provider for each extension we make, we wanted to create one and leverage it from any of the extensions we build in the future.

How to make it reusable?

First, you already need an authentication provider in a separate extension.

Once you have the authentication provider, you can make it reusable by configuring the package.json file.

Update the activation events

Update the activationEvents array with the following:

{
...
"activationEvents": [
"onAuthenticationRequest:<auth id>"
],
}

This onAuthenticationRequest event with your authentication ID (the ID you used in the registerAuthenticationProvider method) will trigger your extension to activate when another extension requests authentication.

With this change applied, your authentication provider extension is now reusable and can be published.

What about dependent extensions?

All the other extensions that you create require one small change as well. In the extension’s package.json file, it is best to add the extensionDependencies array with the ID of your authentication provider extension.

{
...
"extensionDependencies": [
"<auth provider extension id>"
],
}

This array of extension dependencies will ensure that your Visual Studio Code instance installs the authentication provider extension.

How to use it?

There is no difference in using the authentication provider from another extension. You use the getSessions to retrieve the session or create a new one.

const session = authentication.getSession("<auth id>", [], { createIfNone: true });

Related articles

Report issues or make changes on GitHub

Found a typo or issue in this article? Visit the GitHub repository to make changes or submit a bug report.

Comments

Elio Struyf

Solutions Architect & Developer Expert

Loading...

Let's build together

Manage content in VS Code

Present from VS Code