@urga-panel/ur-panels-core 1.0.1 → 1.0.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.
@@ -37,6 +37,13 @@ export declare abstract class AuthService extends Service {
37
37
  databaseId?: string;
38
38
  };
39
39
  }>;
40
+ logoutRequest({ username, password, params, request, url }: {
41
+ username: any;
42
+ password: any;
43
+ params: any;
44
+ request: any;
45
+ url: any;
46
+ }): Promise<any>;
40
47
  loginRequest({ username, password, params, request, url }: {
41
48
  username: any;
42
49
  password: any;
@@ -21,6 +21,7 @@ export class AuthService extends Service {
21
21
  const requestHandlerService = this.ots.usedService.RequestHandlerService();
22
22
  // requestHandlerService.addHandler("deneme",this.loginHandler.bind(this));
23
23
  requestHandlerService.addHandler("login", this.loginRequest.bind(this));
24
+ requestHandlerService.addHandler("logout", this.logoutRequest.bind(this));
24
25
  requestHandlerService.addHandler("deneme", this.deneme.bind(this), {
25
26
  auth: true,
26
27
  role: "guest" // Örnek olarak admin rolü
@@ -51,6 +52,23 @@ export class AuthService extends Service {
51
52
  // role?: string; // Optional role field
52
53
  // };
53
54
  // }>;
55
+ async logoutRequest({ username, password, params, request, url }) {
56
+ try {
57
+ // Token cookie’lerini silmek için geçmiş tarih veriyoruz
58
+ return new Response(JSON.stringify({ status: "success", message: "Logged out successfully" }), {
59
+ status: 200,
60
+ headers: {
61
+ "Set-Cookie": `accessToken=; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=0, ` +
62
+ `refreshToken=; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=0`,
63
+ "Content-Type": "application/json"
64
+ }
65
+ });
66
+ }
67
+ catch (error) {
68
+ console.error("Logout error:", error);
69
+ return new Response(JSON.stringify({ status: "error", message: "An error occurred during logout" }), { status: 500, headers: { "Content-Type": "application/json" } });
70
+ }
71
+ }
54
72
  // Define methods specific to AuthService here
55
73
  async loginRequest({ username, password, params, request, url }) {
56
74
  //const { username, password } = params;
@@ -7,6 +7,7 @@ export type WVFrontServiceOts = ServiceOts & {
7
7
  usedService: {};
8
8
  };
9
9
  export declare class WVFrontService extends Service {
10
+ private static _requestIdCounter;
10
11
  static serviceInfo: {
11
12
  name: string;
12
13
  requiredServices: any[];
@@ -1,6 +1,7 @@
1
1
  import { Service } from "../../../types/Service.js";
2
2
  import { NSWebViewinterface } from "./nv.js";
3
3
  export class WVFrontService extends Service {
4
+ static _requestIdCounter = 0;
4
5
  static serviceInfo = {
5
6
  name: "WVFrontService",
6
7
  requiredServices: [],
@@ -27,28 +28,56 @@ export class WVFrontService extends Service {
27
28
  }
28
29
  async sendFetchRequest(url, options) {
29
30
  console.log("sendFetchRequest", url, options);
31
+ const id = `r_${Date.now()}_${++WVFrontService._requestIdCounter}`;
30
32
  return new Promise((resolve) => {
31
- const callback = (args) => {
32
- const param = args;
33
- // Convert param to a Response object
34
- //debugger;
35
- //@ts-ignore
36
- const byteArray = Object.values(param._bodyBlob._buffer);
37
- //@ts-ignore
38
- const uint8 = new Uint8Array(byteArray);
39
- //@ts-ignore
40
- const text = new TextDecoder().decode(uint8);
33
+ const handler = (args) => {
34
+ // support envelope { id, param } or legacy raw param
35
+ const envelope = args && typeof args === 'object' && 'id' in args ? args : { id: undefined, param: args };
36
+ if (envelope.id !== id)
37
+ return; // ignore unrelated responses
38
+ const param = envelope.param;
39
+ // Try to extract text safely from different shapes we might receive
40
+ let text = null;
41
+ try {
42
+ if (!param) {
43
+ text = '';
44
+ }
45
+ else if (typeof param === 'string') {
46
+ text = param;
47
+ }
48
+ else if (param._bodyBlob && param._bodyBlob._buffer) {
49
+ // @ts-ignore
50
+ const byteArray = Object.values(param._bodyBlob._buffer);
51
+ // @ts-ignore
52
+ const uint8 = new Uint8Array(byteArray);
53
+ text = new TextDecoder().decode(uint8);
54
+ }
55
+ else if (param.body) {
56
+ text = typeof param.body === 'string' ? param.body : JSON.stringify(param.body);
57
+ }
58
+ else {
59
+ text = JSON.stringify(param);
60
+ }
61
+ }
62
+ catch (e) {
63
+ console.error('sendFetchRequest: failed to decode response body', e);
64
+ text = '';
65
+ }
41
66
  const response = new Response(text, {
42
67
  status: 200,
43
68
  headers: { "Content-Type": "application/json" }
44
69
  });
45
- //debugger;
46
- console.log("sendFetchRequest received", param);
47
- this.oWebViewInterface.removeListener('sendFetchRequest', callback);
70
+ console.log("sendFetchRequest received (id)", id, envelope.param);
71
+ // remove listeners
72
+ this.oWebViewInterface.removeListener('sendFetchRequest', handler);
73
+ this.oWebViewInterface.removeListener('sendFetchRequestResponse', handler);
48
74
  resolve(response);
49
75
  };
50
- this.oWebViewInterface.on('sendFetchRequest', callback);
51
- this.oWebViewInterface.emit("sendFetchRequest", { url, options });
76
+ // listen for both legacy and explicit response events
77
+ this.oWebViewInterface.on('sendFetchRequest', handler);
78
+ this.oWebViewInterface.on('sendFetchRequestResponse', handler);
79
+ // emit request with id so receiver can correlate
80
+ this.oWebViewInterface.emit("sendFetchRequest", { id, url, options });
52
81
  });
53
82
  }
54
83
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urga-panel/ur-panels-core",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "type": "module",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -37,6 +37,7 @@ export abstract class AuthService extends Service {
37
37
  const requestHandlerService = this.ots.usedService.RequestHandlerService() as RequestHandlerService;
38
38
  // requestHandlerService.addHandler("deneme",this.loginHandler.bind(this));
39
39
  requestHandlerService.addHandler("login", this.loginRequest.bind(this));
40
+ requestHandlerService.addHandler("logout", this.logoutRequest.bind(this));
40
41
  requestHandlerService.addHandler("deneme", this.deneme.bind(this), {
41
42
  auth: true,
42
43
  role: "guest" // Örnek olarak admin rolü
@@ -91,6 +92,30 @@ export abstract class AuthService extends Service {
91
92
  // };
92
93
  // }>;
93
94
 
95
+ async logoutRequest({ username, password, params, request, url }): Promise<any> {
96
+ try {
97
+ // Token cookie’lerini silmek için geçmiş tarih veriyoruz
98
+ return new Response(
99
+ JSON.stringify({ status: "success", message: "Logged out successfully" }),
100
+ {
101
+ status: 200,
102
+ headers: {
103
+ "Set-Cookie":
104
+ `accessToken=; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=0, ` +
105
+ `refreshToken=; Path=/; HttpOnly; Secure; SameSite=Strict; Max-Age=0`,
106
+ "Content-Type": "application/json"
107
+ }
108
+ }
109
+ );
110
+ } catch (error) {
111
+ console.error("Logout error:", error);
112
+ return new Response(
113
+ JSON.stringify({ status: "error", message: "An error occurred during logout" }),
114
+ { status: 500, headers: { "Content-Type": "application/json" } }
115
+ );
116
+ }
117
+ }
118
+
94
119
  // Define methods specific to AuthService here
95
120
  async loginRequest({ username, password, params, request, url }): Promise<any> {
96
121
  //const { username, password } = params;
@@ -124,9 +149,9 @@ export abstract class AuthService extends Service {
124
149
  }
125
150
 
126
151
  const accessToken = jwt.sign(
127
- {
128
- id: result.user.id,
129
- username: result.user.username,
152
+ {
153
+ id: result.user.id,
154
+ username: result.user.username,
130
155
  role: result.user.role,
131
156
  databaseId: result.user.databaseId // databaseId eklendi
132
157
  },
@@ -136,9 +161,9 @@ export abstract class AuthService extends Service {
136
161
 
137
162
  // 5. Refresh Token üret
138
163
  const refreshToken = jwt.sign(
139
- {
140
- id: result.user.id,
141
- username: result.user.username,
164
+ {
165
+ id: result.user.id,
166
+ username: result.user.username,
142
167
  role: result.user.role,
143
168
  databaseId: result.user.databaseId // databaseId eklendi
144
169
  },
@@ -231,7 +256,7 @@ export abstract class AuthService extends Service {
231
256
 
232
257
  //Check role here
233
258
  // Admin ise onay ver
234
-
259
+
235
260
  // Admin ise devam etsin
236
261
  if (options?.role && res.user?.role === 'admin') {
237
262
  // admin ise role kontrolü atlanır, devam edilir
@@ -251,9 +276,9 @@ export abstract class AuthService extends Service {
251
276
  { expiresIn: '15m' }
252
277
  );
253
278
  const newRefreshToken = jwt.sign(
254
- {
255
- id: decodedRefresh.id,
256
- username: decodedRefresh.username,
279
+ {
280
+ id: decodedRefresh.id,
281
+ username: decodedRefresh.username,
257
282
  role: decodedRefresh.role,
258
283
  databaseId: decodedRefresh.databaseId // databaseId eklendi
259
284
  },
@@ -9,6 +9,7 @@ export type WVFrontServiceOts = ServiceOts & {
9
9
  }
10
10
 
11
11
  export class WVFrontService extends Service {
12
+ private static _requestIdCounter = 0;
12
13
  static serviceInfo = {
13
14
  name: "WVFrontService",
14
15
  requiredServices: [],
@@ -40,31 +41,57 @@ export class WVFrontService extends Service {
40
41
 
41
42
  public async sendFetchRequest(url: string, options?: RequestInit): Promise<any> {
42
43
  console.log("sendFetchRequest", url, options);
44
+ const id = `r_${Date.now()}_${++WVFrontService._requestIdCounter}`;
43
45
 
44
46
  return new Promise((resolve) => {
45
- const callback = (args: any) => {
46
- const param = args as unknown;
47
- // Convert param to a Response object
48
- //debugger;
49
- //@ts-ignore
50
- const byteArray = Object.values(param._bodyBlob._buffer);
51
- //@ts-ignore
52
- const uint8 = new Uint8Array(byteArray);
53
- //@ts-ignore
54
- const text = new TextDecoder().decode(uint8);
55
- const response = new Response(text,
56
- {
57
- status: 200,
58
- headers: { "Content-Type": "application/json" }
47
+ const handler = (args: any) => {
48
+ // support envelope { id, param } or legacy raw param
49
+ const envelope = args && typeof args === 'object' && 'id' in args ? args : { id: undefined, param: args };
50
+ if (envelope.id !== id) return; // ignore unrelated responses
51
+
52
+ const param = envelope.param as any;
53
+
54
+ // Try to extract text safely from different shapes we might receive
55
+ let text: string | null = null;
56
+ try {
57
+ if (!param) {
58
+ text = '';
59
+ } else if (typeof param === 'string') {
60
+ text = param;
61
+ } else if (param._bodyBlob && param._bodyBlob._buffer) {
62
+ // @ts-ignore
63
+ const byteArray = Object.values(param._bodyBlob._buffer);
64
+ // @ts-ignore
65
+ const uint8 = new Uint8Array(byteArray);
66
+ text = new TextDecoder().decode(uint8);
67
+ } else if (param.body) {
68
+ text = typeof param.body === 'string' ? param.body : JSON.stringify(param.body);
69
+ } else {
70
+ text = JSON.stringify(param);
59
71
  }
60
- );
61
- //debugger;
62
- console.log("sendFetchRequest received", param);
63
- this.oWebViewInterface.removeListener('sendFetchRequest', callback);
72
+ } catch (e) {
73
+ console.error('sendFetchRequest: failed to decode response body', e);
74
+ text = '';
75
+ }
76
+
77
+ const response = new Response(text, {
78
+ status: 200,
79
+ headers: { "Content-Type": "application/json" }
80
+ });
81
+
82
+ console.log("sendFetchRequest received (id)", id, envelope.param);
83
+ // remove listeners
84
+ this.oWebViewInterface.removeListener('sendFetchRequest', handler);
85
+ this.oWebViewInterface.removeListener('sendFetchRequestResponse', handler);
64
86
  resolve(response);
65
87
  };
66
- this.oWebViewInterface.on('sendFetchRequest', callback);
67
- this.oWebViewInterface.emit("sendFetchRequest", { url, options });
88
+
89
+ // listen for both legacy and explicit response events
90
+ this.oWebViewInterface.on('sendFetchRequest', handler);
91
+ this.oWebViewInterface.on('sendFetchRequestResponse', handler);
92
+
93
+ // emit request with id so receiver can correlate
94
+ this.oWebViewInterface.emit("sendFetchRequest", { id, url, options });
68
95
  });
69
96
  }
70
97