-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathguard.ts
42 lines (36 loc) · 1.14 KB
/
guard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { authSupevisor, getCloudBaseConfig } from '../utils'
export interface AuthGuardOptions {
// 无法通过 guard 认证时的提示信息
tips?: string
// 校验配置文件是否存在
ensureConfig?: boolean
}
/**
* Auth 装饰器,用于在执行操作前检验是否登录
* 没有登录时,终止操作,显示登录提示
*/
export const AuthGuard = (options: AuthGuardOptions = {}): MethodDecorator => (
target: any,
key: string | symbol,
descriptor: TypedPropertyDescriptor<any>
) => {
const { tips, ensureConfig = true } = options
const rawFunc: Function = descriptor.value
// 修改原函数行为
descriptor.value = async function (...args) {
const credential = target?.credential
const loginState = await authSupevisor.getLoginState()
// 未登录,终止运行
if (!credential && !loginState) {
return
}
if (ensureConfig) {
const config = await getCloudBaseConfig()
if (!config?.envId) {
return
}
}
return rawFunc.apply(this, args)
}
return descriptor
}