moc-oauth-client 1.0.4 → 1.0.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.
package/README.md CHANGED
@@ -30,39 +30,36 @@ touch .env
30
30
  Add the following environment variables to your .env file:
31
31
 
32
32
  ```bash
33
- BASE_URL=https://identity.moc.gov.kh
33
+ BASE_URL=https://api-dev-dentity.moc.gov.kh
34
34
  CLIENT_ID=your_client_id
35
35
  CLIENT_SECRET=your_client_secret
36
- REDIRECT_URI=http://localhost:3000/callback
36
+ REDIRECT_URI=https://example.com/callback
37
37
  ```
38
38
 
39
39
  ## 🚀 Quick Start
40
40
 
41
41
  ```typescript
42
- import { MocOAuthClient } from "moc-oauth-client";
42
+ import { MOCOAuthClient } from "moc-oauth-client";
43
43
 
44
- const oauth = new MocOAuthClient();
45
-
46
- // Authorize the client
47
- const authorizationUrl = await oauth.authorize();
44
+ const oauth = new MOCOAuthClient();
48
45
  ```
49
46
 
50
47
  ## 🔐 Authorize Client
51
48
 
52
49
  ```typescript
53
- const response = await oauth.authorizeClient();
50
+ const result = await oauth.authorizeClient();
54
51
 
55
- console.log(response.data.redirectUri);
52
+ console.log(result);
56
53
  ```
57
54
 
58
55
  ✅ Success Response
59
56
 
60
57
  ```json
61
58
  {
62
- "success": true,
63
59
  "data": {
64
60
  "redirectUri": "http://localhost:3000/login?loginToken=..."
65
- }
61
+ },
62
+ "error": null
66
63
  }
67
64
  ```
68
65
 
@@ -70,15 +67,20 @@ console.log(response.data.redirectUri);
70
67
 
71
68
  ```typescript
72
69
  const result = await oauth.validateToken(accessToken);
70
+
71
+ console.log(result);
73
72
  ```
74
73
 
75
74
  ✅ Success Response
76
75
 
77
76
  ```json
78
77
  {
79
- "isValid": true,
80
- "accessToken": "...",
81
- "refreshToken": "..."
78
+ "data": {
79
+ "isValid": true,
80
+ "accessToken": "...",
81
+ "refreshToken": "..."
82
+ },
83
+ "error": null
82
84
  }
83
85
  ```
84
86
 
@@ -86,18 +88,21 @@ const result = await oauth.validateToken(accessToken);
86
88
  Retrieve authenticated user information.
87
89
 
88
90
  ```typescript
89
- const user = await oauth.getCurrentUser(accessToken);
91
+ const result = await oauth.getCurrentUser(accessToken);
90
92
 
91
- console.log(user.data);
93
+ console.log(result);
92
94
  ```
93
95
 
94
96
  ✅ Success Response
95
97
 
96
98
  ```json
97
99
  {
98
- "email": "user@email.com",
99
- "firstName": "Sok",
100
- "lastName": "Dara"
100
+ "data": {
101
+ "email": "user@email.com",
102
+ "firstName": "Sok",
103
+ "lastName": "Dara"
104
+ },
105
+ "error": null
101
106
  }
102
107
  ```
103
108
 
@@ -105,15 +110,20 @@ console.log(user.data);
105
110
  Generate a new access token using refresh token.
106
111
 
107
112
  ```typescript
108
- const tokens = await oauth.refreshToken(refreshToken);
113
+ const result = await oauth.refreshToken(refreshToken);
114
+
115
+ console.log(result);
109
116
  ```
110
117
 
111
118
  ✅ Success Response
112
119
 
113
120
  ```json
114
121
  {
115
- "accessToken": "...",
116
- "refreshToken": "..."
122
+ "data": {
123
+ "accessToken": "...",
124
+ "refreshToken": "..."
125
+ },
126
+ "error": null
117
127
  }
118
128
  ```
119
129
 
@@ -123,7 +133,6 @@ All API errors follow a standardized format:
123
133
 
