@qyh213/easyauth-client 1.0.0-beta.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/LICENSE +21 -0
- package/README.md +425 -0
- package/dist/chunk-H65ZLXQJ.mjs +495 -0
- package/dist/client-RPDHpVsj.d.mts +300 -0
- package/dist/client-RPDHpVsj.d.ts +300 -0
- package/dist/index.d.mts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +530 -0
- package/dist/index.mjs +18 -0
- package/dist/next.d.mts +106 -0
- package/dist/next.d.ts +106 -0
- package/dist/next.js +638 -0
- package/dist/next.mjs +124 -0
- package/dist/react.d.mts +68 -0
- package/dist/react.d.ts +68 -0
- package/dist/react.js +733 -0
- package/dist/react.mjs +220 -0
- package/package.json +99 -0
package/dist/react.mjs
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EasyAuthClient,
|
|
3
|
+
EasyAuthError,
|
|
4
|
+
LocalStorageTokenStorage,
|
|
5
|
+
MemoryTokenStorage,
|
|
6
|
+
ScopeError
|
|
7
|
+
} from "./chunk-H65ZLXQJ.mjs";
|
|
8
|
+
|
|
9
|
+
// src/react.tsx
|
|
10
|
+
import {
|
|
11
|
+
createContext,
|
|
12
|
+
useContext,
|
|
13
|
+
useState,
|
|
14
|
+
useEffect,
|
|
15
|
+
useCallback
|
|
16
|
+
} from "react";
|
|
17
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
18
|
+
var EasyAuthContext = createContext(null);
|
|
19
|
+
function EasyAuthProvider({
|
|
20
|
+
config,
|
|
21
|
+
tokenStorage,
|
|
22
|
+
children,
|
|
23
|
+
validateOnMount = true,
|
|
24
|
+
validationInterval = 5 * 60 * 1e3
|
|
25
|
+
}) {
|
|
26
|
+
const [client] = useState(() => new EasyAuthClient(config, tokenStorage));
|
|
27
|
+
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
|
28
|
+
const [user, setUser] = useState(null);
|
|
29
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
30
|
+
useEffect(() => {
|
|
31
|
+
const checkAuth = async () => {
|
|
32
|
+
const token = client.getAccessToken();
|
|
33
|
+
if (token && validateOnMount) {
|
|
34
|
+
try {
|
|
35
|
+
const result = await client.validate();
|
|
36
|
+
if (result.valid) {
|
|
37
|
+
setIsAuthenticated(true);
|
|
38
|
+
} else {
|
|
39
|
+
client.clearAccessToken();
|
|
40
|
+
}
|
|
41
|
+
} catch (error) {
|
|
42
|
+
client.clearAccessToken();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
setIsLoading(false);
|
|
46
|
+
};
|
|
47
|
+
checkAuth();
|
|
48
|
+
}, [client, validateOnMount]);
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
if (!isAuthenticated || !validationInterval) return;
|
|
51
|
+
const interval = setInterval(async () => {
|
|
52
|
+
try {
|
|
53
|
+
const result = await client.validate();
|
|
54
|
+
if (!result.valid) {
|
|
55
|
+
setIsAuthenticated(false);
|
|
56
|
+
setUser(null);
|
|
57
|
+
}
|
|
58
|
+
} catch (error) {
|
|
59
|
+
setIsAuthenticated(false);
|
|
60
|
+
setUser(null);
|
|
61
|
+
}
|
|
62
|
+
}, validationInterval);
|
|
63
|
+
return () => clearInterval(interval);
|
|
64
|
+
}, [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
|
+
);
|
|
82
|
+
const logout = useCallback(async () => {
|
|
83
|
+
setIsLoading(true);
|
|
84
|
+
try {
|
|
85
|
+
await client.logout();
|
|
86
|
+
} finally {
|
|
87
|
+
setIsAuthenticated(false);
|
|
88
|
+
setUser(null);
|
|
89
|
+
setIsLoading(false);
|
|
90
|
+
}
|
|
91
|
+
}, [client]);
|
|
92
|
+
const validate = useCallback(async () => {
|
|
93
|
+
const result = await client.validate();
|
|
94
|
+
if (!result.valid) {
|
|
95
|
+
setIsAuthenticated(false);
|
|
96
|
+
setUser(null);
|
|
97
|
+
}
|
|
98
|
+
return result;
|
|
99
|
+
}, [client]);
|
|
100
|
+
const value = {
|
|
101
|
+
client,
|
|
102
|
+
isAuthenticated,
|
|
103
|
+
user,
|
|
104
|
+
isLoading,
|
|
105
|
+
login,
|
|
106
|
+
logout,
|
|
107
|
+
validate
|
|
108
|
+
};
|
|
109
|
+
return /* @__PURE__ */ jsx(EasyAuthContext.Provider, { value, children });
|
|
110
|
+
}
|
|
111
|
+
function useEasyAuth() {
|
|
112
|
+
const context = useContext(EasyAuthContext);
|
|
113
|
+
if (!context) {
|
|
114
|
+
throw new Error("useEasyAuth must be used within EasyAuthProvider");
|
|
115
|
+
}
|
|
116
|
+
return context;
|
|
117
|
+
}
|
|
118
|
+
function useEasyAuthClient() {
|
|
119
|
+
return useEasyAuth().client;
|
|
120
|
+
}
|
|
121
|
+
function useIsAuthenticated() {
|
|
122
|
+
return useEasyAuth().isAuthenticated;
|
|
123
|
+
}
|
|
124
|
+
function useUser() {
|
|
125
|
+
return useEasyAuth().user;
|
|
126
|
+
}
|
|
127
|
+
function useAuthActions() {
|
|
128
|
+
const { login, logout, isLoading } = useEasyAuth();
|
|
129
|
+
return { login, logout, isLoading };
|
|
130
|
+
}
|
|
131
|
+
function Protected({ children, fallback = null }) {
|
|
132
|
+
const { isAuthenticated, isLoading } = useEasyAuth();
|
|
133
|
+
if (isLoading) {
|
|
134
|
+
return /* @__PURE__ */ jsx(Fragment, { children: fallback });
|
|
135
|
+
}
|
|
136
|
+
if (!isAuthenticated) {
|
|
137
|
+
return null;
|
|
138
|
+
}
|
|
139
|
+
return /* @__PURE__ */ jsx(Fragment, { children });
|
|
140
|
+
}
|
|
141
|
+
function LoginForm({ onSuccess, onError, className }) {
|
|
142
|
+
const { login, isLoading } = useEasyAuth();
|
|
143
|
+
const [email, setEmail] = useState("");
|
|
144
|
+
const [password, setPassword] = useState("");
|
|
145
|
+
const [error, setError] = useState(null);
|
|
146
|
+
const handleSubmit = async (e) => {
|
|
147
|
+
e.preventDefault();
|
|
148
|
+
setError(null);
|
|
149
|
+
try {
|
|
150
|
+
await login(email, password);
|
|
151
|
+
onSuccess?.();
|
|
152
|
+
} catch (err) {
|
|
153
|
+
const message = err instanceof Error ? err.message : "Login failed";
|
|
154
|
+
setError(message);
|
|
155
|
+
onError?.(err instanceof Error ? err : new Error(message));
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
return /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, className, children: [
|
|
159
|
+
error && /* @__PURE__ */ jsx("div", { style: { color: "red", marginBottom: "1rem" }, children: error }),
|
|
160
|
+
/* @__PURE__ */ jsxs("div", { style: { marginBottom: "1rem" }, children: [
|
|
161
|
+
/* @__PURE__ */ jsx("label", { htmlFor: "email", children: "Email" }),
|
|
162
|
+
/* @__PURE__ */ jsx(
|
|
163
|
+
"input",
|
|
164
|
+
{
|
|
165
|
+
id: "email",
|
|
166
|
+
type: "email",
|
|
167
|
+
value: email,
|
|
168
|
+
onChange: (e) => setEmail(e.target.value),
|
|
169
|
+
required: true,
|
|
170
|
+
style: { display: "block", width: "100%", padding: "0.5rem" }
|
|
171
|
+
}
|
|
172
|
+
)
|
|
173
|
+
] }),
|
|
174
|
+
/* @__PURE__ */ jsxs("div", { style: { marginBottom: "1rem" }, children: [
|
|
175
|
+
/* @__PURE__ */ jsx("label", { htmlFor: "password", children: "Password" }),
|
|
176
|
+
/* @__PURE__ */ jsx(
|
|
177
|
+
"input",
|
|
178
|
+
{
|
|
179
|
+
id: "password",
|
|
180
|
+
type: "password",
|
|
181
|
+
value: password,
|
|
182
|
+
onChange: (e) => setPassword(e.target.value),
|
|
183
|
+
required: true,
|
|
184
|
+
style: { display: "block", width: "100%", padding: "0.5rem" }
|
|
185
|
+
}
|
|
186
|
+
)
|
|
187
|
+
] }),
|
|
188
|
+
/* @__PURE__ */ jsx(
|
|
189
|
+
"button",
|
|
190
|
+
{
|
|
191
|
+
type: "submit",
|
|
192
|
+
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
|
+
},
|
|
201
|
+
children: isLoading ? "Logging in..." : "Login"
|
|
202
|
+
}
|
|
203
|
+
)
|
|
204
|
+
] });
|
|
205
|
+
}
|
|
206
|
+
export {
|
|
207
|
+
EasyAuthClient,
|
|
208
|
+
EasyAuthError,
|
|
209
|
+
EasyAuthProvider,
|
|
210
|
+
LocalStorageTokenStorage,
|
|
211
|
+
LoginForm,
|
|
212
|
+
MemoryTokenStorage,
|
|
213
|
+
Protected,
|
|
214
|
+
ScopeError,
|
|
215
|
+
useAuthActions,
|
|
216
|
+
useEasyAuth,
|
|
217
|
+
useEasyAuthClient,
|
|
218
|
+
useIsAuthenticated,
|
|
219
|
+
useUser
|
|
220
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@qyh213/easyauth-client",
|
|
3
|
+
"version": "1.0.0-beta.1",
|
|
4
|
+
"description": "Official client library for Easy Auth - Multi-tenant authentication-as-a-service",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.mjs"
|
|
13
|
+
},
|
|
14
|
+
"require": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"./react": {
|
|
20
|
+
"import": {
|
|
21
|
+
"types": "./dist/react.d.ts",
|
|
22
|
+
"default": "./dist/react.mjs"
|
|
23
|
+
},
|
|
24
|
+
"require": {
|
|
25
|
+
"types": "./dist/react.d.ts",
|
|
26
|
+
"default": "./dist/react.js"
|
|
27
|
+
}
|
|
28
|
+
},
|
|
29
|
+
"./next": {
|
|
30
|
+
"import": {
|
|
31
|
+
"types": "./dist/next.d.ts",
|
|
32
|
+
"default": "./dist/next.mjs"
|
|
33
|
+
},
|
|
34
|
+
"require": {
|
|
35
|
+
"types": "./dist/next.d.ts",
|
|
36
|
+
"default": "./dist/next.js"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
"files": [
|
|
41
|
+
"dist",
|
|
42
|
+
"README.md",
|
|
43
|
+
"LICENSE"
|
|
44
|
+
],
|
|
45
|
+
"scripts": {
|
|
46
|
+
"build": "tsup src/index.ts src/react.tsx src/next.tsx --format cjs,esm --dts --clean",
|
|
47
|
+
"dev": "tsup src/index.ts src/react.tsx src/next.tsx --format cjs,esm --dts --watch",
|
|
48
|
+
"test": "vitest",
|
|
49
|
+
"lint": "eslint src",
|
|
50
|
+
"typecheck": "tsc --noEmit",
|
|
51
|
+
"prepublishOnly": "npm run build"
|
|
52
|
+
},
|
|
53
|
+
"keywords": [
|
|
54
|
+
"auth",
|
|
55
|
+
"authentication",
|
|
56
|
+
"easy-auth",
|
|
57
|
+
"multi-tenant",
|
|
58
|
+
"api-keys",
|
|
59
|
+
"jwt",
|
|
60
|
+
"tokens",
|
|
61
|
+
"react",
|
|
62
|
+
"nextjs"
|
|
63
|
+
],
|
|
64
|
+
"author": "Easy Auth Team",
|
|
65
|
+
"license": "MIT",
|
|
66
|
+
"repository": {
|
|
67
|
+
"type": "git",
|
|
68
|
+
"url": "https://github.com/yourorg/easy-auth.git",
|
|
69
|
+
"directory": "packages/easy-auth-client"
|
|
70
|
+
},
|
|
71
|
+
"bugs": {
|
|
72
|
+
"url": "https://github.com/yourorg/easy-auth/issues"
|
|
73
|
+
},
|
|
74
|
+
"homepage": "https://github.com/yourorg/easy-auth#readme",
|
|
75
|
+
"peerDependencies": {
|
|
76
|
+
"react": "^18.0.0 || ^19.0.0",
|
|
77
|
+
"next": "^14.0.0 || ^15.0.0"
|
|
78
|
+
},
|
|
79
|
+
"peerDependenciesMeta": {
|
|
80
|
+
"react": {
|
|
81
|
+
"optional": true
|
|
82
|
+
},
|
|
83
|
+
"next": {
|
|
84
|
+
"optional": true
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
"devDependencies": {
|
|
88
|
+
"@types/node": "^20.0.0",
|
|
89
|
+
"@types/react": "^18.0.0",
|
|
90
|
+
"eslint": "^8.0.0",
|
|
91
|
+
"react": "^18.0.0",
|
|
92
|
+
"tsup": "^8.0.0",
|
|
93
|
+
"typescript": "^5.0.0",
|
|
94
|
+
"vitest": "^1.0.0"
|
|
95
|
+
},
|
|
96
|
+
"engines": {
|
|
97
|
+
"node": ">=18.0.0"
|
|
98
|
+
}
|
|
99
|
+
}
|