@mspkapps/auth-client 0.1.0 → 0.1.2
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/LICENSE +21 -0
- package/package.json +17 -6
- package/src/AuthClient.js +111 -47
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 PRANESH KARTHI M S
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mspkapps/auth-client",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Lightweight client for Your Auth Service",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "src/index.js",
|
|
@@ -12,15 +12,26 @@
|
|
|
12
12
|
"README.md",
|
|
13
13
|
"LICENSE"
|
|
14
14
|
],
|
|
15
|
-
"keywords": [
|
|
15
|
+
"keywords": [
|
|
16
|
+
"auth",
|
|
17
|
+
"sdk",
|
|
18
|
+
"jwt",
|
|
19
|
+
"client"
|
|
20
|
+
],
|
|
16
21
|
"author": "MSPK Apps",
|
|
17
22
|
"license": "MIT",
|
|
18
|
-
"publishConfig": {
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
19
26
|
"repository": {
|
|
20
27
|
"type": "git",
|
|
21
28
|
"url": "https://github.com/mspk5196/authServiceNpm.git"
|
|
22
29
|
},
|
|
23
|
-
"bugs": {
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/mspk5196/authServiceNpm/issues"
|
|
32
|
+
},
|
|
24
33
|
"homepage": "https://github.com/mspk5196/authServiceNpm#readme",
|
|
25
|
-
"engines": {
|
|
26
|
-
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=18"
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/AuthClient.js
CHANGED
|
@@ -1,73 +1,137 @@
|
|
|
1
|
+
// ESM module
|
|
1
2
|
import { AuthError } from './errors.js';
|
|
2
3
|
|
|
3
4
|
export class AuthClient {
|
|
4
|
-
constructor(
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
constructor({
|
|
6
|
+
apiKey,
|
|
7
|
+
apiSecret,
|
|
8
|
+
baseUrl = 'https://cpanel.backend.mspkapps.in/api/v1',
|
|
9
|
+
keyInPath = true,
|
|
10
|
+
storage,
|
|
11
|
+
fetch: fetchFn
|
|
12
|
+
} = {}) {
|
|
13
|
+
if (!apiKey) throw new Error('apiKey is required');
|
|
14
|
+
if (!apiSecret) throw new Error('apiSecret is required'); // note: avoid exposing secret in browsers if possible
|
|
15
|
+
this.apiKey = apiKey;
|
|
16
|
+
this.apiSecret = apiSecret;
|
|
17
|
+
this.baseUrl = baseUrl.replace(/\/$/, '');
|
|
18
|
+
this.keyInPath = !!keyInPath;
|
|
19
|
+
this.fetch = fetchFn || (typeof fetch !== 'undefined' ? fetch : null);
|
|
20
|
+
if (!this.fetch) throw new Error('No fetch available. Pass { fetch } or run on Node 18+/browsers.');
|
|
21
|
+
|
|
22
|
+
this.storage = storage || (typeof window !== 'undefined' ? window.localStorage : null);
|
|
23
|
+
this.tokenKey = 'auth_user_token';
|
|
24
|
+
this.token = this._load(this.tokenKey);
|
|
10
25
|
}
|
|
11
26
|
|
|
12
|
-
|
|
13
|
-
|
|
27
|
+
// ---------- storage helpers ----------
|
|
28
|
+
_load(key) {
|
|
29
|
+
if (!this.storage) return null;
|
|
30
|
+
try { return this.storage.getItem(key); } catch { return null; }
|
|
14
31
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
this.
|
|
32
|
+
_save(key, val) {
|
|
33
|
+
if (!this.storage) return;
|
|
34
|
+
try { this.storage.setItem(key, val); } catch { /* ignore */ }
|
|
18
35
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
36
|
+
_clear(key) {
|
|
37
|
+
if (!this.storage) return;
|
|
38
|
+
try { this.storage.removeItem(key); } catch { /* ignore */ }
|
|
22
39
|
}
|
|
23
40
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return
|
|
41
|
+
// ---------- internal builders ----------
|
|
42
|
+
_buildUrl(path) {
|
|
43
|
+
const p = path.startsWith('/') ? path.slice(1) : path;
|
|
44
|
+
return this.keyInPath
|
|
45
|
+
? `${this.baseUrl}/${encodeURIComponent(this.apiKey)}/${p}`
|
|
46
|
+
: `${this.baseUrl}/${p}`;
|
|
28
47
|
}
|
|
29
48
|
|
|
30
|
-
|
|
31
|
-
return
|
|
49
|
+
_headers(extra = {}) {
|
|
50
|
+
return {
|
|
51
|
+
'Content-Type': 'application/json',
|
|
52
|
+
'X-API-Key': this.apiKey,
|
|
53
|
+
'X-API-Secret': this.apiSecret,
|
|
54
|
+
...(this.token ? { Authorization: `UserToken ${this.token}` } : {}),
|
|
55
|
+
...extra
|
|
56
|
+
};
|
|
32
57
|
}
|
|
33
58
|
|
|
34
|
-
|
|
35
|
-
|
|
59
|
+
setToken(token) {
|
|
60
|
+
this.token = token || null;
|
|
61
|
+
if (token) this._save(this.tokenKey, token);
|
|
62
|
+
else this._clear(this.tokenKey);
|
|
36
63
|
}
|
|
37
64
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
return this._request(options.method || 'GET', path, options.body, options);
|
|
65
|
+
getAuthHeader() {
|
|
66
|
+
return this.token ? { Authorization: `UserToken ${this.token}` } : {};
|
|
41
67
|
}
|
|
42
68
|
|
|
43
|
-
|
|
44
|
-
|
|
69
|
+
logout() {
|
|
70
|
+
this.setToken(null);
|
|
71
|
+
}
|
|
45
72
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
73
|
+
// ---------- public API methods ----------
|
|
74
|
+
async register({ email, username, password, name }) {
|
|
75
|
+
const resp = await this.fetch(this._buildUrl('auth/register'), {
|
|
76
|
+
method: 'POST',
|
|
77
|
+
headers: this._headers(),
|
|
78
|
+
body: JSON.stringify({ email, username, password, name })
|
|
79
|
+
});
|
|
80
|
+
const json = await safeJson(resp);
|
|
81
|
+
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Register failed');
|
|
82
|
+
const token = json?.data?.user_token;
|
|
83
|
+
if (token) this.setToken(token);
|
|
84
|
+
return json;
|
|
85
|
+
}
|
|
49
86
|
|
|
50
|
-
|
|
87
|
+
async login({ email, username, password }) {
|
|
88
|
+
const payload = email ? { email, password } : { username, password };
|
|
89
|
+
const resp = await this.fetch(this._buildUrl('auth/login'), {
|
|
90
|
+
method: 'POST',
|
|
91
|
+
headers: this._headers(),
|
|
92
|
+
body: JSON.stringify(payload)
|
|
93
|
+
});
|
|
94
|
+
const json = await safeJson(resp);
|
|
95
|
+
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Login failed');
|
|
96
|
+
const token = json?.data?.user_token;
|
|
97
|
+
if (token) this.setToken(token);
|
|
98
|
+
return json;
|
|
99
|
+
}
|
|
51
100
|
|
|
52
|
-
|
|
101
|
+
async getProfile() {
|
|
102
|
+
const resp = await this.fetch(this._buildUrl('user/profile'), {
|
|
103
|
+
method: 'GET',
|
|
104
|
+
headers: this._headers()
|
|
105
|
+
});
|
|
106
|
+
const json = await safeJson(resp);
|
|
107
|
+
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Profile failed');
|
|
108
|
+
return json;
|
|
109
|
+
}
|
|
53
110
|
|
|
54
|
-
|
|
111
|
+
// Generic authorized call for extra endpoints
|
|
112
|
+
async authed(path, { method = 'GET', body, headers } = {}) {
|
|
113
|
+
const resp = await this.fetch(this._buildUrl(path), {
|
|
55
114
|
method,
|
|
56
|
-
headers,
|
|
57
|
-
body: body
|
|
115
|
+
headers: this._headers(headers),
|
|
116
|
+
body: body ? JSON.stringify(body) : undefined
|
|
58
117
|
});
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (!resp.ok || json.success === false) {
|
|
63
|
-
throw new AuthError(
|
|
64
|
-
json.message || 'Request failed',
|
|
65
|
-
resp.status,
|
|
66
|
-
json.error || 'REQUEST_FAILED',
|
|
67
|
-
json
|
|
68
|
-
);
|
|
69
|
-
}
|
|
70
|
-
|
|
118
|
+
const json = await safeJson(resp);
|
|
119
|
+
if (!resp.ok || json?.success === false) throw toError(resp, json, 'Request failed');
|
|
71
120
|
return json;
|
|
72
121
|
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ---------- helpers ----------
|
|
125
|
+
async function safeJson(resp) {
|
|
126
|
+
try { return await resp.json(); } catch { return null; }
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function toError(resp, json, fallback) {
|
|
130
|
+
const err = new AuthError(
|
|
131
|
+
json?.message || fallback || 'Request failed',
|
|
132
|
+
resp.status,
|
|
133
|
+
json?.code || json?.error || 'REQUEST_FAILED',
|
|
134
|
+
json
|
|
135
|
+
);
|
|
136
|
+
return err;
|
|
73
137
|
}
|