@pubinfo-pr/module-auth 0.203.1 → 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/src/index.ts CHANGED
@@ -1,78 +1,26 @@
1
- import type { ModuleOptions } from 'pubinfo-pr';
2
- import type { RouteRecordRaw } from 'vue-router';
3
- import type { AuthOptions } from './interface';
4
- import { cleanup } from 'pubinfo-pr';
5
- import { ctx } from './context';
6
- import { authenticate, redirect } from './core';
7
- import { PageAuth } from './pages/auth';
8
-
9
- export function auth(options: AuthOptions): ModuleOptions {
10
- const {
11
- redirectTo = '/',
12
- pages = {},
13
- baseURL,
14
- providers,
15
- } = options;
16
- const { signIn = '/login' } = pages;
17
-
18
- ctx.set({ baseURL, providers });
19
-
20
- return {
21
- name: 'pubinfo-pr:auth',
22
- enforce: 'pre',
23
- setup(ctx) {
24
- const { router } = ctx;
25
-
26
- const ROUTE_AUTH: RouteRecordRaw = {
27
- path: '/auth/:type',
28
- name: 'Auth',
29
- component: PageAuth,
30
- meta: {
31
- whiteList: true,
32
- title: '授权登录',
33
- },
34
- props: route => ({
35
- type: route.params.type,
36
- authenticate,
37
- redirectTo() {
38
- if (typeof redirectTo === 'function') {
39
- redirectTo();
40
- return;
41
- }
42
-
43
- router.push(redirectTo);
44
- },
45
- }),
46
- };
47
-
48
- router.beforeEach((to) => {
49
- // 注册静态页面
50
- if (!router.hasRoute(ROUTE_AUTH.name!)) {
51
- router.addRoute(ROUTE_AUTH);
52
- return to.fullPath;
53
- }
54
-
55
- // 前往单点登录前,清空登录状态
56
- if (to.name === ROUTE_AUTH.name) {
57
- cleanup();
58
- }
59
-
60
- // 前往登录前,重定向至统一登录页(若有)
61
- if (to.path === signIn) {
62
- redirect();
63
- }
64
- });
65
- },
66
- };
67
- }
1
+ import { AuthClient } from './auth';
68
2
 
3
+ export * from './auth';
69
4
  export { LoginWithFourA } from './components/LoginWithFourA';
70
-
71
- export {
72
- authenticate,
73
- redirect,
74
- renderQRCode,
75
- signIn,
76
- } from './core';
77
-
5
+ export { createAuth } from './core';
78
6
  export * from './interface';
7
+
8
+ /**
9
+ * @deprecated 认证, 请使用 `authClient.authenticate` 方法替代
10
+ */
11
+ export const authenticate = AuthClient.authenticate;
12
+
13
+ /**
14
+ * @deprecated 重定向至统一登录页, 请使用 `authClient.redirect` 方法替代
15
+ */
16
+ export const redirect = AuthClient.redirect;
17
+
18
+ /**
19
+ * @deprecated 生成二维码, 请使用 `authClient.renderQRCode` 方法替代
20
+ */
21
+ export const renderQRCode = AuthClient.renderQRCode;
22
+
23
+ /**
24
+ * @deprecated 前往登录, 请使用 `authClient.signIn` 方法替代
25
+ */
26
+ export const signIn = AuthClient.signIn;
package/src/interface.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import type { RouteLocationRaw } from 'vue-router';
2
+ import type { CreateAuthOptions } from './core';
2
3
 
3
4
  export type Recordable = Record<string, any>;
4
5
  export type Fn<T = any, K = any> = (params?: Recordable & T) => Promise<K> | K;
5
6
  export type FnApi<T = any, R = any> = (params: Recordable & T, baseURL: string) => Promise<R>;
6
7
 
