nexus-auth-sdk 4.1.0 → 4.2.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.
package/index.d.ts CHANGED
@@ -38,5 +38,7 @@ declare module '@authcore/sdk' {
38
38
  register(params: RegisterParams): Promise<AuthResponse>;
39
39
  login(params: LoginParams): Promise<AuthResponse>;
40
40
  verify(token: string): Promise<VerifyResponse>;
41
+ sendCode(params: { email: string }): Promise<{ success: boolean; expires_in: number }>;
42
+ verifyCode(params: { email: string; code: string }): Promise<{ valid: boolean; error?: string }>;
41
43
  }
42
44
  }
package/index.js CHANGED
@@ -45,8 +45,20 @@ class AuthCore {
45
45
  if (!token) throw new Error('[AuthCore] token is required');
46
46
  return this._fetch('/auth/authenticate', { token });
47
47
  }
48
+
49
+ /** Send verification code to email. Returns { success, expires_in }. */
50
+ async sendCode({ email }) {
51
+ if (!email) throw new Error('[AuthCore] email is required');
52
+ return this._fetch('/auth/send-code', { email });
53
+ }
54
+
55
+ /** Verify a received code. Returns { valid }. */
56
+ async verifyCode({ email, code }) {
57
+ if (!email || !code) throw new Error('[AuthCore] email and code are required');
58
+ return this._fetch('/auth/verify-code', { email, code });
59
+ }
48
60
  }
49
61
 
50
- // ESM and CJS compatible
62
+ // CJS
51
63
  module.exports = { AuthCore };
52
64
  if (typeof exports !== 'undefined') exports.AuthCore = AuthCore;
package/index.mjs ADDED
@@ -0,0 +1,56 @@
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
+ async sendCode({ email }) {
46
+ if (!email) throw new Error('[AuthCore] email is required');
47
+ return this._fetch('/auth/send-code', { email });
48
+ }
49
+
50
+ async verifyCode({ email, code }) {
51
+ if (!email || !code) throw new Error('[AuthCore] email and code are required');
52
+ return this._fetch('/auth/verify-code', { email, code });
53
+ }
54
+ }
55
+
56
+ export { AuthCore };
package/package.json CHANGED
@@ -1,11 +1,29 @@
1
1
  {
2
2
  "name": "nexus-auth-sdk",
3
- "version": "4.1.0",
3
+ "version": "4.2.0",
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
  }