rts2003-so 1.1.3 → 1.3.0

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.
Files changed (3) hide show
  1. package/README.md +24 -6
  2. package/index.js +53 -9
  3. package/package.json +2 -2
package/README.md CHANGED
@@ -16,19 +16,26 @@ $ npm install --save rts2003-so
16
16
  ## Usage
17
17
 
18
18
  ```js
19
- const { SingleSignOn, RTS2003_SO_ENDPOINT, RTS2003_SO_SECRET_KEY } = require('rts2003-so');
19
+ const { SingleSignOn, RTS2003_SO_ENDPOINT, RTS2003_SO_CLIENT_KEY, RTS2003_SO_SECRET_KEY } = require('rts2003-so');
20
20
 
21
21
  process.env.RTS2003_SO_ENDPOINT = 'custom-endpoint';
22
+ process.env.RTS2003_SO_CLIENT_KEY = 'custom-client-id';
22
23
  process.env.RTS2003_SO_SECRET_KEY = 'custom-secret-key';
23
24
 
24
25
  // show env
25
26
  console.log('ENDPOINT Key:', RTS2003_SO_ENDPOINT);
27
+ console.log('CLIENT_KEY Key:', RTS2003_SO_CLIENT_KEY);
26
28
  console.log('SECRET_KEY Key:', RTS2003_SO_SECRET_KEY);
27
29
 
28
30
  // use new class
29
31
  const sso = new SingleSignOn();
30
32
 
31
- // user register
33
+ // api require
34
+ // process.env.RTS2003_SO_ENDPOINT = 'custom-endpoint';
35
+ // process.env.RTS2003_SO_CLIENT_KEY = 'custom-client-id';
36
+ // process.env.RTS2003_SO_SECRET_KEY = 'custom-secret-key';
37
+
38
+ // user register for api
32
39
  const result = sso.userRegister({
33
40
  "email": "string",
34
41
  "first_name": "string",
@@ -37,7 +44,7 @@ const result = sso.userRegister({
37
44
  "created_by": 0
38
45
  });
39
46
 
40
- // user update
47
+ // user update for api
41
48
  const result = sso.userUpdate({
42
49
  "id": 0,
43
50
  "email": "string",
@@ -47,18 +54,29 @@ const result = sso.userUpdate({
47
54
  "updated_by": 0
48
55
  });
49
56
 
50
- // user delete
57
+ // user delete for api
51
58
  const result = sso.userDelete({
52
59
  "id": 0,
53
60
  "deleted_by": 0
54
61
  });
55
62
 
56
- // user refresh token
63
+ // user update password for api
64
+ const result = sso.userUpdatePassword({
65
+ "email": "string",
66
+ "current_password": "string",
67
+ "new_password": "string",
68
+ });
69
+
70
+ // frontend require
71
+ // process.env.RTS2003_SO_ENDPOINT = 'custom-endpoint';
72
+ // process.env.RTS2003_SO_CLIENT_KEY = 'custom-client-id';
73
+
74
+ // user refresh token for frontend
57
75
  const result = sso.userRefreshToken({
58
76
  "token": "string"
59
77
  });
60
78
 
61
- // user logout
79
+ // user logout for frontend
62
80
  const result = sso.userLogout({
63
81
  "token": "string"
64
82
  });
package/index.js CHANGED
@@ -1,12 +1,14 @@
1
- const fetch = require('node-fetch');
1
+ const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
2
2
 
3
3
  const RTS2003_SO_ENDPOINT = process.env.RTS2003_SO_ENDPOINT || '';
4
+ const RTS2003_SO_CLIENT_KEY = process.env.RTS2003_SO_CLIENT_KEY || '';
4
5
  const RTS2003_SO_SECRET_KEY = process.env.RTS2003_SO_SECRET_KEY || '';
5
6
 
6
7
  class SingleSignOn {
7
- constructor(RTS2003_SO_ENDPOINT, RTS2003_SO_SECRET_KEY) {
8
- this.endpoint = RTS2003_SO_ENDPOINT;
9
- this.secretKey = RTS2003_SO_SECRET_KEY;
8
+ constructor(endpoint = RTS2003_SO_ENDPOINT, clientId = RTS2003_SO_CLIENT_KEY, secretKey = RTS2003_SO_SECRET_KEY) {
9
+ this.endpoint = endpoint;
10
+ this.clientId = clientId;
11
+ this.secretKey = secretKey;
10
12
  }
11
13
 
12
14
  async userRegister(dataToSend = {
@@ -20,6 +22,9 @@ class SingleSignOn {
20
22
  if (!this.endpoint) {
21
23
  return { message: "Environment endpoint not found", statusCode: 400, error: false };
22
24
  }
25
+ if (!this.clientId) {
26
+ return { message: "Environment client id not found", statusCode: 400, error: false };
27
+ }
23
28
  if (!this.secretKey) {
24
29
  return { message: "Environment secret key not found", statusCode: 400, error: false };
25
30
  }
@@ -39,7 +44,7 @@ class SingleSignOn {
39
44
  return { message: "Field created_by not found", statusCode: 400, error: false };
40
45
  }
41
46
 
42
- const response = await fetch(`${this.endpoint}/user/register/${this.secretKey}`, {
47
+ const response = await fetch(`${this.endpoint}/user/register/${this.clientId}/${this.secretKey}`, {
43
48
  method: 'POST',
44
49
  headers: {
45
50
  'Content-Type': 'application/json',
@@ -85,7 +90,7 @@ class SingleSignOn {
85
90
  return { message: "Field updated_by not found", statusCode: 400, error: false };
86
91
  }
87
92
 
88
- const response = await fetch(`${this.endpoint}/user/update/${this.secretKey}`, {
93
+ const response = await fetch(`${this.endpoint}/user/update/${this.clientId}/${this.secretKey}`, {
89
94
  method: 'POST',
90
95
  headers: {
91
96
  'Content-Type': 'application/json',
@@ -119,7 +124,45 @@ class SingleSignOn {
119
124
  return { message: "Field deleted_by not found", statusCode: 400, error: false };
120
125
  }
121
126
 
122
- const response = await fetch(`${this.endpoint}/user/delete/${this.secretKey}`, {
127
+ const response = await fetch(`${this.endpoint}/user/delete/${this.clientId}/${this.secretKey}`, {
128
+ method: 'POST',
129
+ headers: {
130
+ 'Content-Type': 'application/json',
131
+ },
132
+ body: JSON.stringify(dataToSend)
133
+ });
134
+
135
+ const responseData = await response.json();
136
+
137
+ return responseData;
138
+ } catch (error) {
139
+ return { message: error, statusCode: 500, error: true };
140
+ }
141
+ }
142
+
143
+ async userUpdatePassword(dataToSend = {
144
+ email,
145
+ current_password,
146
+ new_password,
147
+ }) {
148
+ try {
149
+ if (!this.endpoint) {
150
+ return { message: "Environment endpoint not found", statusCode: 400, error: false };
151
+ }
152
+ if (!this.secretKey) {
153
+ return { message: "Environment secret key not found", statusCode: 400, error: false };
154
+ }
155
+ if (!dataToSend.email) {
156
+ return { message: "Field email not found", statusCode: 400, error: false };
157
+ }
158
+ if (!dataToSend.current_password) {
159
+ return { message: "Field current_password not found", statusCode: 400, error: false };
160
+ }
161
+ if (!dataToSend.new_password) {
162
+ return { message: "Field new_password not found", statusCode: 400, error: false };
163
+ }
164
+
165
+ const response = await fetch(`${this.endpoint}/user/update-password/${this.clientId}/${this.secretKey}`, {
123
166
  method: 'POST',
124
167
  headers: {
125
168
  'Content-Type': 'application/json',
@@ -149,7 +192,7 @@ class SingleSignOn {
149
192
  return { message: "Field token not found", statusCode: 400, error: false };
150
193
  }
151
194
 
152
- const response = await fetch(`${this.endpoint}/user/refresh-token/${this.secretKey}`, {
195
+ const response = await fetch(`${this.endpoint}/user/refresh-token/${this.clientId}`, {
153
196
  method: 'POST',
154
197
  headers: {
155
198
  'Content-Type': 'application/json',
@@ -179,7 +222,7 @@ class SingleSignOn {
179
222
  return { message: "Field token not found", statusCode: 400, error: false };
180
223
  }
181
224
 
182
- const response = await fetch(`${this.endpoint}/user/logout/${this.secretKey}`, {
225
+ const response = await fetch(`${this.endpoint}/user/logout/${this.clientId}`, {
183
226
  method: 'POST',
184
227
  headers: {
185
228
  'Content-Type': 'application/json',
@@ -198,6 +241,7 @@ class SingleSignOn {
198
241
 
199
242
  module.exports = {
200
243
  RTS2003_SO_ENDPOINT,
244
+ RTS2003_SO_CLIENT_KEY,
201
245
  RTS2003_SO_SECRET_KEY,
202
246
  SingleSignOn
203
247
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "rts2003-so",
3
- "version": "1.1.3",
4
- "description": "RTS2003 SO is service order, manage user, like register, update, delete.",
3
+ "version": "1.3.0",
4
+ "description": "RTS2003 SO is service order, manage user, like re-token, logout, register, update, delete, update password.",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "test": "jest -o ./test.js"