Skip to content

Commit fff7e23

Browse files
committed
WIP: token saving.
1 parent a7b604d commit fff7e23

File tree

5 files changed

+353
-34
lines changed

5 files changed

+353
-34
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
"dependencies": {
118118
"axios": "^0.21.0",
119119
"isomorphic-git": "^1.8.0",
120+
"keytar": "^7.1.0",
120121
"querystring": "^0.2.0",
121122
"react": "^17.0.1",
122123
"react-dom": "^17.0.1"

src/init.ts

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import MRTreeDataProvider from './trees/mr';
55
import toast from './utils/toast';
66
import { getMRUrl } from './utils/repo';
77
import ACTIONS, { dispatch } from './utils/actions';
8-
import { IDepot, IMRItem, IOAuthResponse } from './typings/common';
8+
import { IDepot, IMRItem, IOAuthResponse, ITokenType } from './typings/common';
99
import * as DCloudService from './services/dcloud';
1010

1111
const { registerCommand } = hx.commands;
@@ -62,28 +62,23 @@ export function createTreeViews(context: IContext) {
6262
}),
6363
);
6464

65-
hx.authorize
66-
.login({
67-
scopes: ['basic', 'email', 'phone'],
68-
appId: DCloudService.appId,
69-
})
70-
.then(async (param: IOAuthResponse) => {
71-
const { code, error } = param;
72-
if (error) {
73-
console.log(error);
74-
return;
75-
}
76-
console.info(code);
65+
initialize();
66+
}
7767

78-
try {
79-
const token = await DCloudService.applyForToken(code);
80-
const resp = await DCloudService.fetchUser(token.data.access_token);
81-
console.info(resp);
82-
toast.info(`logged in as DCloud user: ${resp.data.nickname} ${resp.data.email}`);
83-
} catch (err) {
84-
console.error(err);
85-
}
86-
});
68+
async function initialize() {
69+
try {
70+
let aToken = await DCloudService.readToken(ITokenType.AccessToken);
71+
if (!aToken) {
72+
const code = await DCloudService.grantForUserInfo();
73+
const tokenResult = await DCloudService.applyForToken(code);
74+
aToken = tokenResult.data.access_token;
75+
}
76+
const resp = await DCloudService.fetchUser(aToken);
77+
console.info(resp);
78+
toast.info(`logged in as DCloud user: ${resp.data.nickname} ${resp.data.email}`);
79+
} catch (err) {
80+
console.error(err);
81+
}
8782
}
8883

8984
export function workspaceInit() {

src/services/dcloud.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
import keytar from 'keytar';
2+
import hx from 'hbuilderx';
3+
14
import axios from '../utils/axios';
2-
import { ITokenResponse, IDCloudUser } from '../typings/common';
5+
import { ITokenResponse, IDCloudUser, ITokenType, IOAuthResponse } from '../typings/common';
36

47
const appSecret = `eLEDnBuT258P1OmRx1OVFSCj4SZXom`;
58
export const appId = `gZTr3CuevT`;
69

7-
export const applyForToken = async (code: string) => {
10+
export const applyForToken = async (code: string | null) => {
811
try {
12+
if (!code) {
13+
throw new Error(`no code provided.`);
14+
}
15+
916
const resp: ITokenResponse = await axios.get(`https://ide.dcloud.net.cn/dcloudOauthv2/accessToken`, {
1017
params: {
1118
code,
@@ -17,6 +24,9 @@ export const applyForToken = async (code: string) => {
1724
if (resp.ret) {
1825
return Promise.reject(resp);
1926
}
27+
28+
await keytar.setPassword(appId, ITokenType.AccessToken, resp.data.access_token);
29+
await keytar.setPassword(appId, ITokenType.RefreshToken, resp.data.refresh_token);
2030
return resp;
2131
} catch (e) {
2232
return Promise.reject(e);
@@ -39,3 +49,33 @@ export const fetchUser = async (accessToken: string) => {
3949
return Promise.reject(e);
4050
}
4151
};
52+
53+
export const setToken = async (name: ITokenType, value: string) => {
54+
return await keytar.setPassword(appId, name, value);
55+
};
56+
57+
export const readToken = async (name: ITokenType) => {
58+
const val = await keytar.getPassword(appId, name);
59+
if (!val) {
60+
return Promise.reject(null);
61+
}
62+
63+
return val;
64+
};
65+
66+
export const grantForUserInfo = (): Promise<string | null> =>
67+
new Promise((resolve, reject) => {
68+
hx.authorize
69+
.login({
70+
scopes: ['basic', 'email', 'phone'],
71+
appId: appId,
72+
})
73+
.then((param: IOAuthResponse) => {
74+
const { code, error } = param;
75+
if (error || !code) {
76+
return reject(null);
77+
}
78+
79+
return resolve(code);
80+
});
81+
});

src/typings/common.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,8 @@ export interface IDCloudUser {
8484
phone: string;
8585
};
8686
}
87+
88+
export enum ITokenType {
89+
AccessToken = `accessToken`,
90+
RefreshToken = `refreshToken`,
91+
}

0 commit comments

Comments
 (0)