Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Support more than one authentication session #906

Merged
merged 5 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions generator/src/templates/theiaPlugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"vscode-editorconfig": "https://open-vsx.org/api/EditorConfig/EditorConfig/0.14.4/file/EditorConfig.EditorConfig-0.14.4.vsix",
"vscode-eslint": "https://open-vsx.org/api/dbaeumer/vscode-eslint/2.1.1/file/dbaeumer.vscode-eslint-2.1.1.vsix",
"vscode-git": "https://open-vsx.org/api/vscode/git/1.49.3/file/vscode.git-1.49.3.vsix",
"vscode-github": "https://open-vsx.org/api/vscode/github/1.50.1/file/vscode.github-1.50.1.vsix",
"vscode-references-view": "https://open-vsx.org/api/ms-vscode/references-view/0.0.47/file/ms-vscode.references-view-0.0.47.vsix",
"vscode-builtin-icon-theme-seti": "https://open-vsx.org/api/vscode/vscode-theme-seti/1.44.2/file/vscode.vscode-theme-seti-1.44.2.vsix",
"vscode-builtin-theme-abyss": "https://open-vsx.org/api/vscode/theme-abyss/1.44.2/file/vscode.theme-abyss-1.44.2.vsix",
Expand Down
3 changes: 2 additions & 1 deletion plugins/github-auth-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"@eclipse-che/plugin": "0.0.1",
"rimraf": "2.6.2",
"typescript-formatter": "7.2.2",
"ts-loader": "^4.1.0"
"ts-loader": "^4.1.0",
"uuid": "^8.0.0"
},
"scripts": {
"prepare": "yarn run clean && yarn run build",
Expand Down
46 changes: 25 additions & 21 deletions plugins/github-auth-plugin/src/github-auth-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,49 @@

import * as theia from '@theia/plugin';
import * as che from '@eclipse-che/plugin';
import { v4 } from 'uuid';

export async function start(context: theia.PluginContext): Promise<void> {
if (theia.plugins.getPlugin('github.vscode-pull-request-github')) {
let session: theia.AuthenticationSession | undefined = context.workspaceState.get('session');
const onDidChangeSessions = new theia.EventEmitter<theia.AuthenticationProviderAuthenticationSessionsChangeEvent>();
theia.authentication.registerAuthenticationProvider({
const sessions: theia.AuthenticationSession[] = context.workspaceState.get('sessions') || [];
const onDidChangeSessions = new theia.EventEmitter<theia.AuthenticationProviderAuthenticationSessionsChangeEvent>();
theia.authentication.registerAuthenticationProvider({
id: 'github',
label: 'GitHub',
supportsMultipleAccounts: false,
onDidChangeSessions: onDidChangeSessions.event,
getSessions: async () => {
if (session) {
return [session];
} else {
return [];
}
},
getSessions: async () => sessions,
login: async (scopes: string[]) => {
const githubUser = await che.github.getUser();
session = {
id: 'github-session',
const session = {
id: v4(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need uuid or just a counter ?

accessToken: await che.github.getToken(),
account: { label: githubUser.login, id: githubUser.id.toString() },
scopes
};
context.workspaceState.update('session', session);
const sessionIndex = sessions.findIndex(s => s.id === session.id);
if (sessionIndex > -1) {
sessions.splice(sessionIndex, 1, session);
} else {
sessions.push(session);
}
context.workspaceState.update('sessions', sessions);
onDidChangeSessions.fire({ added: [session.id], removed: [], changed: [] });
return session;
},
logout: async (id: string) => {
session = undefined;
context.workspaceState.update('session', session);
onDidChangeSessions.fire({ added: [], removed: [id], changed: [] });
const sessionIndex = sessions.findIndex(session => session.id === id);
if (sessionIndex > -1) {
sessions.splice(sessionIndex, 1);
context.workspaceState.update('sessions', sessions);
onDidChangeSessions.fire({ added: [], removed: [id], changed: [] });
}
}
}
);
if (session) {
onDidChangeSessions.fire({ added: [session.id], removed: [], changed: [] });
// TODO Remove the notification when https://github.com/eclipse-theia/theia/issues/7178 is fixed.
);
if (theia.plugins.getPlugin('github.vscode-pull-request-github')) {
if (sessions.length > 0) {
onDidChangeSessions.fire({ added: sessions.map(s => s.id), removed: [], changed: [] });
// TODO Remove the notification when https://github.com/eclipse-theia/theia/issues/7178 is fixed.
} else {
const signIn = 'Sign in';
const result = await theia.window.showInformationMessage(
Expand Down