Skip to content

Commit 44c0e92

Browse files
committed
feat(oauth): retrieve coding user data.
1 parent 96e1f2f commit 44c0e92

File tree

5 files changed

+37
-24
lines changed

5 files changed

+37
-24
lines changed

src/extension.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import CodingServer from './services/codingServer';
44
import ACTIONS, { dispatch } from './utils/actions';
55
import { proxyCtx } from './utils/proxy';
66
import toast from './utils/toast';
7+
import { readConfig } from './services/dcloud';
78

89
const accessToken = '7e4d9d17f87875e731d536d13635a700ddf52b12';
910
const user = {
@@ -21,6 +22,10 @@ async function activate(context: IContext) {
2122
const webviewProvider = new WebviewProvider();
2223
const repoInfo = await CodingServer.getRepoParams();
2324
console.log('repoInfo ==> ', repoInfo);
25+
const token = await readConfig(`token`);
26+
if (!token) {
27+
toast.warn(`请先登录 CODING`);
28+
}
2429

2530
const codingServer = new CodingServer(
2631
{
@@ -39,6 +44,8 @@ async function activate(context: IContext) {
3944
codingServer,
4045
depots: [],
4146
selectedDepot: null,
47+
token,
48+
repoInfo,
4249
},
4350
});
4451

src/init.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,6 @@ export function createTreeViews(context: IContext) {
7373
treeDataProvider: new DepotTreeDataProvider(context),
7474
}),
7575
);
76-
77-
initialize();
78-
}
79-
80-
async function initialize() {
81-
try {
82-
let hbToken = await DCloudService.readToken(`hbToken`);
83-
console.log(`hb token: ${hbToken}`);
84-
if (!hbToken) {
85-
const code = await DCloudService.grantForUserInfo();
86-
const tokenResult = await DCloudService.applyForToken(code);
87-
hbToken = tokenResult.data.access_token;
88-
}
89-
const resp = await DCloudService.fetchUser(hbToken);
90-
console.info(resp);
91-
toast.info(`logged in as DCloud user: ${resp.data.nickname} ${resp.data.email}`);
92-
} catch (err) {
93-
console.error(err);
94-
}
9576
}
9677

9778
export function workspaceInit() {
@@ -110,8 +91,29 @@ export function clear(context: IContext) {
11091
context.subscriptions.forEach(({ dispose }) => dispose());
11192
}
11293

94+
async function initCredentials(context: IContext) {
95+
try {
96+
let hbToken = await DCloudService.readConfig(`hbToken`);
97+
if (!hbToken) {
98+
const code = await DCloudService.grantForUserInfo();
99+
const tokenResult = await DCloudService.applyForToken(code);
100+
hbToken = tokenResult.data.access_token;
101+
}
102+
const resp = await DCloudService.fetchUser(hbToken);
103+
toast.info(`logged in as DCloud user: ${resp.data.nickname} ${resp.data.email}`);
104+
const {
105+
ctx: { codingServer, repoInfo, token },
106+
} = context;
107+
const userData = await codingServer.getUserInfo(repoInfo.team, token);
108+
toast.info(`logged in as coding user: ${userData.name} @ ${userData.team}`);
109+
} catch (err) {
110+
console.error(err);
111+
}
112+
}
113+
113114
export default function init(context: IContext) {
114115
registerCommands(context);
115116
createTreeViews(context);
116117
workspaceInit();
118+
initCredentials(context);
117119
}

src/services/codingServer.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ export default class CodingServer {
4949
const result = await axios({
5050
method: 'get',
5151
url: `https://${team}.coding.net/api/current_user`,
52-
headers: this.getHeaders(),
52+
headers: this.getHeaders(token),
5353
});
5454

55+
if (result.code) {
56+
return Promise.reject(result);
57+
}
58+
5559
return result?.data;
5660
} catch (err) {
5761
console.error(err);

src/services/dcloud.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export const setConfig = async (prop: string, value: string) => {
6464
}
6565
};
6666

67-
export const readToken = async (prop: string) => {
67+
export const readConfig = async (prop: string) => {
6868
const codingPlugin = hx.workspace.getConfiguration(`codingPlugin`);
6969
const token = codingPlugin.get(prop, ``);
7070
return token;

src/utils/toast.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import hx from 'hbuilderx';
22

3-
export const info = (msg: string, buttons?: string[]): Promise<string> => {
3+
export const info = (msg: string, buttons?: string[]) => {
44
return hx.window.showInformationMessage(msg, buttons);
55
};
66

7-
export const warn = (msg: string, buttons?: string[]): Promise<string> => {
7+
export const warn = (msg: string, buttons?: string[]) => {
88
return hx.window.showWarningMessage(msg, buttons);
99
};
1010

11-
export const error = (msg: string, buttons?: string[]): Promise<string> => {
11+
export const error = (msg: string, buttons?: string[]) => {
1212
return hx.window.showErrorMessage(msg, buttons);
1313
};
1414

0 commit comments

Comments
 (0)