Skip to content
This repository was archived by the owner on May 6, 2025. It is now read-only.

Commit cd88897

Browse files
committed
support specify login auth url
1 parent 0f71f16 commit cd88897

9 files changed

Lines changed: 34 additions & 20 deletions

File tree

lib/auth/login.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ const LoginRes = {
4545
};
4646
}
4747
};
48-
function loginWithToken() {
48+
function loginWithToken(options) {
4949
return __awaiter(this, void 0, void 0, function* () {
5050
const isLogin = yield utils_1.checkAndGetCredential();
5151
if (isLogin) {
5252
return LoginRes.SUCCESS;
5353
}
5454
let credential;
5555
try {
56-
credential = yield utils_1.getAuthTokenFromWeb();
56+
credential = yield utils_1.getAuthTokenFromWeb(options);
5757
}
5858
catch (e) {
5959
return LoginRes.UNKNOWN_ERROR(e.message);
@@ -92,14 +92,14 @@ function loginWithKey(secretId, secretKey) {
9292
});
9393
}
9494
exports.loginWithKey = loginWithKey;
95-
function login(options) {
95+
function login(options = {}) {
9696
return __awaiter(this, void 0, void 0, function* () {
9797
if (options && options.key) {
9898
const { secretId, secretKey } = options;
9999
return yield loginWithKey(secretId, secretKey);
100100
}
101101
else {
102-
return yield loginWithToken();
102+
return yield loginWithToken(options);
103103
}
104104
});
105105
}

lib/utils/auth.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@ function createLocalServer() {
141141
}));
142142
});
143143
}
144-
function getAuthTokenFromWeb() {
144+
function getAuthTokenFromWeb(options) {
145145
return __awaiter(this, void 0, void 0, function* () {
146+
console.log(options);
147+
const { authUrl } = options;
146148
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
147149
const loading = utils_1.loadingFactory();
148150
loading.start('正在打开腾讯云获取授权');
@@ -154,7 +156,8 @@ function getAuthTokenFromWeb() {
154156
if (!mac) {
155157
throw new error_1.CloudBaseError('获取 Mac 地址失败,无法登录!');
156158
}
157-
const CliAuthUrl = `${CliAuthBaseUrl}?port=${port}&hash=${hash}&mac=${mac}&os=${os}`;
159+
const CliAuthUrl = authUrl ||
160+
`${CliAuthBaseUrl}?port=${port}&hash=${hash}&mac=${mac}&os=${os}`;
158161
yield open_1.default(CliAuthUrl);
159162
loading.succeed('已打开云开发 CLI 授权页面,请在云开发 CLI 授权页面同意授权!');
160163
server.on('request', (req, res) => {

src/auth/login.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const LoginRes = {
4545
}
4646

4747
// 打开腾讯云-云开发控制台,通过获取临时密钥登录,临时密钥可续期,最长时间为 1 个月
48-
export async function loginWithToken() {
48+
export async function loginWithToken(options: ILoginOptions) {
4949
const isLogin = await checkAndGetCredential()
5050

5151
if (isLogin) {
@@ -55,7 +55,7 @@ export async function loginWithToken() {
5555
let credential
5656

5757
try {
58-
credential = await getAuthTokenFromWeb()
58+
credential = await getAuthTokenFromWeb(options)
5959
} catch (e) {
6060
return LoginRes.UNKNOWN_ERROR(e.message)
6161
}
@@ -98,12 +98,12 @@ export async function loginWithKey(secretId?: string, secretKey?: string) {
9898
}
9999

100100
export async function login(
101-
options?: ILoginOptions
101+
options: ILoginOptions = {}
102102
): Promise<{ code: string; msg: string }> {
103103
if (options && options.key) {
104104
const { secretId, secretKey } = options
105105
return await loginWithKey(secretId, secretKey)
106106
} else {
107-
return await loginWithToken()
107+
return await loginWithToken(options)
108108
}
109109
}

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,11 @@ export interface IFunctionTriggerOptions {
165165
}
166166

167167
export interface ILoginOptions {
168-
key: boolean
168+
key?: boolean
169169
secretId?: string
170170
secretKey?: string
171+
// 浏览器登录打开的链接
172+
authUrl?: string
171173
}
172174

173175
export interface FunctionContext {

src/utils/auth.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import queryString from 'query-string'
66
import open from 'open'
77
import address from 'address'
88

9-
import { Credential, AuthSecret } from '../types'
9+
import { Credential, AuthSecret, ILoginOptions } from '../types'
1010
export { printHorizontalTable } from './cli-table'
1111
import { authStore } from './store'
1212
import { ConfigItems } from '../constant'
@@ -156,7 +156,11 @@ async function createLocalServer(): Promise<ServerRes> {
156156
}
157157

158158
// 打开云开发控制台,获取授权
159-
export async function getAuthTokenFromWeb(): Promise<Credential> {
159+
export async function getAuthTokenFromWeb(
160+
options: ILoginOptions
161+
): Promise<Credential> {
162+
console.log(options)
163+
const { authUrl } = options
160164
return new Promise(async (resolve, reject) => {
161165
const loading = loadingFactory()
162166
loading.start('正在打开腾讯云获取授权')
@@ -171,7 +175,9 @@ export async function getAuthTokenFromWeb(): Promise<Credential> {
171175
throw new CloudBaseError('获取 Mac 地址失败,无法登录!')
172176
}
173177

174-
const CliAuthUrl = `${CliAuthBaseUrl}?port=${port}&hash=${hash}&mac=${mac}&os=${os}`
178+
const CliAuthUrl =
179+
authUrl ||
180+
`${CliAuthBaseUrl}?port=${port}&hash=${hash}&mac=${mac}&os=${os}`
175181
await open(CliAuthUrl)
176182

177183
loading.succeed(

test/login/index.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { login } from '../../src/auth'
22

33
test('登录:cloudbase login', async () => {
4-
const res = await login()
4+
const res = await login({
5+
authUrl: 'https://google.com'
6+
})
57
expect(res.code).toEqual('SUCCESS')
6-
})
8+
})

types/auth/login.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ILoginOptions } from '../types';
2-
export declare function loginWithToken(): Promise<{
2+
export declare function loginWithToken(options: ILoginOptions): Promise<{
33
code: string;
44
msg: string;
55
}>;

types/types.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ export interface IFunctionTriggerOptions {
135135
envId: string;
136136
}
137137
export interface ILoginOptions {
138-
key: boolean;
138+
key?: boolean;
139139
secretId?: string;
140140
secretKey?: string;
141+
authUrl?: string;
141142
}
142143
export interface FunctionContext {
143144
name: string;

types/utils/auth.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Credential, AuthSecret } from '../types';
1+
import { Credential, AuthSecret, ILoginOptions } from '../types';
22
export { printHorizontalTable } from './cli-table';
33
export declare function refreshTmpToken(metaData: Credential & {
44
isLogout?: boolean;
55
}): Promise<Credential>;
66
export declare function getCredentialData(): Credential;
77
export declare function getCredentialWithoutCheck(): Promise<AuthSecret>;
8-
export declare function getAuthTokenFromWeb(): Promise<Credential>;
8+
export declare function getAuthTokenFromWeb(options: ILoginOptions): Promise<Credential>;

0 commit comments

Comments
 (0)