@nocobase/auth 0.17.0-alpha.4 → 0.17.0-alpha.6

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.
@@ -4,9 +4,9 @@ import { Registry } from '@nocobase/utils';
4
4
  import { Auth, AuthExtend } from './auth';
5
5
  import { JwtOptions, JwtService } from './base/jwt-service';
6
6
  import { ITokenBlacklistService } from './base/token-blacklist-service';
7
- type Storer = {
7
+ export interface Storer {
8
8
  get: (name: string) => Promise<Model>;
9
- };
9
+ }
10
10
  export type AuthManagerOptions = {
11
11
  authKey: string;
12
12
  default?: string;
@@ -29,8 +29,8 @@ export declare class AuthManager {
29
29
  * @description Add a new authenticate type and the corresponding authenticator.
30
30
  * The types will show in the authenticators list of the admin panel.
31
31
  *
32
- * @param {string} authType - The type of the authenticator. It is required to be unique.
33
- * @param {AuthConfig} authConfig - Configurations of the kind of authenticator.
32
+ * @param authType - The type of the authenticator. It is required to be unique.
33
+ * @param authConfig - Configurations of the kind of authenticator.
34
34
  */
35
35
  registerTypes(authType: string, authConfig: AuthConfig): void;
36
36
  listTypes(): {
@@ -41,8 +41,8 @@ export declare class AuthManager {
41
41
  /**
42
42
  * get
43
43
  * @description Get authenticator instance by name.
44
- * @param {string} name - The name of the authenticator.
45
- * @return {Promise<Auth>} authenticator instance.
44
+ * @param name - The name of the authenticator.
45
+ * @return authenticator instance.
46
46
  */
47
47
  get(name: string, ctx: Context): Promise<Auth>;
48
48
  /**
@@ -44,8 +44,8 @@ const _AuthManager = class _AuthManager {
44
44
  * @description Add a new authenticate type and the corresponding authenticator.
45
45
  * The types will show in the authenticators list of the admin panel.
46
46
  *
47
- * @param {string} authType - The type of the authenticator. It is required to be unique.
48
- * @param {AuthConfig} authConfig - Configurations of the kind of authenticator.
47
+ * @param authType - The type of the authenticator. It is required to be unique.
48
+ * @param authConfig - Configurations of the kind of authenticator.
49
49
  */
50
50
  registerTypes(authType, authConfig) {
51
51
  this.authTypes.register(authType, authConfig);
@@ -62,8 +62,8 @@ const _AuthManager = class _AuthManager {
62
62
  /**
63
63
  * get
64
64
  * @description Get authenticator instance by name.
65
- * @param {string} name - The name of the authenticator.
66
- * @return {Promise<Auth>} authenticator instance.
65
+ * @param name - The name of the authenticator.
66
+ * @return authenticator instance.
67
67
  */
68
68
  async get(name, ctx) {
69
69
  if (!this.storer) {
@@ -14,6 +14,7 @@ export declare class BaseAuth extends Auth {
14
14
  get jwt(): JwtService;
15
15
  set user(user: Model);
16
16
  get user(): Model;
17
+ getCacheKey(userId: number): string;
17
18
  validateUsername(username: string): boolean;
18
19
  check(): Promise<any>;
19
20
  validate(): Promise<Model>;
package/lib/base/auth.js CHANGED
@@ -41,6 +41,9 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
41
41
  get user() {
42
42
  return this.ctx.state.currentUser;
43
43
  }
44
+ getCacheKey(userId) {
45
+ return `auth:${userId}`;
46
+ }
44
47
  validateUsername(username) {
45
48
  return /^[^@.<>"'/]{2,16}$/.test(username);
46
49
  }
@@ -54,11 +57,16 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
54
57
  if (roleName) {
55
58
  this.ctx.headers["x-role"] = roleName;
56
59
  }
57
- return await this.userRepository.findOne({
58
- filter: {
59
- id: userId
60
- }
61
- });
60
+ const cache = this.ctx.cache;
61
+ return await cache.wrap(
62
+ this.getCacheKey(userId),
63
+ () => this.userRepository.findOne({
64
+ filter: {
65
+ id: userId
66
+ },
67
+ raw: true
68
+ })
69
+ );
62
70
  } catch (err) {
63
71
  this.ctx.logger.error(err);
64
72
  return null;
@@ -72,7 +80,6 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
72
80
  try {
73
81
  user = await this.validate();
74
82
  } catch (err) {
75
- console.log(err);
76
83
  this.ctx.throw(401, err.message);
77
84
  }
78
85
  if (!user) {
@@ -91,6 +98,9 @@ const _BaseAuth = class _BaseAuth extends import_auth.Auth {
91
98
  if (!token) {
92
99
  return;
93
100
  }
101
+ const { userId } = await this.jwt.decode(token);
102
+ await this.ctx.app.emitAsync("beforeSignOut", { userId });
103
+ await this.ctx.cache.del(this.getCacheKey(userId));
94
104
  return await this.jwt.block(token);
95
105
  }
96
106
  };
package/package.json CHANGED
@@ -1,15 +1,16 @@
1
1
  {
2
2
  "name": "@nocobase/auth",
3
- "version": "0.17.0-alpha.4",
3
+ "version": "0.17.0-alpha.6",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./lib/index.js",
7
7
  "types": "./lib/index.d.ts",
8
8
  "dependencies": {
9
- "@nocobase/actions": "0.17.0-alpha.4",
10
- "@nocobase/database": "0.17.0-alpha.4",
11
- "@nocobase/resourcer": "0.17.0-alpha.4",
12
- "@nocobase/utils": "0.17.0-alpha.4",
9
+ "@nocobase/actions": "0.17.0-alpha.6",
10
+ "@nocobase/cache": "0.17.0-alpha.6",
11
+ "@nocobase/database": "0.17.0-alpha.6",
12
+ "@nocobase/resourcer": "0.17.0-alpha.6",
13
+ "@nocobase/utils": "0.17.0-alpha.6",
13
14
  "@types/jsonwebtoken": "^8.5.8",
14
15
  "jsonwebtoken": "^8.5.1"
15
16
  },
@@ -18,5 +19,5 @@
18
19
  "url": "git+https://github.com/nocobase/nocobase.git",
19
20
  "directory": "packages/auth"
20
21
  },
21
- "gitHead": "663b03a3799a70ba1a2bc6a0d686e679331a50ad"
22
+ "gitHead": "f92f8bcdf6d5baf07566381e9425ebca11e19626"
22
23
  }