124
134
  ```json
125
135
  {
126
- "success": false,
127
136
  "error": {
128
137
  "code": "UNAUTHORIZED",
129
138
  "message": "Invalid token"
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ interface OAuthResult<T> {
3
3
  error: OAuthError["error"] | null;
4
4
  }
5
5
  interface OAuthError {
6
+ status: number;
6
7
  success: false;
7
8
  error: {
8
9
  code: string;
@@ -23,12 +24,18 @@ interface CurrentUser {
23
24
  lastName: string;
24
25
  }
25
26
 
27
+ interface MOCOAuthClientBase {
28
+ authorizeClient(): Promise<OAuthResult<AuthorizeResponse>>;
29
+ validateToken(accessToken: string): Promise<OAuthResult<ValidateTokenResponse>>;
30
+ getCurrentUser(accessToken: string): Promise<OAuthResult<CurrentUser>>;
31
+ refreshToken(refreshToken: string): Promise<OAuthResult<ValidateTokenResponse>>;
32
+ }
26
33
  /**
27
34
  * MOCOAuthClient is a client library for interacting with the MOC OAuth API.
28
35
  * It provides methods for authorizing clients, validating tokens, refreshing tokens,
29
36
  * and retrieving the current user's information.
30
37
  */
31
- declare class MOCOAuthClient {
38
+ declare class MOCOAuthClient implements MOCOAuthClientBase {
32
39
  /**
33
40
  * Authorizes a client using the MOC OAuth API.
34
41
  * @returns A promise resolving to an object containing the response data.
package/dist/index.js CHANGED
@@ -46,8 +46,10 @@ var config = {
46
46
  clientSecret: process.env.CLIENT_SECRET,
47
47
  redirectUri: process.env.REDIRECT_URI
48
48
  };
49
- if (!config.baseUrl || !config.clientId) {
50
- throw new Error("Missing OAuth environment variables");
49
+ if (!config.baseUrl || !config.clientId || !config.clientSecret || !config.redirectUri) {
50
+ throw new Error(
51
+ "ENV variables BASE_URL, CLIENT_SECRET, REDIRECT_URI and CLIENT_ID are required"
52
+ );
51
53
  }
52
54
 
53
55
  // src/http.ts
@@ -60,10 +62,13 @@ var http = import_axios.default.create({
60
62
  });
61
63
 
62
64
  // src/errors.ts
63
- function parseError(error) {
65
+ function handleError(error) {
64
66
  const err = error;
65
67
  if (err.response?.data?.error) {
66
- return err.response.data.error;
68
+ return {
69
+ code: err.response.data.error.code,
70
+ message: err.response.data.error.message
71
+ };
67
72
  }
68
73
  return {
69
74
  code: "UNKNOWN_ERROR",
@@ -73,10 +78,10 @@ function parseError(error) {
73
78
 
74
79
  // src/api-endpoint.ts
75
80
  var API_ENDPOINTS = {
76
- AUTHORIZE: "/api/v1/oauth/authorize",
77
- USER: "/api/v1/oauth/get-user",
78
- REFRESH_TOKEN: "/api/v1/oauth/refresh-token",
79
- VALIDATE_TOKEN: "/api/v1/oauth/validate-jwt-token"
81
+ LOGIN_TOKEN: "/api/v1/service-account/login-token",
82
+ USER: "/api/v1/service-account/profile-user",
83
+ REFRESH_TOKEN: "/api/v1/service-account/refresh-token",
84
+ VALIDATE_TOKEN: "/api/v1/service-account/validate-jwt-token"
80
85
  };
81
86
 
82
87
  // src/client.ts
@@ -89,7 +94,7 @@ var MOCOAuthClient = class {
89
94
  */
90
95
  async authorizeClient() {
91
96
  try {
92
- const res = await http.post(API_ENDPOINTS.AUTHORIZE, {
97
+ const res = await http.post(API_ENDPOINTS.LOGIN_TOKEN, {
93
98
  clientId: config.clientId,
94
99
  clientSecret: config.clientSecret,
95
100
  redirectUri: config.redirectUri
@@ -101,7 +106,7 @@ var MOCOAuthClient = class {
101
106
  } catch (e) {
102
107
  return {
103
108
  data: null,
104
- error: parseError(e)
109
+ error: handleError(e)
105
110
  };
106
111
  }
107
112
  }
@@ -125,7 +130,7 @@ var MOCOAuthClient = class {
125
130
  } catch (e) {
126
131
  return {
127
132
  data: null,
128
- error: parseError(e)
133
+ error: handleError(e)
129
134
  };
130
135
  }
131
136
  }
@@ -150,7 +155,7 @@ var MOCOAuthClient = class {
150
155
  } catch (e) {
151
156
  return {
152
157
  data: null,
153
- error: parseError(e)
158
+ error: handleError(e)
154
159
  };
155
160
  }
156
161
  }
@@ -173,7 +178,7 @@ var MOCOAuthClient = class {
173
178
  } catch (e) {
174
179
  return {
175
180
  data: null,
176
- error: parseError(e)
181
+ error: handleError(e)
177
182
  };
178
183
  }
179
184
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moc-oauth-client",
3
- "version": "1.0.4",
3
+ "version": "1.0.6",
4
4
  "description": "OAuth client for Ministry of Commerce, Cambodia",
5
5
  "author": "Ministry of Commerce, Cambodia",
6
6
  "license": "MIT",