authmi 1.0.0 → 1.1.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/dist/react.mjs CHANGED
@@ -1,10 +1,8 @@
1
1
  import {
2
2
  AuthMiClient,
3
3
  AuthMiError,
4
- LocalStorageTokenStorage,
5
- MemoryTokenStorage,
6
4
  ScopeError
7
- } from "./chunk-UNKK6FP4.mjs";
5
+ } from "./chunk-EAK7C6YO.mjs";
8
6
 
9
7
  // src/react.tsx
10
8
  import {
@@ -18,27 +16,30 @@ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
18
16
  var AuthMiContext = createContext(null);
19
17
  function AuthMiProvider({
20
18
  config,
21
- tokenStorage,
19
+ options,
22
20
  children,
23
21
  validateOnMount = true,
24
22
  validationInterval = 5 * 60 * 1e3
25
23
  }) {
26
- const [client] = useState(() => new AuthMiClient(config, tokenStorage));
24
+ const [client] = useState(() => new AuthMiClient(config, options));
27
25
  const [isAuthenticated, setIsAuthenticated] = useState(false);
28
26
  const [user, setUser] = useState(null);
29
27
  const [isLoading, setIsLoading] = useState(true);
30
28
  useEffect(() => {
31
29
  const checkAuth = async () => {
32
- const token = client.getAccessToken();
30
+ const token = await client.getAccessToken();
33
31
  if (token && validateOnMount) {
34
32
  try {
35
33
  const result = await client.validate();
36
34
  if (result.valid) {
37
35
  setIsAuthenticated(true);
36
+ if (result.userId && result.email) {
37
+ setUser({ userId: result.userId, email: result.email, createdAt: "" });
38
+ }
38
39
  } else {
39
40
  client.clearAccessToken();
40
41
  }
41
- } catch (error) {
42
+ } catch {
42
43
  client.clearAccessToken();
43
44
  }
44
45
  }
@@ -55,30 +56,34 @@ function AuthMiProvider({
55
56
  setIsAuthenticated(false);
56
57
  setUser(null);
57
58
  }
58
- } catch (error) {
59
+ } catch {
59
60
  setIsAuthenticated(false);
60
61
  setUser(null);
61
62
  }
62
63
  }, validationInterval);
63
64
  return () => clearInterval(interval);
64
65
  }, [client, isAuthenticated, validationInterval]);
65
- const login = useCallback(
66
- async (email, password) => {
67
- setIsLoading(true);
68
- try {
69
- const token = await client.login({ email, password });
70
- setIsAuthenticated(true);
71
- setUser({
72
- userId: token.userId,
73
- email: token.email,
74
- createdAt: (/* @__PURE__ */ new Date()).toISOString()
75
- });
76
- } finally {
77
- setIsLoading(false);
78
- }
79
- },
80
- [client]
81
- );
66
+ const login = useCallback(async (email, password) => {
67
+ setIsLoading(true);
68
+ try {
69
+ const token = await client.login({ email, password });
70
+ setIsAuthenticated(true);
71
+ setUser({ userId: token.userId, email: token.email, createdAt: (/* @__PURE__ */ new Date()).toISOString() });
72
+ } finally {
73
+ setIsLoading(false);
74
+ }
75
+ }, [client]);
76
+ const signup = useCallback(async (email, password, name) => {
77
+ setIsLoading(true);
78
+ try {
79
+ const cfg = client["config"];
80
+ const token = await client.signup({ email, password, name, client_id: cfg.serviceId || cfg.clientId });
81
+ setIsAuthenticated(true);
82
+ setUser({ userId: token.userId, email: token.email, createdAt: (/* @__PURE__ */ new Date()).toISOString() });
83
+ } finally {
84
+ setIsLoading(false);
85
+ }
86
+ }, [client]);
82
87
  const logout = useCallback(async () => {
83
88
  setIsLoading(true);
84
89
  try {
@@ -103,17 +108,26 @@ function AuthMiProvider({
103
108
  user,
104
109
  isLoading,
105
110
  login,
111
+ signup,
106
112
  logout,
107
- validate
113
+ validate,
114
+ setup2fa: () => client.setup2fa(),
115
+ enable2fa: (code) => client.enable2fa(code),
116
+ disable2fa: (password) => client.disable2fa(password),
117
+ regenerateBackupCodes: () => client.regenerateBackupCodes(),
118
+ challenge2fa: (request) => client.challenge2fa(request),
119
+ forgotPassword: (email) => client.forgotPassword({ email }),
120
+ initiateOAuth: (provider, redirectUri) => client.initiateOAuth(provider, redirectUri),
121
+ handleOAuthCallback: (url) => client.handleOAuthCallback(url),
122
+ isOAuthCallback: (url) => client.isOAuthCallback(url),
123
+ getMe: () => client.getMe()
108
124
  };
109
125
  return /* @__PURE__ */ jsx(AuthMiContext.Provider, { value, children });
110
126
  }
111
127
  function useAuthMi() {
112
- const context = useContext(AuthMiContext);
113
- if (!context) {
114
- throw new Error("useAuthMi must be used within AuthMiProvider");
115
- }
116
- return context;
128
+ const ctx = useContext(AuthMiContext);
129
+ if (!ctx) throw new Error("useAuthMi must be used within AuthMiProvider");
130
+ return ctx;
117
131
  }
118
132
  function useAuthMiClient() {
119
133
  return useAuthMi().client;
@@ -125,17 +139,37 @@ function useUser() {
125
139
  return useAuthMi().user;
126
140
  }
127
141
  function useAuthActions() {
128
- const { login, logout, isLoading } = useAuthMi();
129
- return { login, logout, isLoading };
142
+ const {
143
+ login,
144
+ signup,
145
+ logout,
146
+ isLoading,
147
+ forgotPassword,
148
+ setup2fa,
149
+ enable2fa,
150
+ disable2fa,
151
+ regenerateBackupCodes,
152
+ challenge2fa,
153
+ initiateOAuth
154
+ } = useAuthMi();
155
+ return {
156
+ login,
157
+ signup,
158
+ logout,
159
+ isLoading,
160
+ forgotPassword,
161
+ setup2fa,
162
+ enable2fa,
163
+ disable2fa,
164
+ regenerateBackupCodes,
165
+ challenge2fa,
166
+ initiateOAuth
167
+ };
130
168
  }
131
169
  function Protected({ children, fallback = null }) {
132
170
  const { isAuthenticated, isLoading } = useAuthMi();
133
- if (isLoading) {
134
- return /* @__PURE__ */ jsx(Fragment, { children: fallback });
135
- }
136
- if (!isAuthenticated) {
137
- return null;
138
- }
171
+ if (isLoading) return /* @__PURE__ */ jsx(Fragment, { children: fallback });
172
+ if (!isAuthenticated) return null;
139
173
  return /* @__PURE__ */ jsx(Fragment, { children });
140
174
  }
141
175
  function LoginForm({ onSuccess, onError, className }) {
@@ -150,9 +184,9 @@ function LoginForm({ onSuccess, onError, className }) {
150
184
  await login(email, password);
151
185
  onSuccess?.();
152
186
  } catch (err) {
153
- const message = err instanceof Error ? err.message : "Login failed";
154
- setError(message);
155
- onError?.(err instanceof Error ? err : new Error(message));
187
+ const msg = err instanceof Error ? err.message : "Login failed";
188
+ setError(msg);
189
+ onError?.(err instanceof Error ? err : new Error(msg));
156
190
  }
157
191
  };
158
192
  return /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, className, children: [
@@ -190,14 +224,7 @@ function LoginForm({ onSuccess, onError, className }) {
190
224
  {
191
225
  type: "submit",
192
226
  disabled: isLoading,
193
- style: {
194
- padding: "0.5rem 1rem",
195
- background: isLoading ? "#ccc" : "#007bff",
196
- color: "white",
197
- border: "none",
198
- borderRadius: "4px",
199
- cursor: isLoading ? "not-allowed" : "pointer"
200
- },
227
+ style: { padding: "0.5rem 1rem", background: isLoading ? "#ccc" : "#007bff", color: "white", border: "none", borderRadius: "4px", cursor: isLoading ? "not-allowed" : "pointer" },
201
228
  children: isLoading ? "Logging in..." : "Login"
202
229
  }
203
230
  )
@@ -207,9 +234,7 @@ export {
207
234
  AuthMiClient,
208
235
  AuthMiError,
209
236
  AuthMiProvider,
210
- LocalStorageTokenStorage,
211
237
  LoginForm,
212
- MemoryTokenStorage,
213
238
  Protected,
214
239
  ScopeError,
215
240
  useAuthActions,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "authmi",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Official client library for Auth Mi - Multi-tenant authentication-as-a-service",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -45,7 +45,9 @@
45
45
  "scripts": {
46
46
  "build": "tsup src/index.ts src/react.tsx src/next.tsx --format cjs,esm --dts --clean",
47
47
  "dev": "tsup src/index.ts src/react.tsx src/next.tsx --format cjs,esm --dts --watch",
48
- "test": "vitest",
48
+ "test": "vitest run",
49
+ "test:watch": "vitest",
50
+ "test:integration": "vitest run src/tests/client.integ.test.ts",
49
51
  "lint": "eslint src",
50
52
  "typecheck": "tsc --noEmit",
51
53
  "prepublishOnly": "npm run build"
@@ -65,13 +67,13 @@
65
67
  "license": "MIT",
66
68
  "repository": {
67
69
  "type": "git",
68
- "url": "https://gitee.com/YonghaoQiu/easy-auth-api.git",
70
+ "url": "https://github.com/Qiu-Technologies/authmi.git",
69
71
  "directory": "packages/easy-auth-client"
70
72
  },
71
73
  "bugs": {
72
- "url": "https://gitee.com/YonghaoQiu/easy-auth-api/issues"
74
+ "url": "https://github.com/Qiu-Technologies/authmi/issues"
73
75
  },
74
- "homepage": "https://gitee.com/YonghaoQiu/easy-auth-api#readme",
76
+ "homepage": "https://github.com/Qiu-Technologies/authmi#readme",
75
77
  "peerDependencies": {
76
78
  "react": "^18.0.0 || ^19.0.0"
77
79
  },
@@ -81,7 +83,7 @@
81
83
  }
82
84
  },
83
85
  "devDependencies": {
84
- "@types/node": "^20.0.0",
86
+ "@types/node": "^22",
85
87
  "@types/react": "^19.0.0",
86
88
  "eslint": "^8.0.0",
87
89
  "next": "^16.0.0",