nexus-auth-sdk 4.1.0 → 4.1.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/index.js +1 -1
- package/index.mjs +46 -0
- package/package.json +21 -3
package/index.js
CHANGED
package/index.mjs
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* nexus-auth-sdk — ESM version (for CDN / <script type="module">)
|
|
3
|
+
* import { AuthCore } from 'https://cdn.jsdelivr.net/npm/nexus-auth-sdk@4/index.mjs';
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const API = 'https://auth.miaogou.site/api';
|
|
7
|
+
|
|
8
|
+
class AuthCore {
|
|
9
|
+
constructor(config = {}) {
|
|
10
|
+
if (!config.apiKey) throw new Error('[AuthCore] apiKey is required. Get yours at https://auth.miaogou.site');
|
|
11
|
+
this.apiKey = config.apiKey;
|
|
12
|
+
this.baseUrl = config.baseUrl || API;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async _fetch(path, body) {
|
|
16
|
+
const res = await fetch(this.baseUrl + path, {
|
|
17
|
+
method: 'POST',
|
|
18
|
+
headers: { 'Content-Type': 'application/json', 'X-API-Key': this.apiKey },
|
|
19
|
+
body: JSON.stringify(body),
|
|
20
|
+
});
|
|
21
|
+
const data = await res.json();
|
|
22
|
+
if (!res.ok) throw new Error(data.message || data.error || `AuthCore API error: ${res.status}`);
|
|
23
|
+
return data;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Register a new user. Returns { token, user }. */
|
|
27
|
+
async register({ email, password, username }) {
|
|
28
|
+
if (!email || !password) throw new Error('[AuthCore] email and password are required');
|
|
29
|
+
if (password.length < 8) throw new Error('[AuthCore] password must be at least 8 characters');
|
|
30
|
+
return this._fetch('/auth/register', { email, password, username: username || email.split('@')[0] });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Login an existing user. Returns { token, user }. */
|
|
34
|
+
async login({ email, password }) {
|
|
35
|
+
if (!email || !password) throw new Error('[AuthCore] email and password are required');
|
|
36
|
+
return this._fetch('/auth/login', { email, password });
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Verify a JWT token. Returns { valid, user? }. */
|
|
40
|
+
async verify(token) {
|
|
41
|
+
if (!token) throw new Error('[AuthCore] token is required');
|
|
42
|
+
return this._fetch('/auth/authenticate', { token });
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export { AuthCore };
|
package/package.json
CHANGED
|
@@ -1,11 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nexus-auth-sdk",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.1",
|
|
4
4
|
"description": "AuthCore — 认证即服务 SDK。为你的应用添加用户注册、登录、Token 验证,3 行代码接入。",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
7
|
-
"files": ["index.js", "index.d.ts", "react.js", "react.d.ts"],
|
|
7
|
+
"files": ["index.js", "index.mjs", "index.d.ts", "react.js", "react.d.ts"],
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./index.mjs",
|
|
11
|
+
"require": "./index.js",
|
|
12
|
+
"default": "./index.js"
|
|
13
|
+
},
|
|
14
|
+
"./react": {
|
|
15
|
+
"import": "./react.js",
|
|
16
|
+
"require": "./react.js",
|
|
17
|
+
"default": "./react.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
8
20
|
"keywords": ["auth", "authentication", "jwt", "authcore", "login", "register"],
|
|
9
21
|
"license": "MIT",
|
|
10
|
-
"repository": "https://github.com/kwan/authcore-sdk"
|
|
22
|
+
"repository": "https://github.com/kwan/authcore-sdk",
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": ">=17"
|
|
25
|
+
},
|
|
26
|
+
"peerDependenciesMeta": {
|
|
27
|
+
"react": { "optional": true }
|
|
28
|
+
}
|
|
11
29
|
}
|