@pubinfo-pr/module-auth 0.203.2 → 0.203.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -3,20 +3,20 @@ export { LoginWithFourA } from './components/LoginWithFourA';
3
3
  export { createAuth } from './core';
4
4
  export * from './interface';
5
5
  /**
6
- * @deprecated 认证, 请使用 `AuthClient.authenticate` 方法替代
6
+ * @deprecated 认证, 请使用 `authClient.authenticate` 方法替代
7
7
  */
8
8
  export declare const authenticate: (id: import("./interface").ThirdParty) => Promise<any>;
9
9
  /**
10
- * @deprecated 重定向至统一登录页, 请使用 `AuthClient.redirect` 方法替代
10
+ * @deprecated 重定向至统一登录页, 请使用 `authClient.redirect` 方法替代
11
11
  */
12
12
  export declare const redirect: () => void;
13
13
  /**
14
- * @deprecated 生成二维码, 请使用 `AuthClient.renderQRCode` 方法替代
14
+ * @deprecated 生成二维码, 请使用 `authClient.renderQRCode` 方法替代
15
15
  */
16
16
  export declare const renderQRCode: (id: import("./interface").ThirdParty, options: {
17
17
  id: string;
18
18
  } & import("./interface").Recordable) => void;
19
19
  /**
20
- * @deprecated 前往登录, 请使用 `AuthClient.signIn` 方法替代
20
+ * @deprecated 前往登录, 请使用 `authClient.signIn` 方法替代
21
21
  */
22
22
  export declare const signIn: (id: import("./interface").ThirdParty, options?: import("./interface").Recordable) => Promise<any> | any;
package/dist/index.js CHANGED
@@ -22,10 +22,13 @@ function createAuth(e) {
22
22
  if (!t) throw Error(`Provider '${e}' is not found.`);
23
23
  return t;
24
24
  }
