@take-out/better-auth-utils 0.4.3 → 0.4.4
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/cjs/createAuthClient.cjs +140 -89
- package/dist/cjs/createAuthClient.native.js +185 -141
- package/dist/cjs/createAuthClient.native.js.map +1 -1
- package/dist/cjs/index.cjs +7 -5
- package/dist/cjs/index.native.js +7 -5
- package/dist/cjs/index.native.js.map +1 -1
- package/dist/cjs/server.cjs +57 -42
- package/dist/cjs/server.native.js +137 -92
- package/dist/cjs/server.native.js.map +1 -1
- package/dist/esm/createAuthClient.mjs +126 -77
- package/dist/esm/createAuthClient.mjs.map +1 -1
- package/dist/esm/createAuthClient.native.js +171 -129
- package/dist/esm/createAuthClient.native.js.map +1 -1
- package/dist/esm/server.mjs +45 -32
- package/dist/esm/server.mjs.map +1 -1
- package/dist/esm/server.native.js +125 -82
- package/dist/esm/server.native.js.map +1 -1
- package/package.json +3 -3
|
@@ -3,48 +3,51 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
|
3
3
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
5
|
var __export = (target, all) => {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
for (var name in all) __defProp(target, name, {
|
|
7
|
+
get: all[name],
|
|
8
|
+
enumerable: true
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
14
|
get: () => from[key],
|
|
14
15
|
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
16
|
});
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
18
20
|
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
19
|
-
value:
|
|
21
|
+
value: true
|
|
20
22
|
}), mod);
|
|
21
23
|
var createAuthClient_exports = {};
|
|
22
24
|
__export(createAuthClient_exports, {
|
|
23
25
|
createBetterAuthClient: () => createBetterAuthClient
|
|
24
26
|
});
|
|
25
27
|
module.exports = __toCommonJS(createAuthClient_exports);
|
|
26
|
-
var import_helpers = require("@take-out/helpers")
|
|
27
|
-
|
|
28
|
+
var import_helpers = require("@take-out/helpers");
|
|
29
|
+
var import_client = require("better-auth/client");
|
|
28
30
|
function createBetterAuthClient(options) {
|
|
29
31
|
const {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
32
|
+
onAuthStateChange,
|
|
33
|
+
onAuthError,
|
|
34
|
+
createUser,
|
|
35
|
+
storagePrefix = "auth",
|
|
36
|
+
retryDelay = 4e3,
|
|
37
|
+
useJWT = false,
|
|
38
|
+
authCookieNames = ["better-auth.jwt", "better-auth.session_token"],
|
|
39
|
+
...authClientOptions
|
|
40
|
+
} = options;
|
|
41
|
+
const empty = {
|
|
42
|
+
state: "logged-out",
|
|
43
|
+
session: null,
|
|
44
|
+
user: null,
|
|
45
|
+
token: null
|
|
46
|
+
};
|
|
47
|
+
const keysStorage = (0, import_helpers.createStorageValue)(`${storagePrefix}-keys`);
|
|
48
|
+
const stateStorage = (0, import_helpers.createStorageValue)(`${storagePrefix}-state`);
|
|
49
|
+
const createAuthClientWithSession = session => {
|
|
50
|
+
return (0, import_client.createAuthClient)({
|
|
48
51
|
...authClientOptions,
|
|
49
52
|
fetchOptions: {
|
|
50
53
|
credentials: "include",
|
|
@@ -53,6 +56,7 @@ function createBetterAuthClient(options) {
|
|
|
53
56
|
} : void 0
|
|
54
57
|
}
|
|
55
58
|
});
|
|
59
|
+
};
|
|
56
60
|
let authClient = (() => {
|
|
57
61
|
const existingSession = keysStorage.get()?.session;
|
|
58
62
|
return existingSession ? createAuthClientWithSession(existingSession) : (0, import_client.createAuthClient)({
|
|
@@ -63,79 +67,108 @@ function createBetterAuthClient(options) {
|
|
|
63
67
|
});
|
|
64
68
|
})();
|
|
65
69
|
const authState = (0, import_helpers.createEmitter)("authState", stateStorage.get() || empty, {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
comparator: import_helpers.isEqualDeepLite
|
|
71
|
+
});
|
|
72
|
+
const authClientVersion = (0, import_helpers.createEmitter)("authClientVersion", 0);
|
|
73
|
+
const setState = update => {
|
|
74
|
+
const current = authState.value;
|
|
75
|
+
const next = {
|
|
76
|
+
...current,
|
|
77
|
+
...update
|
|
78
|
+
};
|
|
79
|
+
stateStorage.set(next);
|
|
80
|
+
authState.emit(next);
|
|
81
|
+
if (next.token && next.session) {
|
|
82
|
+
keysStorage.set({
|
|
75
83
|
token: next.token,
|
|
76
84
|
session: next.session.token
|
|
77
|
-
})
|
|
85
|
+
});
|
|
86
|
+
} else if (next.session) {
|
|
87
|
+
keysStorage.set({
|
|
78
88
|
token: "",
|
|
79
89
|
session: next.session.token
|
|
80
|
-
})
|
|
90
|
+
});
|
|
91
|
+
} else {
|
|
92
|
+
keysStorage.set({
|
|
81
93
|
token: "",
|
|
82
94
|
session: ""
|
|
83
|
-
})
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
onAuthStateChange?.(next);
|
|
98
|
+
};
|
|
99
|
+
const setAuthClientToken = async props => {
|
|
100
|
+
keysStorage.set(props);
|
|
101
|
+
updateAuthClient(props.session);
|
|
102
|
+
};
|
|
88
103
|
function updateAuthClient(session) {
|
|
89
|
-
authClient = createAuthClientWithSession(session)
|
|
104
|
+
authClient = createAuthClientWithSession(session);
|
|
105
|
+
authClientVersion.emit(Math.random());
|
|
106
|
+
subscribeToAuthEffect();
|
|
90
107
|
}
|
|
91
|
-
let dispose = null
|
|
92
|
-
|
|
108
|
+
let dispose = null;
|
|
109
|
+
let retryTimer = null;
|
|
93
110
|
function subscribeToAuthEffect() {
|
|
94
|
-
dispose?.()
|
|
111
|
+
dispose?.();
|
|
112
|
+
dispose = authClient.useSession.subscribe(async props => {
|
|
95
113
|
const {
|
|
96
114
|
data: dataGeneric,
|
|
97
115
|
isPending,
|
|
98
116
|
error
|
|
99
117
|
} = props;
|
|
100
118
|
if (error) {
|
|
101
|
-
onAuthError?.(error)
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
119
|
+
onAuthError?.(error);
|
|
120
|
+
const hasPersistedSession2 = !!keysStorage.get()?.session;
|
|
121
|
+
if (!hasPersistedSession2) {
|
|
122
|
+
setState({
|
|
123
|
+
state: "logged-out",
|
|
124
|
+
session: null,
|
|
125
|
+
user: null
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
scheduleAuthRetry(retryDelay);
|
|
106
129
|
return;
|
|
107
130
|
}
|
|
108
|
-
const data = dataGeneric
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
131
|
+
const data = dataGeneric;
|
|
132
|
+
const hasPersistedSession = !!keysStorage.get()?.session;
|
|
133
|
+
const nextState = isPending ? "loading" : data?.session ? "logged-in" : hasPersistedSession && data === void 0 ? "loading" : "logged-out";
|
|
134
|
+
const sessionUpdate = nextState === "loading" ? {} : {
|
|
135
|
+
session: data?.session ?? null,
|
|
136
|
+
user: data?.user ? createUser ? createUser(data.user) : data.user : null
|
|
137
|
+
};
|
|
138
|
+
const previousSession = authState.value?.session;
|
|
139
|
+
const isNewSession = data?.session && (!previousSession || previousSession.id !== data.session.id || previousSession.userId !== data.session.userId);
|
|
117
140
|
setState({
|
|
118
141
|
state: nextState,
|
|
119
142
|
...sessionUpdate
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
143
|
+
});
|
|
144
|
+
if (useJWT && data?.session && (isNewSession || !authState.value.token)) {
|
|
145
|
+
if (isNewSession && authState.value.token) {
|
|
146
|
+
setState({
|
|
147
|
+
token: null
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
getValidToken().then(token => {
|
|
151
|
+
if (token) {
|
|
152
|
+
setState({
|
|
153
|
+
token
|
|
154
|
+
});
|
|
155
|
+
}
|
|
125
156
|
});
|
|
126
|
-
}
|
|
157
|
+
}
|
|
127
158
|
});
|
|
128
159
|
}
|
|
129
160
|
function scheduleAuthRetry(delayMs) {
|
|
130
|
-
|
|
131
|
-
|
|
161
|
+
if (retryTimer) clearTimeout(retryTimer);
|
|
162
|
+
retryTimer = setTimeout(() => {
|
|
163
|
+
retryTimer = null;
|
|
164
|
+
subscribeToAuthEffect();
|
|
132
165
|
}, delayMs);
|
|
133
166
|
}
|
|
134
167
|
async function getValidToken() {
|
|
135
168
|
const res = await authClient.$fetch("/token");
|
|
136
169
|
if (res.error) {
|
|
137
170
|
console.error(`Error fetching token: ${res.error.statusText}`);
|
|
138
|
-
return;
|
|
171
|
+
return void 0;
|
|
139
172
|
}
|
|
140
173
|
return res.data?.token;
|
|
141
174
|
}
|
|
@@ -143,37 +176,55 @@ function createBetterAuthClient(options) {
|
|
|
143
176
|
keysStorage.remove();
|
|
144
177
|
};
|
|
145
178
|
function clearAuthCookies() {
|
|
146
|
-
if (
|
|
179
|
+
if (typeof document === "undefined") return;
|
|
180
|
+
for (const cookieName of authCookieNames) {
|
|
147
181
|
document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`;
|
|
148
182
|
const domain = window.location.hostname;
|
|
149
|
-
document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=${domain}
|
|
183
|
+
document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=${domain}`;
|
|
184
|
+
if (domain.startsWith(".")) {
|
|
185
|
+
document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain=${domain.slice(1)}`;
|
|
186
|
+
}
|
|
150
187
|
}
|
|
151
188
|
}
|
|
152
189
|
function clearAllAuth() {
|
|
153
|
-
clearAuthCookies()
|
|
190
|
+
clearAuthCookies();
|
|
191
|
+
clearState();
|
|
154
192
|
}
|
|
155
193
|
const getAuth = () => {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
194
|
+
const state = authState?.value || empty;
|
|
195
|
+
return {
|
|
196
|
+
...state,
|
|
197
|
+
loggedIn: !!state.session
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
const useAuth = () => {
|
|
201
|
+
return (0, import_helpers.useEmitterValue)(authState) || empty;
|
|
202
|
+
};
|
|
163
203
|
function clearState() {
|
|
164
|
-
keysStorage.remove()
|
|
204
|
+
keysStorage.remove();
|
|
205
|
+
stateStorage.remove();
|
|
206
|
+
setState(empty);
|
|
165
207
|
}
|
|
166
|
-
|
|
208
|
+
subscribeToAuthEffect();
|
|
209
|
+
if (typeof window !== "undefined" && window.addEventListener) {
|
|
167
210
|
const cleanup = () => {
|
|
168
|
-
dispose?.()
|
|
211
|
+
dispose?.();
|
|
212
|
+
if (retryTimer) clearTimeout(retryTimer);
|
|
169
213
|
};
|
|
170
214
|
window.addEventListener("beforeunload", cleanup);
|
|
171
215
|
}
|
|
172
216
|
const proxiedAuthClient = new Proxy(authClient, {
|
|
173
217
|
get(_target, key) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
218
|
+
if (key === "signOut") {
|
|
219
|
+
return () => {
|
|
220
|
+
clearState();
|
|
221
|
+
authClient.signOut?.();
|
|
222
|
+
if (typeof window !== "undefined") {
|
|
223
|
+
window.location?.reload?.();
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
return Reflect.get(authClient, key);
|
|
177
228
|
}
|
|
178
229
|
});
|
|
179
230
|
return {
|