Skip to content

Commit c35ae46

Browse files
committed
Clear login status when opening the extension if the cookie is deleted
1 parent 2853944 commit c35ae46

File tree

8 files changed

+48
-45
lines changed

8 files changed

+48
-45
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@
4646
"properties": {
4747
"Hackmd.serverURL": {
4848
"type": "string",
49-
"default": "https://hackmd.io/",
49+
"default": "https://hackmd.io",
5050
"markdownDescription": "Specify the self-hosted server URL for HackMD EE or CodiMD."
5151
},
5252
"Hackmd.enterprise": {
5353
"type": "boolean",
54-
"default": false,
54+
"default": true,
5555
"description": "Check this if you are connecting to HackMD Enterprise server which contains more features."
5656
}
5757
}

src/commands/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import * as vscode from 'vscode';
2-
import { registerUserCommands } from './user'
3-
import { registerTreeViewCommands } from './treeView'
2+
import { registerUserCommands } from './user';
3+
import { registerTreeViewCommands } from './treeView';
44
import { registerNoteCommands } from './note';
5-
import { Store } from '../store';
65

7-
export function registerCommand(context: vscode.ExtensionContext, store: Store) {
8-
registerUserCommands(context, store);
9-
registerTreeViewCommands(context, store);
6+
export function registerCommands(context: vscode.ExtensionContext) {
7+
registerUserCommands(context);
8+
registerTreeViewCommands(context);
109
registerNoteCommands(context);
1110
}

src/commands/treeView.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import * as vscode from 'vscode';
2-
import { Store } from '../store';
32
import { HackMDTreeViewProvider } from './../tree/index';
43
import { NoteTreeNode } from './../tree/nodes';
54
import { MdTextDocumentContentProvider } from './../mdTextDocument';
6-
import { refreshHistoryList } from './../utils';
5+
import { refreshHistoryList, refreshLoginStatus, refreshLoginCredential, getLoginCredential } from './../utils';
76
import { API, ExportType } from './../api';
87

9-
export async function registerTreeViewCommands(context: vscode.ExtensionContext, store: Store) {
10-
const hackMDTreeViewProvider = new HackMDTreeViewProvider(store);
8+
export async function registerTreeViewCommands(context: vscode.ExtensionContext) {
9+
const hackMDTreeViewProvider = new HackMDTreeViewProvider();
1110
context.subscriptions.push(vscode.window.registerTreeDataProvider('mdTreeItems', hackMDTreeViewProvider));
12-
context.subscriptions.push(vscode.commands.registerCommand('treeView.refreshList', async () => await refreshHistoryList()));
11+
context.subscriptions.push(vscode.commands.registerCommand('treeView.refreshList', async () => {
12+
await refreshLoginStatus();
13+
await refreshHistoryList();
14+
await refreshLoginCredential(context);
15+
}));
1316

1417
context.subscriptions.push(vscode.commands.registerCommand('clickTreeItem', async (label, noteId) => {
1518
if (label && noteId) {
@@ -36,7 +39,7 @@ export async function registerTreeViewCommands(context: vscode.ExtensionContext,
3639
context.subscriptions.push(vscode.commands.registerCommand('note.showPreviewAndEditor', async (node: NoteTreeNode) => {
3740
const noteNode = node;
3841
if (noteNode.label && noteNode.noteId) {
39-
const content = await API.exportString(noteNode.noteId,ExportType.MD);
42+
const content = await API.exportString(noteNode.noteId, ExportType.MD);
4043
if (content) {
4144
const uri = vscode.Uri.parse(`hackmd:${noteNode.label}.md#${noteNode.noteId}`);
4245
const doc = await vscode.workspace.openTextDocument(uri);

src/commands/user.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import * as vscode from 'vscode';
2-
import { checkLogin, login, refreshHistoryList } from './../utils'
3-
import { Store } from '../store';
2+
import { checkLogin, login, refreshHistoryList, refreshLoginStatus } from './../utils';
43
import { API } from './../api';
54

6-
export async function registerUserCommands(context: vscode.ExtensionContext, store: Store) {
5+
export async function registerUserCommands(context: vscode.ExtensionContext) {
76
context.subscriptions.push(vscode.commands.registerCommand('HackMD.login', async () => {
87
if (await checkLogin()) {
98
vscode.window.showInformationMessage('Already logged in, please log out first.');
@@ -50,8 +49,8 @@ export async function registerUserCommands(context: vscode.ExtensionContext, sto
5049
return;
5150
}
5251
await API.logout();
53-
store.isLogin = false;
54-
vscode.window.showInformationMessage('Successfully logged out.');
52+
await refreshLoginStatus();
5553
await refreshHistoryList();
54+
vscode.window.showInformationMessage('Successfully logged out.');
5655
}));
5756
}

src/extension.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import axios from 'axios';
44
import * as vscode from 'vscode';
55
import * as markdownitContainer from 'markdown-it-container';
66
import * as S from 'string';
7-
import { store } from './store';
87
import { initializeStorage } from './store/storage';
98
import * as Prism from 'prismjs';
10-
import { registerCommand } from './commands';
9+
import { registerCommands } from './commands';
1110

1211
require('prismjs/components/prism-wiki');
1312
require('prismjs/components/prism-haskell');
@@ -228,8 +227,8 @@ let highlight;
228227
axios.defaults.withCredentials = true;
229228

230229
export async function activate(context: vscode.ExtensionContext) {
231-
initializeStorage(context);
232-
registerCommand(context, store);
230+
initializeStorage();
231+
registerCommands(context);
233232

234233
return {
235234
extendMarkdownIt(md: any) {

src/store/storage.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
import { reaction } from "mobx";
21
import { store } from '.';
3-
import * as vscode from 'vscode';
2+
import { checkLogin } from "../utils";
3+
import { API } from './../api';
44

5-
export async function initializeStorage(context: vscode.ExtensionContext) {
6-
store.history = context.globalState.get('history');
7-
store.isLogin = context.globalState.get('isLogin');
8-
reaction(
9-
() => [store.history],
10-
() => {
11-
context.globalState.update('history', store.history);
12-
vscode.commands.executeCommand("setContext", 'history', store.history);
13-
14-
context.globalState.update('isLogin', store.isLogin);
15-
vscode.commands.executeCommand("setContext", 'isLogin', store.isLogin);
16-
}
17-
);
5+
export async function initializeStorage() {
6+
store.history = store.history = (await API.getHistory()).history.reverse();
7+
store.isLogin = await checkLogin();
188
}

src/tree/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import * as vscode from 'vscode';
2-
import { Store, store } from '../store';
2+
import { store } from '../store';
33
import { reaction } from 'mobx';
44
import { TreeNode, NoteTreeNode } from './nodes';
55

66
export class HackMDTreeViewProvider implements vscode.TreeDataProvider<TreeNode> {
77
private _onDidChangeTreeData = new vscode.EventEmitter<TreeNode>();
88
public readonly onDidChangeTreeData: vscode.Event<TreeNode> = this._onDidChangeTreeData.event;
9-
constructor(private store: Store) {
9+
constructor() {
1010
reaction(
1111
() => [
12-
store.history
12+
store.history,
13+
store.isLogin
1314
],
14-
() => {
15+
async () => {
1516
this.refresh();
1617
}
1718
);
@@ -24,9 +25,9 @@ export class HackMDTreeViewProvider implements vscode.TreeDataProvider<TreeNode>
2425
getChildren(element?: TreeNode): vscode.ProviderResult<TreeNode[]> {
2526
if (store.isLogin) {
2627
if (element === undefined) {
27-
return [new TreeNode("history", vscode.TreeItemCollapsibleState.Collapsed)]
28+
return [new TreeNode("history", vscode.TreeItemCollapsibleState.Collapsed)];
2829
} else {
29-
return this.store.history.map(item =>
30+
return store.history.map(item =>
3031
new NoteTreeNode(item.id, item.text, vscode.TreeItemCollapsibleState.None)
3132
);
3233
}

src/utils.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ export const refreshHistoryList = async () => {
88
} else {
99
store.history = [{}];
1010
}
11+
12+
};
13+
14+
export const refreshLoginStatus = async () => {
15+
store.isLogin = await checkLogin();
16+
};
17+
18+
export const refreshLoginCredential = async (context: vscode.ExtensionContext) => {
19+
if (!(await checkLogin())) {
20+
context.globalState.update('email', undefined);
21+
context.globalState.update('password', undefined);
22+
}
1123
};
1224

1325
export const checkLogin = async () => {
@@ -17,7 +29,7 @@ export const checkLogin = async () => {
1729
export const login = async (context: vscode.ExtensionContext) => {
1830
const { email, password } = getLoginCredential(context);
1931
if (!email || !password) {
20-
vscode.window.showInformationMessage('Please enter your email and password to use HackMD extension!')
32+
vscode.window.showInformationMessage('Please enter your email and password to use HackMD extension!');
2133
return;
2234
}
2335
await API.login(email, password);

0 commit comments

Comments
 (0)