7
- export interface AuthOptions {
8
+ export interface AuthOptions<Plugins extends PluginOptions[]> {
8
9
  /** 登录后默认重定向至 */
9
10
  redirectTo?: RouteLocationRaw | Fn
10
11
 
@@ -17,14 +18,30 @@ export interface AuthOptions {
17
18
  /** 接口的baseURL */
18
19
  baseURL: string
19
20
 
20
- providers?: ProviderOptions[]
21
+ providers?: CreateAuthOptions<Plugins>['providers']
22
+ plugins?: CreateAuthOptions<Plugins>['plugins']
21
23
  }
22
24
 
23
- export interface InternalContext {
24
- /** 接口的baseURL */
25
- baseURL: string
25
+ export interface CreateAuthModule {
26
+ /** 登录后默认重定向至 */
27
+ redirectTo?: RouteLocationRaw | Fn
28
+
29
+ /** 页面配置 */
30
+ pages?: {
31
+ /** 默认登录页 */
32
+ signIn?: RouteLocationRaw
33
+ }
34
+ }
35
+
36
+ export interface PluginContext {
37
+ data: Recordable
38
+ }
39
+
40
+ export interface PluginOptions<Extend = any> {
41
+ /** 唯一值 */
42
+ id: string
26
43
 
27
- providers?: ProviderOptions[]
44
+ extend?: (context: PluginContext) => Extend
28
45
  }
29
46
 
30
47
  /** 第三方类型 */
@@ -0,0 +1 @@
1
+ export * from './two-factor';
@@ -0,0 +1,69 @@
1
+ import type { RequestInstance } from 'pubinfo-pr';
2
+ import type { Fn, PluginOptions } from '../interface';
3
+
4
+ interface TwoFactorExtend {
5
+ twoFactor: {
6
+ sendOtp: Fn
7
+ verifyOtp: Fn<{ code: string }>
8
+ }
9
+ }
10
+
11
+ interface DefaultOptOptions {
12
+ request: RequestInstance
13
+ sendOtp?: TwoFactorExtend['twoFactor']['sendOtp']
14
+ verifyOtp?: TwoFactorExtend['twoFactor']['verifyOtp']
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
+ */
28
+ export function twoFactor(options: TwoFactorOptions): PluginOptions<TwoFactorExtend> {
29
+ const { request, sendOtp, verifyOtp } = options;
30
+
31
+ return {
32
+ id: 'two-factor',
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
+ }
40
+
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
+ });
61
+ },
62
+ };
63
+
64
+ return {
65
+ twoFactor,
66
+ };
67
+ },
68
+ };
69
+ }
@@ -0,0 +1,29 @@
1
+ import type { ProviderOptions, ProviderUserOptions } from '../interface';
2
+ import { useUserStore } from 'pubinfo-pr';
3
+
4
+ export interface DefaultCredentialsOptions extends ProviderUserOptions {}
5
+
6
+ /**
7
+ * `默认登录`
8
+ *
9
+ * 底座自带的用户名密码登录
10
+ *
11
+ * 默认优先使用 `/auth/loginNew` 接口,若登录参数不符合则调用 `/auth/login` 接口
12
+ */
13
+ export default function DefaultCredentials(options?: DefaultCredentialsOptions): ProviderOptions {
14
+ return {
15
+ id: 'default-credentials',
16
+ name: '默认登录',
17
+ type: 'custom',
18
+ signIn: async (params) => {
19
+ const userStore = useUserStore();
20
+
21
+ // 兼容 `2.1.x` 之前的版本
22
+ // 根据 `captchaType` 字段判断是否使用 `/auth/loginNew` 登录接口
23
+ const signIn = params.captchaType ? userStore.signIn : userStore.login;
24
+
25
+ return await signIn(params);
26
+ },
27
+ ...options,
28
+ };
29
+ }
@@ -0,0 +1,5 @@
1
+ export { default as FourA } from './4A';
2
+ export { default as Credentials } from './credentials';
3
+ export { default as DefaultCredentials } from './default-credentials';
4
+ export { default as DingZJ } from './ding-zj';
5
+ export { default as Sso } from './sso';
package/dist/context.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import type { InternalContext } from './interface';
2
- export declare const ctx: import("unctx").UseContext<InternalContext>;
package/src/context.ts DELETED
@@ -1,4 +0,0 @@
1
- import type { InternalContext } from './interface';
2
- import { createContext } from 'pubinfo-pr';
3
-
4
- export const ctx = createContext<InternalContext>('auth');