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

Commit d72edda

Browse files
committed
feat(WIP): fast login
1 parent cf36798 commit d72edda

File tree

6 files changed

+81
-17
lines changed

6 files changed

+81
-17
lines changed
Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
import { IFastLoginOptions } from './type';
1+
import { IFastLoginOptions, ILoginCallbackProps } from './type';
22
import { ENV } from './env';
33

4-
let fastLoginClient: typeof window.FastLogin = null;
5-
64
const loadFastLoginScripts = (options: IFastLoginOptions): Promise<typeof window.FastLogin> => {
75
const env = options.env || 'prod';
86
const fastLoginUrl = ENV[env] || ENV.prod;
97

108
/* eslint-disable no-undef */
119
return new Promise((resolve, reject) => {
12-
if (fastLoginClient) {
13-
resolve(fastLoginClient);
10+
if (window.FastLogin) {
11+
resolve(window.FastLogin);
1412
return;
1513
}
1614

1715
const script = document.createElement('script');
1816

1917
script.onload = () => {
20-
fastLoginClient = window.FastLogin;
21-
// 必填
2218
// @ts-ignore
2319
window.FastLoginContext = {
2420
tenantName: "console"
2521
};
26-
resolve(fastLoginClient)
22+
resolve(window.FastLogin)
2723
}
2824

2925
script.onerror = () => {
@@ -40,22 +36,44 @@ const loadFastLoginScripts = (options: IFastLoginOptions): Promise<typeof window
4036
const open = async (options: IFastLoginOptions) => {
4137
const fastLogin = await loadFastLoginScripts(options);
4238

43-
return new Promise<void>((resolve) => {
39+
return new Promise<ILoginCallbackProps>((resolve, reject) => {
4440
fastLogin('show', [{
45-
loginCallback: () => {
46-
resolve();
41+
loginCallback: (result) => {
42+
if (result.success) {
43+
resolve(result);
44+
} else {
45+
reject(result)
46+
}
4747
}
4848
}])
4949
})
5050
}
5151

5252
const render = async (options: IFastLoginOptions) => {
5353
const fastLogin = await loadFastLoginScripts(options);
54-
fastLogin('render', [{}])
54+
return new Promise<ILoginCallbackProps>((resolve, reject) => {
55+
fastLogin('render', [{
56+
// @ts-ignore
57+
type: "one_login", // one_login, password, qr
58+
loginCallback: (result) => {
59+
if (result.success) {
60+
resolve(result);
61+
} else {
62+
reject(result)
63+
}
64+
}
65+
}])
66+
})
67+
}
68+
69+
const unmount = async (options: IFastLoginOptions) => {
70+
const fastLogin = await loadFastLoginScripts(options);
71+
fastLogin('unmountDialog', [{}])
5572
}
5673

5774

5875
export {
5976
open,
6077
render,
78+
unmount,
6179
}

packages/console-utils/fastlogin/src/type.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export interface IFastLoginOptions {
22
env?: 'prod' | 'prepub';
3+
target?: HTMLElement;
34
}
45

56
export interface ILoginCallbackProps {
@@ -15,6 +16,6 @@ export interface IFastLoginProps {
1516

1617
declare global {
1718
interface Window {
18-
FastLogin: (method: 'render' | 'show', [props]: [IFastLoginProps]) => void;
19+
FastLogin: (method: 'render' | 'show' | 'unmountDialog', [props]: [IFastLoginProps]) => void;
1920
}
2021
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
import * as React from 'react';
22
import ReactDOM from 'react-dom';
33
import { storiesOf } from '@storybook/react';
4-
import { open } from '../src';
4+
import { open, unmount } from '../src';
55

66
window.React = React;
77
window.ReactDOM = ReactDOM;
88

99
storiesOf('FastLogin', module)
1010
.add('FastLogin', () => {
11-
const onClick = () => {
12-
open({env: 'prepub'})
11+
const onClick = async () => {
12+
try {
13+
const result = await open({ target: document.querySelector('test')});
14+
console.log(result)
15+
unmount({env: 'prepub'});
16+
} catch {
17+
unmount({env: 'prepub'});
18+
}
1319
}
1420

1521
return (<div id="app-wrapper">
1622
<div id="app">
1723
<button onClick={onClick}>open fast login</button>
24+
<div id="test"/>
1825
</div>
1926
</div>);
2027
})

packages/console-utils/xconsole-service/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"react-dom": "^16.11.0"
2727
},
2828
"devDependencies": {
29-
"@alicloud/console-components": "alpha",
29+
"@alicloud/console-components": "^1.0.0",
30+
"@alicloud/console-fastlogin": "^2.0.0",
3031
"@alicloud/console-toolkit-cli": "^1.0.3",
3132
"@alicloud/console-toolkit-preset-wind-component": "^1.0.6",
3233
"@testing-library/react": "^10.4.7",
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import fastLogin from '@alicloud/console-fastlogin';
2+
import { IResponse, IResponseData } from '../../types';
3+
import { ApiType } from '../../const/index';
4+
5+
async function consoleRiskInterceptor(
6+
response: IResponse<IResponseData>
7+
): Promise<IResponse<IResponseData>> {
8+
const {
9+
data: responseData,
10+
config: { apiType, throwDoubleConfirmError },
11+
} = response;
12+
13+
if (apiType === ApiType.custom) return response;
14+
15+
switch (responseData.code) {
16+
case 'CONSOLE_NEED_LOGIN':
17+
try {
18+
const newResponse = await fastLogin.open()
19+
return newResponse;
20+
} catch (e) {
21+
if (throwDoubleConfirmError) {
22+
e.response = response;
23+
throw e;
24+
}
25+
return response;
26+
}
27+
default:
28+
return response;
29+
}
30+
}
31+
32+
export default consoleRiskInterceptor;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import fastLogin from '@alicloud/console-fastlogin';
2+
3+
const login = async () => {
4+
5+
}

0 commit comments

Comments
 (0)