@niledatabase/client 5.0.0-alpha.10
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 +38 -0
- package/dist/index.d.mts +208 -0
- package/dist/index.d.ts +208 -0
- package/dist/index.js +734 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +697 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +62 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,734 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
Authorizer: () => Authorizer,
|
|
24
|
+
auth: () => auth,
|
|
25
|
+
broadcast: () => broadcast,
|
|
26
|
+
getCsrfToken: () => getCsrfToken,
|
|
27
|
+
getProviders: () => getProviders,
|
|
28
|
+
getSession: () => getSession,
|
|
29
|
+
getStatus: () => getStatus,
|
|
30
|
+
resetPassword: () => resetPassword,
|
|
31
|
+
signIn: () => signIn,
|
|
32
|
+
signOut: () => signOut,
|
|
33
|
+
signUp: () => signUp
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
|
|
37
|
+
// src/logger.ts
|
|
38
|
+
var UnknownError = class extends Error {
|
|
39
|
+
code;
|
|
40
|
+
constructor(error) {
|
|
41
|
+
super(error?.message ?? error);
|
|
42
|
+
this.name = "UnknownError";
|
|
43
|
+
this.code = error.code;
|
|
44
|
+
if (error instanceof Error) {
|
|
45
|
+
this.stack = error.stack;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
toJSON() {
|
|
49
|
+
return {
|
|
50
|
+
name: this.name,
|
|
51
|
+
message: this.message,
|
|
52
|
+
stack: this.stack
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
function formatError(o) {
|
|
57
|
+
if (o instanceof Error && !(o instanceof UnknownError)) {
|
|
58
|
+
return JSON.stringify({ message: o.message, stack: o.stack, name: o.name });
|
|
59
|
+
}
|
|
60
|
+
if (hasErrorProperty(o)) {
|
|
61
|
+
o.error = formatError(o.error);
|
|
62
|
+
o.message = o.message ?? o.error.message;
|
|
63
|
+
}
|
|
64
|
+
return o;
|
|
65
|
+
}
|
|
66
|
+
function hasErrorProperty(x) {
|
|
67
|
+
return !!x?.error;
|
|
68
|
+
}
|
|
69
|
+
var _logger = {
|
|
70
|
+
error(code, metadata) {
|
|
71
|
+
metadata = formatError(metadata);
|
|
72
|
+
console.error(`[nile-auth][error][${code}]`, metadata.message, metadata);
|
|
73
|
+
},
|
|
74
|
+
warn(code) {
|
|
75
|
+
console.warn(`[nile-auth][warn][${code}]`);
|
|
76
|
+
},
|
|
77
|
+
debug(code, metadata) {
|
|
78
|
+
console.log(`[next-auth][debug][${code}]`, metadata);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
function proxyLogger(logger2 = _logger, authorizer2) {
|
|
82
|
+
try {
|
|
83
|
+
if (typeof window === "undefined") {
|
|
84
|
+
return logger2;
|
|
85
|
+
}
|
|
86
|
+
const clientLogger = {};
|
|
87
|
+
for (const level in logger2) {
|
|
88
|
+
clientLogger[level] = (code, metadata) => {
|
|
89
|
+
_logger[level](code, metadata);
|
|
90
|
+
if (level === "error") {
|
|
91
|
+
metadata = formatError(metadata);
|
|
92
|
+
}
|
|
93
|
+
metadata.client = true;
|
|
94
|
+
const url = `${authorizer2.state.basePath}/_log`;
|
|
95
|
+
const body = new URLSearchParams({ level, code, ...metadata });
|
|
96
|
+
if (navigator.sendBeacon) {
|
|
97
|
+
return navigator.sendBeacon(url, body);
|
|
98
|
+
}
|
|
99
|
+
return fetch(url, { method: "POST", body, keepalive: true });
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
return clientLogger;
|
|
103
|
+
} catch {
|
|
104
|
+
return _logger;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
var logger = (authorizer2) => proxyLogger(_logger, authorizer2);
|
|
108
|
+
|
|
109
|
+
// src/broadcast.ts
|
|
110
|
+
function now() {
|
|
111
|
+
return Math.floor(Date.now() / 1e3);
|
|
112
|
+
}
|
|
113
|
+
function BroadcastChannel(name = "nextauth.message") {
|
|
114
|
+
return {
|
|
115
|
+
/** Get notified by other tabs/windows. */
|
|
116
|
+
receive(onReceive) {
|
|
117
|
+
const handler = (event) => {
|
|
118
|
+
if (event.key !== name) return;
|
|
119
|
+
const message = JSON.parse(event.newValue ?? "{}");
|
|
120
|
+
if (message?.event !== "session" || !message?.data) return;
|
|
121
|
+
onReceive(message);
|
|
122
|
+
};
|
|
123
|
+
window.addEventListener("storage", handler);
|
|
124
|
+
return () => window.removeEventListener("storage", handler);
|
|
125
|
+
},
|
|
126
|
+
/** Notify other tabs/windows. */
|
|
127
|
+
post(message) {
|
|
128
|
+
if (typeof window === "undefined") return;
|
|
129
|
+
try {
|
|
130
|
+
localStorage.setItem(
|
|
131
|
+
name,
|
|
132
|
+
JSON.stringify({ ...message, timestamp: now() })
|
|
133
|
+
);
|
|
134
|
+
} catch {
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
var broadcast = BroadcastChannel();
|
|
140
|
+
|
|
141
|
+
// src/observable.ts
|
|
142
|
+
function isEqual(a, b) {
|
|
143
|
+
if (a === b) return true;
|
|
144
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false;
|
|
148
|
+
const keysA = Object.keys(a);
|
|
149
|
+
const keysB = Object.keys(b);
|
|
150
|
+
if (keysA.length !== keysB.length) return false;
|
|
151
|
+
for (const key of keysA) {
|
|
152
|
+
if (!keysB.includes(key) || !isEqual(a[key], b[key])) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
function createObservableObject(obj, listenerKeys = ["loading", "session"], eventName = "objectChange") {
|
|
159
|
+
const eventTarget = new EventTarget();
|
|
160
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
161
|
+
const handler = {
|
|
162
|
+
set(target, key, value) {
|
|
163
|
+
const prev = target[key];
|
|
164
|
+
target[key] = value;
|
|
165
|
+
if (isEqual(prev, value)) return true;
|
|
166
|
+
if (listenerKeys.includes(String(key))) {
|
|
167
|
+
eventTarget.dispatchEvent(
|
|
168
|
+
new CustomEvent(eventName, {
|
|
169
|
+
detail: { key, prev, next: value }
|
|
170
|
+
})
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
return true;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
return {
|
|
177
|
+
proxy: new Proxy(obj, handler),
|
|
178
|
+
eventTarget,
|
|
179
|
+
addListener(callback) {
|
|
180
|
+
if (listeners.has(callback)) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const wrappedCallback = (e) => callback(e.detail);
|
|
184
|
+
listeners.set(callback, wrappedCallback);
|
|
185
|
+
eventTarget.addEventListener(eventName, wrappedCallback);
|
|
186
|
+
},
|
|
187
|
+
removeListener(callback) {
|
|
188
|
+
const wrappedCallback = listeners.get(callback);
|
|
189
|
+
if (wrappedCallback) {
|
|
190
|
+
eventTarget.removeEventListener(eventName, wrappedCallback);
|
|
191
|
+
listeners.delete(callback);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// src/Authorizer.ts
|
|
198
|
+
var Authorizer = class {
|
|
199
|
+
state;
|
|
200
|
+
#logger;
|
|
201
|
+
requestInit;
|
|
202
|
+
addListener;
|
|
203
|
+
removeListener;
|
|
204
|
+
status;
|
|
205
|
+
constructor(config) {
|
|
206
|
+
const { proxy, addListener, removeListener } = createObservableObject(
|
|
207
|
+
{
|
|
208
|
+
basePath: parseUrl(config?.basePath).path,
|
|
209
|
+
baseUrl: parseUrl(config?.baseUrl).origin,
|
|
210
|
+
lastSync: 0,
|
|
211
|
+
getSession: () => void 0,
|
|
212
|
+
session: void 0,
|
|
213
|
+
loading: true
|
|
214
|
+
},
|
|
215
|
+
config?.listenerKeys,
|
|
216
|
+
"auth"
|
|
217
|
+
);
|
|
218
|
+
this.state = proxy;
|
|
219
|
+
this.addListener = addListener;
|
|
220
|
+
this.removeListener = removeListener;
|
|
221
|
+
this.#logger = logger(this);
|
|
222
|
+
this.status = null;
|
|
223
|
+
}
|
|
224
|
+
async sync(event) {
|
|
225
|
+
try {
|
|
226
|
+
const storageEvent = event === "storage";
|
|
227
|
+
if (storageEvent || !this.state.session) {
|
|
228
|
+
this.state.getSession = await this.getSession;
|
|
229
|
+
this.state.lastSync = now();
|
|
230
|
+
}
|
|
231
|
+
if (!event || this.state.session == null || now() < this.state.lastSync) {
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
this.state.lastSync = Date.now();
|
|
235
|
+
this.state.session = await this.getSession();
|
|
236
|
+
} catch (error) {
|
|
237
|
+
this.#logger.error("CLIENT_SESSION_ERROR", error);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
set baseUrl(val) {
|
|
241
|
+
this.state.baseUrl = val;
|
|
242
|
+
this.#logger = logger(this);
|
|
243
|
+
}
|
|
244
|
+
get baseUrl() {
|
|
245
|
+
this.#logger = logger(this);
|
|
246
|
+
return this.state.baseUrl;
|
|
247
|
+
}
|
|
248
|
+
configure(config) {
|
|
249
|
+
if (config?.basePath) this.state.basePath = parseUrl(config?.basePath).path;
|
|
250
|
+
if (config?.baseUrl) this.baseUrl = config.baseUrl;
|
|
251
|
+
if (config?.init) this.requestInit = config.init;
|
|
252
|
+
return this;
|
|
253
|
+
}
|
|
254
|
+
sanitize() {
|
|
255
|
+
return {
|
|
256
|
+
state: {
|
|
257
|
+
baseUrl: this.baseUrl,
|
|
258
|
+
session: {
|
|
259
|
+
user: {
|
|
260
|
+
email: this.state.session?.user?.email
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
},
|
|
264
|
+
requestInit: this.requestInit
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
async initialize(params) {
|
|
268
|
+
const { baseUrl, session, event } = params ?? {};
|
|
269
|
+
if (baseUrl) this.baseUrl = baseUrl;
|
|
270
|
+
const hasInitialSession = session !== void 0;
|
|
271
|
+
this.state.loading = !hasInitialSession;
|
|
272
|
+
this.state.lastSync = hasInitialSession ? now() : 0;
|
|
273
|
+
this.state.session = session;
|
|
274
|
+
await this.sync(event);
|
|
275
|
+
}
|
|
276
|
+
get apiBaseUrl() {
|
|
277
|
+
return `${this.baseUrl}${this.state.basePath}`;
|
|
278
|
+
}
|
|
279
|
+
async #sendData(url, init) {
|
|
280
|
+
try {
|
|
281
|
+
const options = {
|
|
282
|
+
headers: {
|
|
283
|
+
"Content-Type": "application/json"
|
|
284
|
+
},
|
|
285
|
+
...this.requestInit ? this.requestInit : {},
|
|
286
|
+
...init
|
|
287
|
+
};
|
|
288
|
+
const filledUrl = !url.startsWith("http") ? `${window.location.origin}${url}` : url;
|
|
289
|
+
const res = await fetch(filledUrl, options);
|
|
290
|
+
this.state.loading = false;
|
|
291
|
+
return res;
|
|
292
|
+
} catch (error) {
|
|
293
|
+
this.#logger.error("CLIENT_FETCH_ERROR", { error, url });
|
|
294
|
+
return void 0;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
async #fetchData(url, init) {
|
|
298
|
+
const options = {
|
|
299
|
+
...this.requestInit ? this.requestInit : {},
|
|
300
|
+
...init
|
|
301
|
+
};
|
|
302
|
+
const res = await this.#sendData(url, options);
|
|
303
|
+
const errorHandler = res?.clone();
|
|
304
|
+
try {
|
|
305
|
+
if (res?.ok) {
|
|
306
|
+
const data = await res.json();
|
|
307
|
+
this.state.loading = false;
|
|
308
|
+
return Object.keys(data).length > 0 ? data : void 0;
|
|
309
|
+
} else {
|
|
310
|
+
const error = await errorHandler?.text();
|
|
311
|
+
if (error) {
|
|
312
|
+
const updatedUrl = new URL(url);
|
|
313
|
+
updatedUrl.searchParams.set("error", error);
|
|
314
|
+
return { url: updatedUrl.toString() };
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
} catch (error) {
|
|
318
|
+
if (error instanceof Error) {
|
|
319
|
+
if (!error.message.includes("is not valid JSON")) {
|
|
320
|
+
this.#logger.error("CLIENT_FETCH_ERROR", {
|
|
321
|
+
error,
|
|
322
|
+
url
|
|
323
|
+
});
|
|
324
|
+
} else {
|
|
325
|
+
const error2 = await errorHandler?.text();
|
|
326
|
+
if (error2) {
|
|
327
|
+
const updatedUrl = new URL(url);
|
|
328
|
+
updatedUrl.searchParams.set("error", error2);
|
|
329
|
+
return { url: updatedUrl.toString() };
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
return void 0;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
async #fetchFormData(url, init) {
|
|
337
|
+
try {
|
|
338
|
+
const res = await fetch(url, {
|
|
339
|
+
...this.requestInit,
|
|
340
|
+
...init,
|
|
341
|
+
headers: {
|
|
342
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
if (res) {
|
|
346
|
+
if (res.ok) {
|
|
347
|
+
return {
|
|
348
|
+
data: await res.json(),
|
|
349
|
+
status: res.status,
|
|
350
|
+
ok: res.ok,
|
|
351
|
+
url: res.url
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
const { url: responseUrl } = await res.json();
|
|
355
|
+
return {
|
|
356
|
+
data: {},
|
|
357
|
+
status: res.status,
|
|
358
|
+
ok: res?.ok,
|
|
359
|
+
url: responseUrl
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
throw new Error(`Unable to fetch ${url}`);
|
|
363
|
+
} catch (error) {
|
|
364
|
+
if (error instanceof Error) {
|
|
365
|
+
if (!error.message.includes("is not valid JSON")) {
|
|
366
|
+
this.#logger.error("CLIENT_FETCH_ERROR", {
|
|
367
|
+
error,
|
|
368
|
+
url
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
return void 0;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
async getProviders(url) {
|
|
376
|
+
return await this.#fetchData(url ?? `${this.apiBaseUrl}/auth/providers`);
|
|
377
|
+
}
|
|
378
|
+
async getCsrfToken(url) {
|
|
379
|
+
const response = await this.#fetchData(
|
|
380
|
+
url ?? `${this.apiBaseUrl}/auth/csrf`
|
|
381
|
+
);
|
|
382
|
+
return response?.csrfToken;
|
|
383
|
+
}
|
|
384
|
+
async getSession(params) {
|
|
385
|
+
if (this.status === "getSession" /* SESSION */) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
this.status = "getSession" /* SESSION */;
|
|
389
|
+
if (params?.init) {
|
|
390
|
+
this.requestInit = params.init;
|
|
391
|
+
}
|
|
392
|
+
if (params?.baseUrl) {
|
|
393
|
+
this.baseUrl = params.baseUrl;
|
|
394
|
+
}
|
|
395
|
+
if (this.state.session && now() < this.state.lastSync) {
|
|
396
|
+
this.status = null;
|
|
397
|
+
return this.state.session;
|
|
398
|
+
}
|
|
399
|
+
this.state.loading = true;
|
|
400
|
+
const session = await this.#fetchData(
|
|
401
|
+
`${this.apiBaseUrl}/auth/session`
|
|
402
|
+
);
|
|
403
|
+
broadcast.post({ event: "session", data: { trigger: "getSession" } });
|
|
404
|
+
this.status = null;
|
|
405
|
+
if (session) {
|
|
406
|
+
this.state.session = session;
|
|
407
|
+
await this.sync("storage");
|
|
408
|
+
return { ...session, loading: this.state.loading };
|
|
409
|
+
}
|
|
410
|
+
return { loading: this.state.loading };
|
|
411
|
+
}
|
|
412
|
+
async refreshSession() {
|
|
413
|
+
this.state.loading = true;
|
|
414
|
+
const session = await this.#fetchData(
|
|
415
|
+
`${this.apiBaseUrl}/auth/session`
|
|
416
|
+
);
|
|
417
|
+
broadcast.post({ event: "session", data: { trigger: "getSession" } });
|
|
418
|
+
this.state.session = session;
|
|
419
|
+
await this.sync("storage");
|
|
420
|
+
return session;
|
|
421
|
+
}
|
|
422
|
+
async signOut(options) {
|
|
423
|
+
const {
|
|
424
|
+
callbackUrl = window.location.href,
|
|
425
|
+
baseUrl,
|
|
426
|
+
auth: auth2,
|
|
427
|
+
fetchUrl,
|
|
428
|
+
basePath
|
|
429
|
+
} = options ?? {};
|
|
430
|
+
if (basePath) {
|
|
431
|
+
this.state.basePath = basePath;
|
|
432
|
+
}
|
|
433
|
+
if (baseUrl) {
|
|
434
|
+
this.baseUrl = baseUrl;
|
|
435
|
+
}
|
|
436
|
+
if (auth2) {
|
|
437
|
+
this.requestInit = auth2.requestInit;
|
|
438
|
+
if (auth2.state?.baseUrl) {
|
|
439
|
+
this.baseUrl = auth2.state.baseUrl;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
const baseFetch = fetchUrl ?? `${this.apiBaseUrl}/auth/signout`;
|
|
443
|
+
const fetchOptions = {
|
|
444
|
+
method: "post",
|
|
445
|
+
body: new URLSearchParams({
|
|
446
|
+
csrfToken: String(await this.getCsrfToken()),
|
|
447
|
+
callbackUrl,
|
|
448
|
+
json: String(true)
|
|
449
|
+
})
|
|
450
|
+
};
|
|
451
|
+
const res = await this.#fetchFormData(
|
|
452
|
+
baseFetch,
|
|
453
|
+
fetchOptions
|
|
454
|
+
);
|
|
455
|
+
broadcast.post({ event: "session", data: { trigger: "signout" } });
|
|
456
|
+
if (this.requestInit?.credentials) {
|
|
457
|
+
window.location.href = callbackUrl;
|
|
458
|
+
if (callbackUrl.includes("#")) window.location.reload();
|
|
459
|
+
return void 0;
|
|
460
|
+
}
|
|
461
|
+
if (options?.redirect ?? true) {
|
|
462
|
+
const url = res?.data?.url ?? callbackUrl;
|
|
463
|
+
window.location.href = url;
|
|
464
|
+
if (url.includes("#")) window.location.reload();
|
|
465
|
+
return void 0;
|
|
466
|
+
}
|
|
467
|
+
await this.state.getSession({ event: "storage" });
|
|
468
|
+
return res?.data;
|
|
469
|
+
}
|
|
470
|
+
async signIn(provider, options, authorizationParams) {
|
|
471
|
+
const {
|
|
472
|
+
callbackUrl = window.location.href,
|
|
473
|
+
resetUrl = window.location.href,
|
|
474
|
+
providersUrl,
|
|
475
|
+
csrfUrl,
|
|
476
|
+
baseUrl,
|
|
477
|
+
fetchUrl,
|
|
478
|
+
init,
|
|
479
|
+
auth: auth2,
|
|
480
|
+
redirect = true,
|
|
481
|
+
...remaining
|
|
482
|
+
} = options ?? {};
|
|
483
|
+
if (baseUrl) {
|
|
484
|
+
this.baseUrl = baseUrl;
|
|
485
|
+
}
|
|
486
|
+
if (auth2) {
|
|
487
|
+
if (auth2.requestInit) {
|
|
488
|
+
this.requestInit = auth2.requestInit;
|
|
489
|
+
}
|
|
490
|
+
if (auth2.state?.baseUrl) {
|
|
491
|
+
this.baseUrl = auth2.state.baseUrl;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
if (init) {
|
|
495
|
+
this.requestInit = init;
|
|
496
|
+
}
|
|
497
|
+
const providers = await this.getProviders(providersUrl);
|
|
498
|
+
if (!providers) {
|
|
499
|
+
return { error: "No providers enabled" };
|
|
500
|
+
}
|
|
501
|
+
if (!provider || !(provider in providers)) {
|
|
502
|
+
return { error: `Provider ${provider} not enabled` };
|
|
503
|
+
}
|
|
504
|
+
const isCredentials = providers[provider].type === "credentials";
|
|
505
|
+
const isEmail = providers[provider].type === "email";
|
|
506
|
+
const isSupportingReturn = isCredentials || isEmail;
|
|
507
|
+
const baseFetch = `${this.apiBaseUrl}/auth`;
|
|
508
|
+
const signInUrl = fetchUrl ?? `${baseFetch}/${isCredentials ? "callback" : "signin"}/${provider}`;
|
|
509
|
+
const _signInUrl = `${signInUrl}${authorizationParams ? `?${new URLSearchParams(authorizationParams)}` : ""}`;
|
|
510
|
+
const data = await this.#fetchFormData(_signInUrl, {
|
|
511
|
+
method: "post",
|
|
512
|
+
body: new URLSearchParams({
|
|
513
|
+
...remaining,
|
|
514
|
+
csrfToken: String(await this.getCsrfToken(csrfUrl)),
|
|
515
|
+
callbackUrl,
|
|
516
|
+
json: String(true),
|
|
517
|
+
resetUrl
|
|
518
|
+
})
|
|
519
|
+
});
|
|
520
|
+
if (data?.ok && this.requestInit?.credentials && isSupportingReturn) {
|
|
521
|
+
window.location.reload();
|
|
522
|
+
return;
|
|
523
|
+
}
|
|
524
|
+
if (data?.ok && (redirect || !isSupportingReturn)) {
|
|
525
|
+
const url = data?.data.url ?? callbackUrl;
|
|
526
|
+
window.location.href = url;
|
|
527
|
+
if (url.includes("#")) window.location.reload();
|
|
528
|
+
return;
|
|
529
|
+
}
|
|
530
|
+
const error = data?.data?.url ? new URL(String(data?.data?.url)).searchParams.get("error") : null;
|
|
531
|
+
if (data?.ok) {
|
|
532
|
+
await this.initialize();
|
|
533
|
+
await this.getSession({ event: "storage" });
|
|
534
|
+
}
|
|
535
|
+
return {
|
|
536
|
+
error,
|
|
537
|
+
status: data?.status,
|
|
538
|
+
ok: data?.ok,
|
|
539
|
+
url: error ? null : data?.url
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
async signUp(options) {
|
|
543
|
+
const {
|
|
544
|
+
password,
|
|
545
|
+
tenantId,
|
|
546
|
+
fetchUrl,
|
|
547
|
+
newTenantName,
|
|
548
|
+
createTenant,
|
|
549
|
+
baseUrl,
|
|
550
|
+
init,
|
|
551
|
+
email,
|
|
552
|
+
auth: auth2,
|
|
553
|
+
callbackUrl = window.location.href
|
|
554
|
+
} = options;
|
|
555
|
+
if (baseUrl) {
|
|
556
|
+
this.baseUrl = baseUrl;
|
|
557
|
+
}
|
|
558
|
+
if (auth2) {
|
|
559
|
+
if (auth2.requestInit) {
|
|
560
|
+
this.requestInit = auth2.requestInit;
|
|
561
|
+
}
|
|
562
|
+
if (auth2.state?.baseUrl) {
|
|
563
|
+
this.baseUrl = auth2.state.baseUrl;
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
if (init) {
|
|
567
|
+
this.requestInit = init;
|
|
568
|
+
}
|
|
569
|
+
const searchParams = new URLSearchParams();
|
|
570
|
+
if (newTenantName) {
|
|
571
|
+
searchParams.set("newTenantName", newTenantName);
|
|
572
|
+
} else if (createTenant) {
|
|
573
|
+
if (typeof createTenant === "boolean") {
|
|
574
|
+
searchParams.set("newTenantName", email);
|
|
575
|
+
} else if (typeof createTenant === "string") {
|
|
576
|
+
searchParams.set("newTenantName", createTenant);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
if (tenantId) {
|
|
580
|
+
searchParams.set("tenantId", tenantId);
|
|
581
|
+
}
|
|
582
|
+
let signUpUrl = fetchUrl ?? `${this.apiBaseUrl}/signup`;
|
|
583
|
+
if (searchParams.size > 0) {
|
|
584
|
+
signUpUrl += `?${searchParams}`;
|
|
585
|
+
}
|
|
586
|
+
const data = await this.#fetchData(signUpUrl, {
|
|
587
|
+
method: "post",
|
|
588
|
+
body: JSON.stringify({ email, password })
|
|
589
|
+
});
|
|
590
|
+
const error = data?.url ? new URL(data.url).searchParams.get("error") : null;
|
|
591
|
+
if (!error) {
|
|
592
|
+
if (data) {
|
|
593
|
+
await this.initialize({ event: "storage" });
|
|
594
|
+
await this.getSession({ event: "storage" });
|
|
595
|
+
}
|
|
596
|
+
if (this.requestInit?.credentials) {
|
|
597
|
+
window.location.reload();
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
if (options?.redirect ?? true) {
|
|
601
|
+
const url = callbackUrl;
|
|
602
|
+
window.location.href = url;
|
|
603
|
+
if (url.includes("#")) window.location.reload();
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
return {
|
|
607
|
+
data,
|
|
608
|
+
error
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
async resetPassword(options) {
|
|
612
|
+
const { password, fetchUrl, email, redirect, callbackUrl } = options;
|
|
613
|
+
this.#configureFetch(options);
|
|
614
|
+
const resetPasswordUrl = fetchUrl ?? `${this.apiBaseUrl}/auth/reset-password`;
|
|
615
|
+
let resetPasswordWithParams = resetPasswordUrl;
|
|
616
|
+
const searchParams = new URLSearchParams();
|
|
617
|
+
if (redirect === false) {
|
|
618
|
+
searchParams.set("json", "true");
|
|
619
|
+
}
|
|
620
|
+
if (searchParams.size > 0) {
|
|
621
|
+
resetPasswordWithParams += `?${searchParams}`;
|
|
622
|
+
}
|
|
623
|
+
const data = await this.#sendData(resetPasswordWithParams, {
|
|
624
|
+
method: "post",
|
|
625
|
+
body: JSON.stringify({
|
|
626
|
+
email,
|
|
627
|
+
password,
|
|
628
|
+
redirectUrl: resetPasswordUrl,
|
|
629
|
+
callbackUrl
|
|
630
|
+
})
|
|
631
|
+
});
|
|
632
|
+
if (!data?.ok) {
|
|
633
|
+
throw new Error(await data?.clone().text());
|
|
634
|
+
}
|
|
635
|
+
if (redirect === false) {
|
|
636
|
+
const json = await data?.json();
|
|
637
|
+
const { url: urlWithParams } = json;
|
|
638
|
+
resetPasswordWithParams = `${urlWithParams}&redirect=false`;
|
|
639
|
+
await this.#sendData(resetPasswordWithParams);
|
|
640
|
+
return await this.#sendData(resetPasswordWithParams, {
|
|
641
|
+
method: password ? "put" : "post",
|
|
642
|
+
body: JSON.stringify({ email, password })
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
#configureFetch(params) {
|
|
647
|
+
const { baseUrl, init, auth: auth2 } = params;
|
|
648
|
+
if (baseUrl) {
|
|
649
|
+
this.baseUrl = baseUrl;
|
|
650
|
+
}
|
|
651
|
+
if (auth2) {
|
|
652
|
+
if (auth2.requestInit) {
|
|
653
|
+
this.requestInit = auth2.requestInit;
|
|
654
|
+
}
|
|
655
|
+
if (auth2.state?.baseUrl) {
|
|
656
|
+
this.baseUrl = auth2.state.baseUrl;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
if (init) {
|
|
660
|
+
this.requestInit = init;
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
function parseUrl(url) {
|
|
665
|
+
let defaultUrl = new URL("http://localhost:3000");
|
|
666
|
+
if (typeof window !== "undefined") {
|
|
667
|
+
defaultUrl = new URL(`${window.location.origin}/api`);
|
|
668
|
+
}
|
|
669
|
+
if (url && !url.startsWith("http")) {
|
|
670
|
+
url = `https://${url}`;
|
|
671
|
+
}
|
|
672
|
+
const _url = new URL(url ?? defaultUrl);
|
|
673
|
+
const path = (_url.pathname === "/" ? defaultUrl.pathname : _url.pathname).replace(/\/$/, "");
|
|
674
|
+
const base = `${_url.origin}${path}`;
|
|
675
|
+
return {
|
|
676
|
+
origin: _url.origin,
|
|
677
|
+
host: _url.host,
|
|
678
|
+
path,
|
|
679
|
+
base,
|
|
680
|
+
toString: () => base
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
var authorizer = new Authorizer();
|
|
684
|
+
var _auth = () => {
|
|
685
|
+
return authorizer;
|
|
686
|
+
};
|
|
687
|
+
var auth = _auth();
|
|
688
|
+
var getSession = async function getSession2(params) {
|
|
689
|
+
return await auth.getSession(params);
|
|
690
|
+
};
|
|
691
|
+
var getCsrfToken = async function getCsrfToken2(url) {
|
|
692
|
+
return auth.getCsrfToken(url);
|
|
693
|
+
};
|
|
694
|
+
var getProviders = async function getProviders2() {
|
|
695
|
+
return auth.getProviders();
|
|
696
|
+
};
|
|
697
|
+
var signOut = async function signOut2(options) {
|
|
698
|
+
return auth.signOut(options);
|
|
699
|
+
};
|
|
700
|
+
var signIn = async function signIn2(provider, options, authParams) {
|
|
701
|
+
return auth.signIn(provider, options, authParams);
|
|
702
|
+
};
|
|
703
|
+
var signUp = async function signUp2(options) {
|
|
704
|
+
return auth.signUp(options);
|
|
705
|
+
};
|
|
706
|
+
var resetPassword = async function resetPassword2(options) {
|
|
707
|
+
return auth.resetPassword(options);
|
|
708
|
+
};
|
|
709
|
+
|
|
710
|
+
// src/status.ts
|
|
711
|
+
function getStatus(load, sess) {
|
|
712
|
+
if (load) {
|
|
713
|
+
return "loading";
|
|
714
|
+
}
|
|
715
|
+
if (sess) {
|
|
716
|
+
return "authenticated";
|
|
717
|
+
}
|
|
718
|
+
return "unauthenticated";
|
|
719
|
+
}
|
|
720
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
721
|
+
0 && (module.exports = {
|
|
722
|
+
Authorizer,
|
|
723
|
+
auth,
|
|
724
|
+
broadcast,
|
|
725
|
+
getCsrfToken,
|
|
726
|
+
getProviders,
|
|
727
|
+
getSession,
|
|
728
|
+
getStatus,
|
|
729
|
+
resetPassword,
|
|
730
|
+
signIn,
|
|
731
|
+
signOut,
|
|
732
|
+
signUp
|
|
733
|
+
});
|
|
734
|
+
//# sourceMappingURL=index.js.map
|