25
- let a = {
25
+ let a = { data: {} }, o = {
26
26
  async signIn(e, t) {
27
27
  let n = i(e);
28
- if (typeof n.signIn == "function") return await n.signIn(t);
28
+ if (typeof n.signIn == "function") {
29
+ let e = await n.signIn(t);
30
+ return a.data = e, e;
31
+ }
29
32
  if (["oauth", "custom"].includes(n.type)) {
30
33
  let e = createUrlByProvider(n);
31
34
  window.location.replace(e);
@@ -58,8 +61,8 @@ function createAuth(e) {
58
61
  },
59
62
  getProvider: i
60
63
  };
61
- for (let e of r) Object.assign(a, e.extend?.({}));
62
- return a;
64
+ for (let e of r) Object.assign(o, e.extend?.(a));
65
+ return o;
63
66
  }
64
67
  const PageAuth = defineComponent({
65
68
  props: {
@@ -25,10 +25,13 @@ export interface CreateAuthModule {
25
25
  signIn?: RouteLocationRaw;
26
26
  };
27
27
  }
28
+ export interface PluginContext {
29
+ data: Recordable;
30
+ }
28
31
  export interface PluginOptions<Extend = any> {
29
32
  /** 唯一值 */
30
33
  id: string;
31
- extend?: (context: any) => Extend;
34
+ extend?: (context: PluginContext) => Extend;
32
35
  }
33
36
  /** 第三方类型 */
34
37
  export type ThirdParty = string;
@@ -8,9 +8,19 @@ interface TwoFactorExtend {
8
8
  }>;
9
9
  };
10
10
  }
11
- export interface TwoFactorOptions {
12
- /** 请求实例 */
11
+ interface DefaultOptOptions {
13
12
  request: RequestInstance;
13
+ sendOtp?: TwoFactorExtend['twoFactor']['sendOtp'];
14
+ verifyOtp?: TwoFactorExtend['twoFactor']['verifyOtp'];
14
15
  }
16
+ interface CustomOptOptions {
17
+ request: never;
18
+ sendOtp: TwoFactorExtend['twoFactor']['sendOtp'];
19
+ verifyOtp: TwoFactorExtend['twoFactor']['verifyOtp'];
20
+ }
21
+ export type TwoFactorOptions = DefaultOptOptions | CustomOptOptions;
22
+ /**
23
+ * `双因子认证插件`
24
+ */
15
25
  export declare function twoFactor(options: TwoFactorOptions): PluginOptions<TwoFactorExtend>;
16
26
  export {};
package/dist/plugins.js CHANGED
@@ -1,23 +1,26 @@
1
1
  function twoFactor(e) {
2
- let { request: t } = e;
2
+ let { request: t, sendOtp: n, verifyOtp: r } = e;
3
3
  return {
4
4
  id: "two-factor",
5
- extend() {
5
+ extend(e) {
6
6
  return { twoFactor: {
7
- sendOtp(e) {
8
- return t.Post("/auth/2fa/prepare", {
7
+ sendOtp: (r) => {
8
+ let i = e.data?.data?.login2faCode;
9
+ return n ? n(r) : t.Post("/auth/2fa/prepare", {
9
10
  fa2Type: "sms",
10
- login2faCode: void 0,
11
- ...e
11
+ login2faCode: i,
12
+ ...r
12
13
  });
13
14
  },
14
- verifyOtp(e) {
15
- let { code: n, ...r } = e ?? {};
15
+ verifyOtp: (n) => {
16
+ let i = e.data?.data?.login2faCode;
17
+ if (r) return r(n);
18
+ let { code: a, ...o } = n ?? {};
16
19
  return t.Post("/auth/2fa/verify", {
17
20
  fa2Type: "sms",
18
- login2faCode: void 0,
19
- verifyCode: n,
20
- ...r
21
+ login2faCode: i,
22
+ verifyCode: a,
23
+ ...o
21
24
  });
22
25
  }
23
26
  } };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pubinfo-pr/module-auth",
3
3
  "type": "module",
4
- "version": "0.203.2",
4
+ "version": "0.203.3",
5
5
  "exports": {
6
6
  ".": {
7
7
  "types": "./dist/index.d.ts",
@@ -31,10 +31,10 @@
31
31
  "node": "^20.19.0 || >=22.12.0"
32
32
  },
33
33
  "peerDependencies": {
34
- "pubinfo-pr": "0.203.2"
34
+ "pubinfo-pr": "0.203.3"
35
35
  },
36
36
  "devDependencies": {
37
- "pubinfo-pr": "0.203.2"
37
+ "pubinfo-pr": "0.203.3"
38
38
  },
39
39
  "scripts": {
40
40
  "dev": "pubinfo build --watch",
package/src/core.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { PluginOptions, ProviderOptions, Recordable, ThirdParty } from './interface';
1
+ import type { PluginContext, PluginOptions, ProviderOptions, Recordable, ThirdParty } from './interface';
2
2
  import { createUrl, createUrlByProvider } from './helper';
3
3
 
4
4
  export interface CreateAuthOptions<Plugins extends PluginOptions[]> {
@@ -68,6 +68,10 @@ export function createAuth<Plugins extends PluginOptions[]>(options: CreateAuthO
68
68
  return provider;
69
69
  }
70
70
 
71
+ const context: PluginContext = {
72
+ data: {},
73
+ };
74
+
71
75
  const authClient = {
72
76
  /**
73
77
  * 前往登录
@@ -78,7 +82,9 @@ export function createAuth<Plugins extends PluginOptions[]>(options: CreateAuthO
78
82
  const provider = getProvider(id);
79
83
 
80
84
  if (typeof provider.signIn === 'function') {
81
- return await provider.signIn(options);
85
+ const res = await provider.signIn(options);
86
+ context.data = res;
87
+ return res;
82
88
  }
83
89
 
84
90
  if (['oauth', 'custom'].includes(provider.type)) {
@@ -147,7 +153,7 @@ export function createAuth<Plugins extends PluginOptions[]>(options: CreateAuthO
147
153
  };
148
154
 
149
155
  for (const plugin of plugins) {
150
- Object.assign(authClient, plugin.extend?.({}));
156
+ Object.assign(authClient, plugin.extend?.(context));
151
157
  }
152
158
 
153
159
  return authClient as AuthClient & PluginExtend<Plugins>;
package/src/index.ts CHANGED
@@ -6,21 +6,21 @@ export { createAuth } from './core';
6
6
  export * from './interface';
7
7
 
8
8
  /**
9
- * @deprecated 认证, 请使用 `AuthClient.authenticate` 方法替代
9
+ * @deprecated 认证, 请使用 `authClient.authenticate` 方法替代
10
10
  */
11
11
  export const authenticate = AuthClient.authenticate;
12
12
 
13
13
  /**
14
- * @deprecated 重定向至统一登录页, 请使用 `AuthClient.redirect` 方法替代
14
+ * @deprecated 重定向至统一登录页, 请使用 `authClient.redirect` 方法替代
15
15
  */
16
16
  export const redirect = AuthClient.redirect;
17
17
 
18
18
  /**
19
- * @deprecated 生成二维码, 请使用 `AuthClient.renderQRCode` 方法替代
19
+ * @deprecated 生成二维码, 请使用 `authClient.renderQRCode` 方法替代
20
20
  */
21
21
  export const renderQRCode = AuthClient.renderQRCode;
22
22
 
23
23
  /**
24
- * @deprecated 前往登录, 请使用 `AuthClient.signIn` 方法替代
24
+ * @deprecated 前往登录, 请使用 `authClient.signIn` 方法替代
25
25
  */
26
26
  export const signIn = AuthClient.signIn;
package/src/interface.ts CHANGED
@@ -33,11 +33,15 @@ export interface CreateAuthModule {
33
33
  }
34
34
  }
35
35
 
36
+ export interface PluginContext {
37
+ data: Recordable
38
+ }
39
+
36
40
  export interface PluginOptions<Extend = any> {
37
41
  /** 唯一值 */
38
42
  id: string
39
43
 
40
- extend?: (context: any) => Extend
44
+ extend?: (context: PluginContext) => Extend
41
45
  }
42
46
 
43
47
  /** 第三方类型 */
@@ -8,42 +8,62 @@ interface TwoFactorExtend {
8
8
  }
9
9
  }
10
10
 
11
- export interface TwoFactorOptions {
12
- /** 请求实例 */
11
+ interface DefaultOptOptions {
13
12
  request: RequestInstance
13
+ sendOtp?: TwoFactorExtend['twoFactor']['sendOtp']
14
+ verifyOtp?: TwoFactorExtend['twoFactor']['verifyOtp']
14
15
  }
15
16
 
17
+ interface CustomOptOptions {
18
+ request: never
19
+ sendOtp: TwoFactorExtend['twoFactor']['sendOtp']
20
+ verifyOtp: TwoFactorExtend['twoFactor']['verifyOtp']
21
+ }
22
+
23
+ export type TwoFactorOptions = DefaultOptOptions | CustomOptOptions;
24
+
25
+ /**
26
+ * `双因子认证插件`
27
+ */
16
28
  export function twoFactor(options: TwoFactorOptions): PluginOptions<TwoFactorExtend> {
17
- const { request } = options;
29
+ const { request, sendOtp, verifyOtp } = options;
18
30
 
19
31
  return {
20
32
  id: 'two-factor',
21
- extend() {
22
- // TODO 通过 context 拿到账号密码登录接口返回的值
23
- let login2faCode;
33
+ extend(context) {
34
+ const twoFactor: TwoFactorExtend['twoFactor'] = {
35
+ sendOtp: (payload) => {
36
+ const login2faCode = context.data?.data?.login2faCode;
37
+ if (sendOtp) {
38
+ return sendOtp(payload);
39
+ }
24
40
 
25
- return {
26
- twoFactor: {
27
- sendOtp(payload) {
28
- return request.Post('/auth/2fa/prepare', {
29
- fa2Type: 'sms',
30
- login2faCode,
31
- ...payload,
32
- });
33
- },
34
-
35
- verifyOtp(payload) {
36
- const { code, ...params } = payload ?? {};
37
-
38
- return request.Post('/auth/2fa/verify', {
39
- fa2Type: 'sms',
40
- login2faCode,
41
- verifyCode: code,
42
- ...params,
43
- });
44
- },
41
+ return request.Post('/auth/2fa/prepare', {
42
+ fa2Type: 'sms',
43
+ login2faCode,
44
+ ...payload,
45
+ });
46
+ },
47
+
48
+ verifyOtp: (payload) => {
49
+ const login2faCode = context.data?.data?.login2faCode;
50
+ if (verifyOtp) {
51
+ return verifyOtp(payload);
52
+ }
53
+
54
+ const { code, ...params } = payload ?? {};
55
+ return request.Post('/auth/2fa/verify', {
56
+ fa2Type: 'sms',
57
+ login2faCode,
58
+ verifyCode: code,
59
+ ...params,
60
+ });
45
61
  },
46
62
  };
63
+
64
+ return {
65
+ twoFactor,
66
+ };
47
67
  },
48
68
  };
49
69
  }