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

Commit c86a543

Browse files
committed
support login get url function
1 parent f843265 commit c86a543

5 files changed

Lines changed: 56 additions & 47 deletions

File tree

lib/utils/auth.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ function createLocalServer() {
143143
}
144144
function getAuthTokenFromWeb(options) {
145145
return __awaiter(this, void 0, void 0, function* () {
146-
console.log(options);
147-
const { authUrl } = options;
146+
const { getAuthUrl } = options;
148147
return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
149148
const loading = utils_1.loadingFactory();
150149
loading.start('正在打开腾讯云获取授权');
@@ -156,9 +155,15 @@ function getAuthTokenFromWeb(options) {
156155
if (!mac) {
157156
throw new error_1.CloudBaseError('获取 Mac 地址失败,无法登录!');
158157
}
159-
const CliAuthUrl = authUrl ||
160-
`${CliAuthBaseUrl}?port=${port}&hash=${hash}&mac=${mac}&os=${os}`;
161-
yield open_1.default(CliAuthUrl);
158+
let cliAuthUrl = `${CliAuthBaseUrl}?port=${port}&hash=${hash}&mac=${mac}&os=${os}`;
159+
if (getAuthUrl) {
160+
try {
161+
cliAuthUrl = getAuthUrl(`${CliAuthBaseUrl}?port=${port}&hash=${hash}&mac=${mac}&os=${os}`);
162+
}
163+
catch (error) {
164+
}
165+
}
166+
yield open_1.default(cliAuthUrl);
162167
loading.succeed('已打开云开发 CLI 授权页面,请在云开发 CLI 授权页面同意授权!');
163168
server.on('request', (req, res) => {
164169
const { url } = req;

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ export interface ILoginOptions {
168168
key?: boolean
169169
secretId?: string
170170
secretKey?: string
171-
// 浏览器登录打开的链接
172-
authUrl?: string
171+
// 修改浏览器登录打开的链接
172+
getAuthUrl?: (url: string) => string
173173
}
174174

175175
export interface FunctionContext {

src/utils/auth.ts

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,8 @@ async function createLocalServer(): Promise<ServerRes> {
156156
}
157157

158158
// 打开云开发控制台,获取授权
159-
export async function getAuthTokenFromWeb(
160-
options: ILoginOptions
161-
): Promise<Credential> {
162-
console.log(options)
163-
const { authUrl } = options
159+
export async function getAuthTokenFromWeb(options: ILoginOptions): Promise<Credential> {
160+
const { getAuthUrl } = options
164161
return new Promise(async (resolve, reject) => {
165162
const loading = loadingFactory()
166163
loading.start('正在打开腾讯云获取授权')
@@ -175,39 +172,43 @@ export async function getAuthTokenFromWeb(
175172
throw new CloudBaseError('获取 Mac 地址失败,无法登录!')
176173
}
177174

178-
const CliAuthUrl =
179-
authUrl ||
180-
`${CliAuthBaseUrl}?port=${port}&hash=${hash}&mac=${mac}&os=${os}`
181-
await open(CliAuthUrl)
182-
183-
loading.succeed(
184-
'已打开云开发 CLI 授权页面,请在云开发 CLI 授权页面同意授权!'
185-
)
186-
187-
server.on(
188-
'request',
189-
(req: IncomingMessage, res: ServerResponse) => {
190-
const { url } = req
191-
const { query } = queryString.parseUrl(url)
192-
193-
// CORS
194-
res.writeHead(200, {
195-
'Access-Control-Allow-Origin': '*',
196-
'Content-Type': 'text/plain',
197-
// 立即关闭 http 连接
198-
Connection: 'close'
199-
})
200-
201-
res.end('ok')
202-
203-
// 防止接受到异常请求导致本地服务关闭
204-
if (query && query.tmpToken) {
205-
server.close()
206-
}
207-
208-
resolve(query as Credential)
175+
let cliAuthUrl = `${CliAuthBaseUrl}?port=${port}&hash=${hash}&mac=${mac}&os=${os}`
176+
177+
if (getAuthUrl) {
178+
try {
179+
cliAuthUrl = getAuthUrl(
180+
`${CliAuthBaseUrl}?port=${port}&hash=${hash}&mac=${mac}&os=${os}`
181+
)
182+
} catch (error) {
183+
// 忽略错误
209184
}
210-
)
185+
}
186+
187+
await open(cliAuthUrl)
188+
189+
loading.succeed('已打开云开发 CLI 授权页面,请在云开发 CLI 授权页面同意授权!')
190+
191+
server.on('request', (req: IncomingMessage, res: ServerResponse) => {
192+
const { url } = req
193+
const { query } = queryString.parseUrl(url)
194+
195+
// CORS
196+
res.writeHead(200, {
197+
'Access-Control-Allow-Origin': '*',
198+
'Content-Type': 'text/plain',
199+
// 立即关闭 http 连接
200+
Connection: 'close'
201+
})
202+
203+
res.end('ok')
204+
205+
// 防止接受到异常请求导致本地服务关闭
206+
if (query && query.tmpToken) {
207+
server.close()
208+
}
209+
210+
resolve(query as Credential)
211+
})
211212
} catch (err) {
212213
logger.error(err.message)
213214
loading.fail('获取授权失败!')

test/login/index.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { login } from '../../src/auth'
22

33
test('登录:cloudbase login', async () => {
44
const res = await login({
5-
authUrl: 'https://google.com'
5+
getAuthUrl: url => {
6+
console.log(url)
7+
return url
8+
}
69
})
710
expect(res.code).toEqual('SUCCESS')
8-
})
11+
})

types/types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ export interface ILoginOptions {
138138
key?: boolean;
139139
secretId?: string;
140140
secretKey?: string;
141-
authUrl?: string;
141+
getAuthUrl?: (url: string) => string;
142142
}
143143
export interface FunctionContext {
144144
name: string;

0 commit comments

Comments
 (0)