@kevisual/auth 1.0.4 → 1.0.5-alpha.1

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/proxy.d.ts CHANGED
@@ -25,6 +25,8 @@ type AuthProxy = {
25
25
  * @returns
26
26
  */
27
27
  setTokenUser?: (ctx: any, data: any) => Promise<any>;
28
+ cacheMe: (token: string) => Promise<any>;
29
+ setCahceMe: (token: string, data: any) => Promise<any>;
28
30
  };
29
31
  type CreateAuthRouteOptions = {
30
32
  app?: any;
package/dist/proxy.js CHANGED
@@ -53,6 +53,18 @@ const createAuthRoute = ({ app, addToApp = true, proxy }) => {
53
53
  if (!token) {
54
54
  app.throw(401, 'Token is Unauthorized');
55
55
  }
56
+ if (proxy?.cacheMe) {
57
+ const cache = await proxy?.cacheMe?.(token);
58
+ if (cache) {
59
+ if (proxy?.setTokenUser) {
60
+ await proxy?.setTokenUser?.(ctx, cache);
61
+ }
62
+ else {
63
+ ctx.state['tokenUser'] = cache;
64
+ }
65
+ return;
66
+ }
67
+ }
56
68
  try {
57
69
  const result = await proxy?.queryMe?.(token);
58
70
  if (result.code === 200) {
@@ -64,6 +76,9 @@ const createAuthRoute = ({ app, addToApp = true, proxy }) => {
64
76
  else {
65
77
  ctx.state['tokenUser'] = result.payload;
66
78
  }
79
+ if (proxy?.setCahceMe) {
80
+ await proxy?.setCahceMe?.(token, result.data);
81
+ }
67
82
  }
68
83
  catch (e) {
69
84
  app.throw(401, 'Token is invalid');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevisual/auth",
3
- "version": "1.0.4",
3
+ "version": "1.0.5-alpha.1",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -21,7 +21,7 @@
21
21
  "author": "",
22
22
  "license": "ISC",
23
23
  "peerDependencies": {
24
- "@kevisual/router": "^0.0.4"
24
+ "@kevisual/router": ">=0.0.4"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@rollup/plugin-commonjs": "^28.0.1",
package/src/proxy.ts CHANGED
@@ -57,6 +57,8 @@ type AuthProxy = {
57
57
  * @returns
58
58
  */
59
59
  setTokenUser?: (ctx: any, data: any) => Promise<any>;
60
+ cacheMe: (token: string) => Promise<any>;
61
+ setCahceMe: (token: string, data: any) => Promise<any>;
60
62
  };
61
63
  type CreateAuthRouteOptions = {
62
64
  app?: any;
@@ -73,6 +75,17 @@ export const createAuthRoute = ({ app, addToApp = true, proxy }: CreateAuthRoute
73
75
  if (!token) {
74
76
  app.throw(401, 'Token is Unauthorized');
75
77
  }
78
+ if (proxy?.cacheMe) {
79
+ const cache = await proxy?.cacheMe?.(token);
80
+ if (cache) {
81
+ if (proxy?.setTokenUser) {
82
+ await proxy?.setTokenUser?.(ctx, cache);
83
+ } else {
84
+ ctx.state['tokenUser'] = cache;
85
+ }
86
+ return;
87
+ }
88
+ }
76
89
  try {
77
90
  const result = await proxy?.queryMe?.(token);
78
91
  if (result.code === 200) {
@@ -83,6 +96,9 @@ export const createAuthRoute = ({ app, addToApp = true, proxy }: CreateAuthRoute
83
96
  } else {
84
97
  ctx.state['tokenUser'] = result.payload;
85
98
  }
99
+ if (proxy?.setCahceMe) {
100
+ await proxy?.setCahceMe?.(token, result.data);
101
+ }
86
102
  } catch (e) {
87
103
  app.throw(401, 'Token is invalid');
88
104
  }