@ubean/auth 0.1.12 → 0.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/dist/core.d.ts +2 -3
- package/dist/core.js +7 -159
- package/dist/index.d.ts +3 -4
- package/dist/index.js +3 -4
- package/dist/runtime-DRgQYona.d.ts +64 -0
- package/dist/runtime-Dbttti-m.js +309 -0
- package/dist/runtime.d.ts +1 -60
- package/dist/runtime.js +1 -154
- package/dist/vite.js +4 -3
- package/package.json +9 -9
package/dist/core.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as AuthState, c as ResolvedAuthOptions, d as UbeanAuthOptions, i as AuthSession,
|
|
1
|
+
import { a as AuthState, c as ResolvedAuthOptions, d as UbeanAuthOptions, i as AuthSession, o as AuthUser } from "./types-DlL9lr7A.js";
|
|
2
2
|
import { Context, MiddlewareHandler } from "hono";
|
|
3
3
|
import { BetterAuthOptions } from "better-auth";
|
|
4
4
|
//#region src/core.d.ts
|
|
@@ -15,7 +15,6 @@ declare function authMiddleware(): MiddlewareHandler;
|
|
|
15
15
|
declare function getUser(): AuthUser | null;
|
|
16
16
|
declare function getSession(): AuthState | null;
|
|
17
17
|
declare function requireAuth(c?: Context): AuthUser;
|
|
18
|
-
declare function createAuthClient(basePath?: string): AuthClient;
|
|
19
18
|
declare function getServerSession(req?: Request): Promise<AuthSession | null>;
|
|
20
19
|
//#endregion
|
|
21
|
-
export { type
|
|
20
|
+
export { type AuthSession, type AuthState, type AuthUser, authMiddleware, createAuthHandler, defineAuth, getServerSession, getSession, getUser, requireAuth, resolveAuthOptions };
|
package/dist/core.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { consola } from "consola";
|
|
2
1
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
3
2
|
import { defu } from "defu";
|
|
3
|
+
import { getLogger } from "@ubean/logger";
|
|
4
4
|
//#region src/core.ts
|
|
5
|
+
const logger = getLogger("auth");
|
|
5
6
|
const AUTH_CONTEXT_SYMBOL = Symbol.for("ubean.authContext.v1");
|
|
6
7
|
function getAuthAsyncLocalStorage() {
|
|
7
8
|
const g = globalThis;
|
|
@@ -36,8 +37,8 @@ function resolveAuthOptions(options = {}) {
|
|
|
36
37
|
},
|
|
37
38
|
session: {
|
|
38
39
|
cookieName: "ubean_session",
|
|
39
|
-
expiresIn:
|
|
40
|
-
updateAge:
|
|
40
|
+
expiresIn: 604800,
|
|
41
|
+
updateAge: 86400
|
|
41
42
|
}
|
|
42
43
|
});
|
|
43
44
|
}
|
|
@@ -49,8 +50,8 @@ function defineAuth(config) {
|
|
|
49
50
|
return config;
|
|
50
51
|
}
|
|
51
52
|
function createFallbackAuth(resolved) {
|
|
52
|
-
|
|
53
|
-
|
|
53
|
+
logger.warn("[auth] better-auth not installed, using minimal fallback auth implementation");
|
|
54
|
+
logger.warn("[auth] Install better-auth with: pnpm add better-auth");
|
|
54
55
|
const users = /* @__PURE__ */ new Map();
|
|
55
56
|
const sessions = /* @__PURE__ */ new Map();
|
|
56
57
|
function generateId() {
|
|
@@ -345,159 +346,6 @@ function requireAuth(c) {
|
|
|
345
346
|
}
|
|
346
347
|
return user;
|
|
347
348
|
}
|
|
348
|
-
function createAuthClient(basePath = "/api/auth") {
|
|
349
|
-
async function authFetch(path, init) {
|
|
350
|
-
const res = await fetch(`${basePath}${path}`, {
|
|
351
|
-
credentials: "include",
|
|
352
|
-
headers: {
|
|
353
|
-
"content-type": "application/json",
|
|
354
|
-
...init?.headers
|
|
355
|
-
},
|
|
356
|
-
...init
|
|
357
|
-
});
|
|
358
|
-
if (!res.ok) throw await res.json().catch(() => ({ message: res.statusText }));
|
|
359
|
-
return await res.json().catch(() => null);
|
|
360
|
-
}
|
|
361
|
-
function buildFetchOptions(body) {
|
|
362
|
-
return {
|
|
363
|
-
method: "POST",
|
|
364
|
-
body: body ? JSON.stringify(body) : void 0
|
|
365
|
-
};
|
|
366
|
-
}
|
|
367
|
-
return {
|
|
368
|
-
signIn: {
|
|
369
|
-
email: async ({ email, password, callbackURL, rememberMe }) => {
|
|
370
|
-
try {
|
|
371
|
-
const data = await authFetch("/sign-in/email", buildFetchOptions({
|
|
372
|
-
email,
|
|
373
|
-
password,
|
|
374
|
-
rememberMe
|
|
375
|
-
}));
|
|
376
|
-
if (callbackURL && typeof window !== "undefined") window.location.href = callbackURL;
|
|
377
|
-
return { data };
|
|
378
|
-
} catch (error) {
|
|
379
|
-
return { error };
|
|
380
|
-
}
|
|
381
|
-
},
|
|
382
|
-
social: (provider, opts) => {
|
|
383
|
-
if (typeof window === "undefined") return Promise.resolve();
|
|
384
|
-
const redirect = opts?.callbackURL || window.location.href;
|
|
385
|
-
window.location.href = `${basePath}/sign-in/social/${provider}?callbackURL=${encodeURIComponent(redirect)}`;
|
|
386
|
-
return Promise.resolve();
|
|
387
|
-
}
|
|
388
|
-
},
|
|
389
|
-
signUp: { email: async ({ email, password, name, callbackURL }) => {
|
|
390
|
-
try {
|
|
391
|
-
const data = await authFetch("/sign-up/email", buildFetchOptions({
|
|
392
|
-
email,
|
|
393
|
-
password,
|
|
394
|
-
name
|
|
395
|
-
}));
|
|
396
|
-
if (callbackURL && typeof window !== "undefined") window.location.href = callbackURL;
|
|
397
|
-
return { data };
|
|
398
|
-
} catch (error) {
|
|
399
|
-
return { error };
|
|
400
|
-
}
|
|
401
|
-
} },
|
|
402
|
-
signOut: async () => {
|
|
403
|
-
try {
|
|
404
|
-
return { data: await authFetch("/sign-out", buildFetchOptions()) };
|
|
405
|
-
} catch (error) {
|
|
406
|
-
return { error };
|
|
407
|
-
}
|
|
408
|
-
},
|
|
409
|
-
getSession: async () => {
|
|
410
|
-
try {
|
|
411
|
-
return await authFetch("/get-session");
|
|
412
|
-
} catch {
|
|
413
|
-
return null;
|
|
414
|
-
}
|
|
415
|
-
},
|
|
416
|
-
sendVerificationEmail: async (email, opts) => {
|
|
417
|
-
try {
|
|
418
|
-
return { data: await authFetch("/send-verification-email", buildFetchOptions({
|
|
419
|
-
email,
|
|
420
|
-
...opts
|
|
421
|
-
})) };
|
|
422
|
-
} catch (error) {
|
|
423
|
-
return { error };
|
|
424
|
-
}
|
|
425
|
-
},
|
|
426
|
-
resetPassword: async (newPassword, token) => {
|
|
427
|
-
try {
|
|
428
|
-
return { data: await authFetch("/reset-password", buildFetchOptions({
|
|
429
|
-
newPassword,
|
|
430
|
-
token
|
|
431
|
-
})) };
|
|
432
|
-
} catch (error) {
|
|
433
|
-
return { error };
|
|
434
|
-
}
|
|
435
|
-
},
|
|
436
|
-
forgetPassword: async (email, opts) => {
|
|
437
|
-
try {
|
|
438
|
-
return { data: await authFetch("/forget-password", buildFetchOptions({
|
|
439
|
-
email,
|
|
440
|
-
...opts
|
|
441
|
-
})) };
|
|
442
|
-
} catch (error) {
|
|
443
|
-
return { error };
|
|
444
|
-
}
|
|
445
|
-
},
|
|
446
|
-
updateUser: async (data) => {
|
|
447
|
-
try {
|
|
448
|
-
return { data: (await authFetch("/update-user", buildFetchOptions(data))).user };
|
|
449
|
-
} catch (error) {
|
|
450
|
-
return { error };
|
|
451
|
-
}
|
|
452
|
-
},
|
|
453
|
-
changeEmail: async (newEmail, callbackURL) => {
|
|
454
|
-
try {
|
|
455
|
-
return { data: await authFetch("/change-email", buildFetchOptions({
|
|
456
|
-
newEmail,
|
|
457
|
-
callbackURL
|
|
458
|
-
})) };
|
|
459
|
-
} catch (error) {
|
|
460
|
-
return { error };
|
|
461
|
-
}
|
|
462
|
-
},
|
|
463
|
-
changePassword: async (currentPassword, newPassword, revokeOtherSessions) => {
|
|
464
|
-
try {
|
|
465
|
-
return { data: await authFetch("/change-password", buildFetchOptions({
|
|
466
|
-
currentPassword,
|
|
467
|
-
newPassword,
|
|
468
|
-
revokeOtherSessions
|
|
469
|
-
})) };
|
|
470
|
-
} catch (error) {
|
|
471
|
-
return { error };
|
|
472
|
-
}
|
|
473
|
-
},
|
|
474
|
-
listSessions: async () => {
|
|
475
|
-
try {
|
|
476
|
-
return { data: (await authFetch("/list-sessions")).sessions };
|
|
477
|
-
} catch (error) {
|
|
478
|
-
return { error };
|
|
479
|
-
}
|
|
480
|
-
},
|
|
481
|
-
revokeSession: async (sessionId) => {
|
|
482
|
-
try {
|
|
483
|
-
return { data: await authFetch("/revoke-session", buildFetchOptions({ sessionId })) };
|
|
484
|
-
} catch (error) {
|
|
485
|
-
return { error };
|
|
486
|
-
}
|
|
487
|
-
},
|
|
488
|
-
revokeSessions: async () => {
|
|
489
|
-
try {
|
|
490
|
-
return { data: await authFetch("/revoke-sessions", buildFetchOptions()) };
|
|
491
|
-
} catch (error) {
|
|
492
|
-
return { error };
|
|
493
|
-
}
|
|
494
|
-
},
|
|
495
|
-
$fetch: (input, init) => fetch(input, {
|
|
496
|
-
credentials: "include",
|
|
497
|
-
...init
|
|
498
|
-
})
|
|
499
|
-
};
|
|
500
|
-
}
|
|
501
349
|
async function getServerSession(req) {
|
|
502
350
|
if (!authInstance.handler) createAuthHandler();
|
|
503
351
|
await new Promise((resolve) => {
|
|
@@ -517,4 +365,4 @@ async function getServerSession(req) {
|
|
|
517
365
|
return null;
|
|
518
366
|
}
|
|
519
367
|
//#endregion
|
|
520
|
-
export { authMiddleware,
|
|
368
|
+
export { authMiddleware, createAuthHandler, defineAuth, getServerSession, getSession, getUser, requireAuth, resolveAuthOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { a as AuthState, c as ResolvedAuthOptions, d as UbeanAuthOptions, f as UseAuthReturn, i as AuthSession, l as Session, m as Verification, n as AuthClient, o as AuthUser, p as User, r as AuthError, s as BetterAuthOptions, t as Account, u as SocialProviderConfig } from "./types-DlL9lr7A.js";
|
|
2
|
-
import { authMiddleware,
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
export { type Account, type AuthClient, type AuthError, type AuthSession, type AuthState, type AuthUser, type BetterAuthOptions as BetterAuthConfig, type BetterAuthOptions, type ResolvedAuthOptions, type Session, type SocialProviderConfig, type UbeanAuthOptions, type UseAuthReturn, type User, type Verification, authMiddleware, createAuthClient, createAuthHandler, defineAuth, defineAuthConfig, getSession as getAuthSession, getServerSession, getSessionFromHeaders, getUser, protectRoute, requireAuth, resolveAuthOptions, ubeanAuthPlugin, useAuth, useSession };
|
|
2
|
+
import { authMiddleware, createAuthHandler, defineAuth, getServerSession, getSession, getUser, requireAuth, resolveAuthOptions } from "./core.js";
|
|
3
|
+
import { a as createAuthClient, i as useSession, n as protectRoute, r as useAuth, t as getSessionFromHeaders } from "./runtime-DRgQYona.js";
|
|
4
|
+
export { type Account, type AuthClient, type AuthError, type AuthSession, type AuthState, type AuthUser, type BetterAuthOptions as BetterAuthConfig, type BetterAuthOptions, type ResolvedAuthOptions, type Session, type SocialProviderConfig, type UbeanAuthOptions, type UseAuthReturn, type User, type Verification, authMiddleware, createAuthClient, createAuthHandler, defineAuth, getSession as getAuthSession, getServerSession, getSessionFromHeaders, getUser, protectRoute, requireAuth, resolveAuthOptions, useAuth, useSession };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { authMiddleware,
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
export { authMiddleware, createAuthClient, createAuthHandler, defineAuth, defineAuthConfig, getSession as getAuthSession, getServerSession, getSessionFromHeaders, getUser, protectRoute, requireAuth, resolveAuthOptions, ubeanAuthPlugin, useAuth, useSession };
|
|
1
|
+
import { authMiddleware, createAuthHandler, defineAuth, getServerSession, getSession, getUser, requireAuth, resolveAuthOptions } from "./core.js";
|
|
2
|
+
import { a as createAuthClient, i as useSession, n as protectRoute, r as useAuth, t as getSessionFromHeaders } from "./runtime-Dbttti-m.js";
|
|
3
|
+
export { authMiddleware, createAuthClient, createAuthHandler, defineAuth, getSession as getAuthSession, getServerSession, getSessionFromHeaders, getUser, protectRoute, requireAuth, resolveAuthOptions, useAuth, useSession };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { f as UseAuthReturn, i as AuthSession, n as AuthClient, r as AuthError } from "./types-DlL9lr7A.js";
|
|
2
|
+
//#region src/client.d.ts
|
|
3
|
+
declare function createAuthClient(basePath?: string): AuthClient;
|
|
4
|
+
//#endregion
|
|
5
|
+
//#region src/runtime.d.ts
|
|
6
|
+
declare function useAuth(basePath?: string): UseAuthReturn;
|
|
7
|
+
declare function useSession(basePath?: string): {
|
|
8
|
+
data: import("vue").Ref<{
|
|
9
|
+
session: {
|
|
10
|
+
id: string;
|
|
11
|
+
createdAt: Date;
|
|
12
|
+
updatedAt: Date;
|
|
13
|
+
userId: string;
|
|
14
|
+
expiresAt: Date;
|
|
15
|
+
token: string;
|
|
16
|
+
ipAddress?: string | null | undefined;
|
|
17
|
+
userAgent?: string | null | undefined;
|
|
18
|
+
};
|
|
19
|
+
user: {
|
|
20
|
+
id: string;
|
|
21
|
+
createdAt: Date;
|
|
22
|
+
updatedAt: Date;
|
|
23
|
+
email: string;
|
|
24
|
+
emailVerified: boolean;
|
|
25
|
+
name: string;
|
|
26
|
+
image?: string | null | undefined;
|
|
27
|
+
};
|
|
28
|
+
} | null, AuthSession | {
|
|
29
|
+
session: {
|
|
30
|
+
id: string;
|
|
31
|
+
createdAt: Date;
|
|
32
|
+
updatedAt: Date;
|
|
33
|
+
userId: string;
|
|
34
|
+
expiresAt: Date;
|
|
35
|
+
token: string;
|
|
36
|
+
ipAddress?: string | null | undefined;
|
|
37
|
+
userAgent?: string | null | undefined;
|
|
38
|
+
};
|
|
39
|
+
user: {
|
|
40
|
+
id: string;
|
|
41
|
+
createdAt: Date;
|
|
42
|
+
updatedAt: Date;
|
|
43
|
+
email: string;
|
|
44
|
+
emailVerified: boolean;
|
|
45
|
+
name: string;
|
|
46
|
+
image?: string | null | undefined;
|
|
47
|
+
};
|
|
48
|
+
} | null>;
|
|
49
|
+
isPending: import("vue").Ref<boolean, boolean>;
|
|
50
|
+
error: import("vue").Ref<{
|
|
51
|
+
message: string;
|
|
52
|
+
code?: string | undefined;
|
|
53
|
+
statusCode?: number | undefined;
|
|
54
|
+
} | null, AuthError | {
|
|
55
|
+
message: string;
|
|
56
|
+
code?: string | undefined;
|
|
57
|
+
statusCode?: number | undefined;
|
|
58
|
+
} | null>;
|
|
59
|
+
refetch: () => Promise<void>;
|
|
60
|
+
};
|
|
61
|
+
declare function getSessionFromHeaders(headers: Headers, basePath?: string): Promise<AuthSession | null>;
|
|
62
|
+
declare function protectRoute(redirectTo?: string): void;
|
|
63
|
+
//#endregion
|
|
64
|
+
export { createAuthClient as a, useSession as i, protectRoute as n, useAuth as r, getSessionFromHeaders as t };
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
import { computed, onMounted, ref } from "vue";
|
|
2
|
+
//#region src/client.ts
|
|
3
|
+
function createAuthClient(basePath = "/api/auth") {
|
|
4
|
+
async function authFetch(path, init) {
|
|
5
|
+
const res = await fetch(`${basePath}${path}`, {
|
|
6
|
+
credentials: "include",
|
|
7
|
+
headers: {
|
|
8
|
+
"content-type": "application/json",
|
|
9
|
+
...init?.headers
|
|
10
|
+
},
|
|
11
|
+
...init
|
|
12
|
+
});
|
|
13
|
+
if (!res.ok) throw await res.json().catch(() => ({ message: res.statusText }));
|
|
14
|
+
return await res.json().catch(() => null);
|
|
15
|
+
}
|
|
16
|
+
function buildFetchOptions(body) {
|
|
17
|
+
return {
|
|
18
|
+
method: "POST",
|
|
19
|
+
body: body ? JSON.stringify(body) : void 0
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
signIn: {
|
|
24
|
+
email: async ({ email, password, callbackURL, rememberMe }) => {
|
|
25
|
+
try {
|
|
26
|
+
const data = await authFetch("/sign-in/email", buildFetchOptions({
|
|
27
|
+
email,
|
|
28
|
+
password,
|
|
29
|
+
rememberMe
|
|
30
|
+
}));
|
|
31
|
+
if (callbackURL && typeof window !== "undefined") window.location.href = callbackURL;
|
|
32
|
+
return { data };
|
|
33
|
+
} catch (error) {
|
|
34
|
+
return { error };
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
social: (provider, opts) => {
|
|
38
|
+
if (typeof window === "undefined") return Promise.resolve();
|
|
39
|
+
const redirect = opts?.callbackURL || window.location.href;
|
|
40
|
+
window.location.href = `${basePath}/sign-in/social/${provider}?callbackURL=${encodeURIComponent(redirect)}`;
|
|
41
|
+
return Promise.resolve();
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
signUp: { email: async ({ email, password, name, callbackURL }) => {
|
|
45
|
+
try {
|
|
46
|
+
const data = await authFetch("/sign-up/email", buildFetchOptions({
|
|
47
|
+
email,
|
|
48
|
+
password,
|
|
49
|
+
name
|
|
50
|
+
}));
|
|
51
|
+
if (callbackURL && typeof window !== "undefined") window.location.href = callbackURL;
|
|
52
|
+
return { data };
|
|
53
|
+
} catch (error) {
|
|
54
|
+
return { error };
|
|
55
|
+
}
|
|
56
|
+
} },
|
|
57
|
+
signOut: async () => {
|
|
58
|
+
try {
|
|
59
|
+
return { data: await authFetch("/sign-out", buildFetchOptions()) };
|
|
60
|
+
} catch (error) {
|
|
61
|
+
return { error };
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
getSession: async () => {
|
|
65
|
+
try {
|
|
66
|
+
return await authFetch("/get-session");
|
|
67
|
+
} catch {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
sendVerificationEmail: async (email, opts) => {
|
|
72
|
+
try {
|
|
73
|
+
return { data: await authFetch("/send-verification-email", buildFetchOptions({
|
|
74
|
+
email,
|
|
75
|
+
...opts
|
|
76
|
+
})) };
|
|
77
|
+
} catch (error) {
|
|
78
|
+
return { error };
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
resetPassword: async (newPassword, token) => {
|
|
82
|
+
try {
|
|
83
|
+
return { data: await authFetch("/reset-password", buildFetchOptions({
|
|
84
|
+
newPassword,
|
|
85
|
+
token
|
|
86
|
+
})) };
|
|
87
|
+
} catch (error) {
|
|
88
|
+
return { error };
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
forgetPassword: async (email, opts) => {
|
|
92
|
+
try {
|
|
93
|
+
return { data: await authFetch("/forget-password", buildFetchOptions({
|
|
94
|
+
email,
|
|
95
|
+
...opts
|
|
96
|
+
})) };
|
|
97
|
+
} catch (error) {
|
|
98
|
+
return { error };
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
updateUser: async (data) => {
|
|
102
|
+
try {
|
|
103
|
+
return { data: (await authFetch("/update-user", buildFetchOptions(data))).user };
|
|
104
|
+
} catch (error) {
|
|
105
|
+
return { error };
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
changeEmail: async (newEmail, callbackURL) => {
|
|
109
|
+
try {
|
|
110
|
+
return { data: await authFetch("/change-email", buildFetchOptions({
|
|
111
|
+
newEmail,
|
|
112
|
+
callbackURL
|
|
113
|
+
})) };
|
|
114
|
+
} catch (error) {
|
|
115
|
+
return { error };
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
changePassword: async (currentPassword, newPassword, revokeOtherSessions) => {
|
|
119
|
+
try {
|
|
120
|
+
return { data: await authFetch("/change-password", buildFetchOptions({
|
|
121
|
+
currentPassword,
|
|
122
|
+
newPassword,
|
|
123
|
+
revokeOtherSessions
|
|
124
|
+
})) };
|
|
125
|
+
} catch (error) {
|
|
126
|
+
return { error };
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
listSessions: async () => {
|
|
130
|
+
try {
|
|
131
|
+
return { data: (await authFetch("/list-sessions")).sessions };
|
|
132
|
+
} catch (error) {
|
|
133
|
+
return { error };
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
revokeSession: async (sessionId) => {
|
|
137
|
+
try {
|
|
138
|
+
return { data: await authFetch("/revoke-session", buildFetchOptions({ sessionId })) };
|
|
139
|
+
} catch (error) {
|
|
140
|
+
return { error };
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
revokeSessions: async () => {
|
|
144
|
+
try {
|
|
145
|
+
return { data: await authFetch("/revoke-sessions", buildFetchOptions()) };
|
|
146
|
+
} catch (error) {
|
|
147
|
+
return { error };
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
$fetch: (input, init) => fetch(input, {
|
|
151
|
+
credentials: "include",
|
|
152
|
+
...init
|
|
153
|
+
})
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
//#endregion
|
|
157
|
+
//#region src/runtime.ts
|
|
158
|
+
function useAuth(basePath = "/api/auth") {
|
|
159
|
+
const client = createAuthClient(basePath);
|
|
160
|
+
const session = ref(null);
|
|
161
|
+
const isLoading = ref(false);
|
|
162
|
+
const isPending = ref(true);
|
|
163
|
+
const error = ref(null);
|
|
164
|
+
const user = computed(() => session.value?.user || null);
|
|
165
|
+
const isAuthenticated = computed(() => !!session.value?.user);
|
|
166
|
+
async function fetchSession() {
|
|
167
|
+
if (typeof window === "undefined") return;
|
|
168
|
+
isLoading.value = true;
|
|
169
|
+
isPending.value = true;
|
|
170
|
+
error.value = null;
|
|
171
|
+
try {
|
|
172
|
+
const result = await client.getSession();
|
|
173
|
+
session.value = result;
|
|
174
|
+
} catch (err) {
|
|
175
|
+
session.value = null;
|
|
176
|
+
error.value = err;
|
|
177
|
+
} finally {
|
|
178
|
+
isLoading.value = false;
|
|
179
|
+
isPending.value = false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const signIn = {
|
|
183
|
+
email: async ({ email, password, callbackURL }) => {
|
|
184
|
+
isLoading.value = true;
|
|
185
|
+
try {
|
|
186
|
+
const result = await client.signIn.email({
|
|
187
|
+
email,
|
|
188
|
+
password,
|
|
189
|
+
callbackURL
|
|
190
|
+
});
|
|
191
|
+
if (result.data) {
|
|
192
|
+
session.value = result.data;
|
|
193
|
+
if (callbackURL && typeof window !== "undefined") window.location.href = callbackURL;
|
|
194
|
+
}
|
|
195
|
+
if (result.error) error.value = result.error;
|
|
196
|
+
return result;
|
|
197
|
+
} finally {
|
|
198
|
+
isLoading.value = false;
|
|
199
|
+
}
|
|
200
|
+
},
|
|
201
|
+
social: (provider, opts) => {
|
|
202
|
+
return client.signIn.social(provider, opts);
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
const signUp = { email: async ({ email, password, name, callbackURL }) => {
|
|
206
|
+
isLoading.value = true;
|
|
207
|
+
try {
|
|
208
|
+
const result = await client.signUp.email({
|
|
209
|
+
email,
|
|
210
|
+
password,
|
|
211
|
+
name,
|
|
212
|
+
callbackURL
|
|
213
|
+
});
|
|
214
|
+
if (result.data) {
|
|
215
|
+
session.value = result.data;
|
|
216
|
+
if (callbackURL && typeof window !== "undefined") window.location.href = callbackURL;
|
|
217
|
+
}
|
|
218
|
+
if (result.error) error.value = result.error;
|
|
219
|
+
return result;
|
|
220
|
+
} finally {
|
|
221
|
+
isLoading.value = false;
|
|
222
|
+
}
|
|
223
|
+
} };
|
|
224
|
+
async function signOut() {
|
|
225
|
+
isLoading.value = true;
|
|
226
|
+
try {
|
|
227
|
+
const result = await client.signOut();
|
|
228
|
+
session.value = null;
|
|
229
|
+
return result;
|
|
230
|
+
} finally {
|
|
231
|
+
isLoading.value = false;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async function refreshSession() {
|
|
235
|
+
await fetchSession();
|
|
236
|
+
}
|
|
237
|
+
onMounted(() => {
|
|
238
|
+
fetchSession();
|
|
239
|
+
if (typeof window !== "undefined") {
|
|
240
|
+
window.addEventListener("focus", fetchSession);
|
|
241
|
+
document.addEventListener("visibilitychange", () => {
|
|
242
|
+
if (document.visibilityState === "visible") fetchSession();
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
return {
|
|
247
|
+
session,
|
|
248
|
+
user,
|
|
249
|
+
isLoading,
|
|
250
|
+
isPending,
|
|
251
|
+
isAuthenticated,
|
|
252
|
+
error,
|
|
253
|
+
signIn,
|
|
254
|
+
signUp,
|
|
255
|
+
signOut,
|
|
256
|
+
getSession: client.getSession,
|
|
257
|
+
updateUser: client.updateUser,
|
|
258
|
+
refreshSession
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
function useSession(basePath = "/api/auth") {
|
|
262
|
+
const data = ref(null);
|
|
263
|
+
const isPending = ref(true);
|
|
264
|
+
const error = ref(null);
|
|
265
|
+
const client = createAuthClient(basePath);
|
|
266
|
+
const refetch = async () => {
|
|
267
|
+
isPending.value = true;
|
|
268
|
+
error.value = null;
|
|
269
|
+
try {
|
|
270
|
+
data.value = await client.getSession();
|
|
271
|
+
} catch (err) {
|
|
272
|
+
data.value = null;
|
|
273
|
+
error.value = err;
|
|
274
|
+
} finally {
|
|
275
|
+
isPending.value = false;
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
onMounted(refetch);
|
|
279
|
+
if (typeof window !== "undefined") {
|
|
280
|
+
window.addEventListener("focus", refetch);
|
|
281
|
+
document.addEventListener("visibilitychange", () => {
|
|
282
|
+
if (document.visibilityState === "visible") refetch();
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
return {
|
|
286
|
+
data,
|
|
287
|
+
isPending,
|
|
288
|
+
error,
|
|
289
|
+
refetch
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
function getSessionFromHeaders(headers, basePath = "/api/auth") {
|
|
293
|
+
return fetch(`${basePath}/get-session`, {
|
|
294
|
+
headers: { cookie: headers.get("cookie") || "" },
|
|
295
|
+
credentials: "include"
|
|
296
|
+
}).then((r) => r.json()).then((data) => {
|
|
297
|
+
if (data?.session && data?.user) return data;
|
|
298
|
+
return null;
|
|
299
|
+
}).catch(() => null);
|
|
300
|
+
}
|
|
301
|
+
function protectRoute(redirectTo = "/login") {
|
|
302
|
+
const auth = useAuth();
|
|
303
|
+
if (typeof window !== "undefined" && !auth.isPending.value && !auth.isAuthenticated.value) {
|
|
304
|
+
const redirectUrl = `${redirectTo}?redirect=${encodeURIComponent(window.location.pathname)}`;
|
|
305
|
+
window.location.href = redirectUrl;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
//#endregion
|
|
309
|
+
export { createAuthClient as a, useSession as i, protectRoute as n, useAuth as r, getSessionFromHeaders as t };
|
package/dist/runtime.d.ts
CHANGED
|
@@ -1,62 +1,3 @@
|
|
|
1
1
|
import { f as UseAuthReturn, i as AuthSession, n as AuthClient, p as User, r as AuthError } from "./types-DlL9lr7A.js";
|
|
2
|
-
import { createAuthClient } from "./
|
|
3
|
-
//#region src/runtime.d.ts
|
|
4
|
-
declare function useAuth(basePath?: string): UseAuthReturn;
|
|
5
|
-
declare function useSession(basePath?: string): {
|
|
6
|
-
data: import("vue").Ref<{
|
|
7
|
-
session: {
|
|
8
|
-
id: string;
|
|
9
|
-
createdAt: Date;
|
|
10
|
-
updatedAt: Date;
|
|
11
|
-
userId: string;
|
|
12
|
-
expiresAt: Date;
|
|
13
|
-
token: string;
|
|
14
|
-
ipAddress?: string | null | undefined;
|
|
15
|
-
userAgent?: string | null | undefined;
|
|
16
|
-
};
|
|
17
|
-
user: {
|
|
18
|
-
id: string;
|
|
19
|
-
createdAt: Date;
|
|
20
|
-
updatedAt: Date;
|
|
21
|
-
email: string;
|
|
22
|
-
emailVerified: boolean;
|
|
23
|
-
name: string;
|
|
24
|
-
image?: string | null | undefined;
|
|
25
|
-
};
|
|
26
|
-
} | null, AuthSession | {
|
|
27
|
-
session: {
|
|
28
|
-
id: string;
|
|
29
|
-
createdAt: Date;
|
|
30
|
-
updatedAt: Date;
|
|
31
|
-
userId: string;
|
|
32
|
-
expiresAt: Date;
|
|
33
|
-
token: string;
|
|
34
|
-
ipAddress?: string | null | undefined;
|
|
35
|
-
userAgent?: string | null | undefined;
|
|
36
|
-
};
|
|
37
|
-
user: {
|
|
38
|
-
id: string;
|
|
39
|
-
createdAt: Date;
|
|
40
|
-
updatedAt: Date;
|
|
41
|
-
email: string;
|
|
42
|
-
emailVerified: boolean;
|
|
43
|
-
name: string;
|
|
44
|
-
image?: string | null | undefined;
|
|
45
|
-
};
|
|
46
|
-
} | null>;
|
|
47
|
-
isPending: import("vue").Ref<boolean, boolean>;
|
|
48
|
-
error: import("vue").Ref<{
|
|
49
|
-
message: string;
|
|
50
|
-
code?: string | undefined;
|
|
51
|
-
statusCode?: number | undefined;
|
|
52
|
-
} | null, AuthError | {
|
|
53
|
-
message: string;
|
|
54
|
-
code?: string | undefined;
|
|
55
|
-
statusCode?: number | undefined;
|
|
56
|
-
} | null>;
|
|
57
|
-
refetch: () => Promise<void>;
|
|
58
|
-
};
|
|
59
|
-
declare function getSessionFromHeaders(headers: Headers, basePath?: string): Promise<AuthSession | null>;
|
|
60
|
-
declare function protectRoute(redirectTo?: string): void;
|
|
61
|
-
//#endregion
|
|
2
|
+
import { a as createAuthClient, i as useSession, n as protectRoute, r as useAuth, t as getSessionFromHeaders } from "./runtime-DRgQYona.js";
|
|
62
3
|
export { type AuthClient, type AuthError, type AuthSession, type UseAuthReturn, type User, createAuthClient, getSessionFromHeaders, protectRoute, useAuth, useSession };
|
package/dist/runtime.js
CHANGED
|
@@ -1,155 +1,2 @@
|
|
|
1
|
-
import { createAuthClient } from "./
|
|
2
|
-
import { computed, onMounted, ref } from "vue";
|
|
3
|
-
//#region src/runtime.ts
|
|
4
|
-
function useAuth(basePath = "/api/auth") {
|
|
5
|
-
const client = createAuthClient(basePath);
|
|
6
|
-
const session = ref(null);
|
|
7
|
-
const isLoading = ref(false);
|
|
8
|
-
const isPending = ref(true);
|
|
9
|
-
const error = ref(null);
|
|
10
|
-
const user = computed(() => session.value?.user || null);
|
|
11
|
-
const isAuthenticated = computed(() => !!session.value?.user);
|
|
12
|
-
async function fetchSession() {
|
|
13
|
-
if (typeof window === "undefined") return;
|
|
14
|
-
isLoading.value = true;
|
|
15
|
-
isPending.value = true;
|
|
16
|
-
error.value = null;
|
|
17
|
-
try {
|
|
18
|
-
const result = await client.getSession();
|
|
19
|
-
session.value = result;
|
|
20
|
-
} catch (err) {
|
|
21
|
-
session.value = null;
|
|
22
|
-
error.value = err;
|
|
23
|
-
} finally {
|
|
24
|
-
isLoading.value = false;
|
|
25
|
-
isPending.value = false;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
const signIn = {
|
|
29
|
-
email: async ({ email, password, callbackURL }) => {
|
|
30
|
-
isLoading.value = true;
|
|
31
|
-
try {
|
|
32
|
-
const result = await client.signIn.email({
|
|
33
|
-
email,
|
|
34
|
-
password,
|
|
35
|
-
callbackURL
|
|
36
|
-
});
|
|
37
|
-
if (result.data) {
|
|
38
|
-
session.value = result.data;
|
|
39
|
-
if (callbackURL && typeof window !== "undefined") window.location.href = callbackURL;
|
|
40
|
-
}
|
|
41
|
-
if (result.error) error.value = result.error;
|
|
42
|
-
return result;
|
|
43
|
-
} finally {
|
|
44
|
-
isLoading.value = false;
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
social: (provider, opts) => {
|
|
48
|
-
return client.signIn.social(provider, opts);
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
|
-
const signUp = { email: async ({ email, password, name, callbackURL }) => {
|
|
52
|
-
isLoading.value = true;
|
|
53
|
-
try {
|
|
54
|
-
const result = await client.signUp.email({
|
|
55
|
-
email,
|
|
56
|
-
password,
|
|
57
|
-
name,
|
|
58
|
-
callbackURL
|
|
59
|
-
});
|
|
60
|
-
if (result.data) {
|
|
61
|
-
session.value = result.data;
|
|
62
|
-
if (callbackURL && typeof window !== "undefined") window.location.href = callbackURL;
|
|
63
|
-
}
|
|
64
|
-
if (result.error) error.value = result.error;
|
|
65
|
-
return result;
|
|
66
|
-
} finally {
|
|
67
|
-
isLoading.value = false;
|
|
68
|
-
}
|
|
69
|
-
} };
|
|
70
|
-
async function signOut() {
|
|
71
|
-
isLoading.value = true;
|
|
72
|
-
try {
|
|
73
|
-
const result = await client.signOut();
|
|
74
|
-
session.value = null;
|
|
75
|
-
return result;
|
|
76
|
-
} finally {
|
|
77
|
-
isLoading.value = false;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
async function refreshSession() {
|
|
81
|
-
await fetchSession();
|
|
82
|
-
}
|
|
83
|
-
onMounted(() => {
|
|
84
|
-
fetchSession();
|
|
85
|
-
if (typeof window !== "undefined") {
|
|
86
|
-
window.addEventListener("focus", fetchSession);
|
|
87
|
-
document.addEventListener("visibilitychange", () => {
|
|
88
|
-
if (document.visibilityState === "visible") fetchSession();
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
return {
|
|
93
|
-
session,
|
|
94
|
-
user,
|
|
95
|
-
isLoading,
|
|
96
|
-
isPending,
|
|
97
|
-
isAuthenticated,
|
|
98
|
-
error,
|
|
99
|
-
signIn,
|
|
100
|
-
signUp,
|
|
101
|
-
signOut,
|
|
102
|
-
getSession: client.getSession,
|
|
103
|
-
updateUser: client.updateUser,
|
|
104
|
-
refreshSession
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
function useSession(basePath = "/api/auth") {
|
|
108
|
-
const data = ref(null);
|
|
109
|
-
const isPending = ref(true);
|
|
110
|
-
const error = ref(null);
|
|
111
|
-
const client = createAuthClient(basePath);
|
|
112
|
-
const refetch = async () => {
|
|
113
|
-
isPending.value = true;
|
|
114
|
-
error.value = null;
|
|
115
|
-
try {
|
|
116
|
-
data.value = await client.getSession();
|
|
117
|
-
} catch (err) {
|
|
118
|
-
data.value = null;
|
|
119
|
-
error.value = err;
|
|
120
|
-
} finally {
|
|
121
|
-
isPending.value = false;
|
|
122
|
-
}
|
|
123
|
-
};
|
|
124
|
-
onMounted(refetch);
|
|
125
|
-
if (typeof window !== "undefined") {
|
|
126
|
-
window.addEventListener("focus", refetch);
|
|
127
|
-
document.addEventListener("visibilitychange", () => {
|
|
128
|
-
if (document.visibilityState === "visible") refetch();
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
return {
|
|
132
|
-
data,
|
|
133
|
-
isPending,
|
|
134
|
-
error,
|
|
135
|
-
refetch
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
function getSessionFromHeaders(headers, basePath = "/api/auth") {
|
|
139
|
-
return fetch(`${basePath}/get-session`, {
|
|
140
|
-
headers: { cookie: headers.get("cookie") || "" },
|
|
141
|
-
credentials: "include"
|
|
142
|
-
}).then((r) => r.json()).then((data) => {
|
|
143
|
-
if (data?.session && data?.user) return data;
|
|
144
|
-
return null;
|
|
145
|
-
}).catch(() => null);
|
|
146
|
-
}
|
|
147
|
-
function protectRoute(redirectTo = "/login") {
|
|
148
|
-
const auth = useAuth();
|
|
149
|
-
if (typeof window !== "undefined" && !auth.isPending.value && !auth.isAuthenticated.value) {
|
|
150
|
-
const redirectUrl = `${redirectTo}?redirect=${encodeURIComponent(window.location.pathname)}`;
|
|
151
|
-
window.location.href = redirectUrl;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
//#endregion
|
|
1
|
+
import { a as createAuthClient, i as useSession, n as protectRoute, r as useAuth, t as getSessionFromHeaders } from "./runtime-Dbttti-m.js";
|
|
155
2
|
export { createAuthClient, getSessionFromHeaders, protectRoute, useAuth, useSession };
|
package/dist/vite.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { resolveAuthOptions } from "./core.js";
|
|
2
|
-
import {
|
|
2
|
+
import { getLogger } from "@ubean/logger";
|
|
3
3
|
//#region src/vite.ts
|
|
4
4
|
const VIRTUAL_AUTH_CLIENT_ID = "virtual:ubean-auth/client";
|
|
5
5
|
const RESOLVED_VIRTUAL_AUTH_CLIENT_ID = `\0${VIRTUAL_AUTH_CLIENT_ID}`;
|
|
6
6
|
const VIRTUAL_AUTH_SERVER_ID = "virtual:ubean-auth/server";
|
|
7
7
|
const RESOLVED_VIRTUAL_AUTH_SERVER_ID = `\0${VIRTUAL_AUTH_SERVER_ID}`;
|
|
8
|
+
const logger = getLogger("auth");
|
|
8
9
|
function ubeanAuthPlugin(userOptions = {}) {
|
|
9
10
|
const options = resolveAuthOptions(userOptions);
|
|
10
11
|
let authHandlerInstance = null;
|
|
@@ -47,12 +48,12 @@ function ubeanAuthPlugin(userOptions = {}) {
|
|
|
47
48
|
const text = await response.text();
|
|
48
49
|
res.end(text);
|
|
49
50
|
}).catch((error) => {
|
|
50
|
-
|
|
51
|
+
logger.error("[auth] Error handling auth request:", error);
|
|
51
52
|
res.statusCode = 500;
|
|
52
53
|
res.end(JSON.stringify({ error: "Internal auth error" }));
|
|
53
54
|
});
|
|
54
55
|
});
|
|
55
|
-
|
|
56
|
+
logger.info(`[auth] Better Auth routes mounted at ${options.basePath}/*`);
|
|
56
57
|
}
|
|
57
58
|
};
|
|
58
59
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ubean/auth",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Better Auth integration for ubean framework",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist"
|
|
@@ -28,17 +28,17 @@
|
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"better-auth": "^1.6.
|
|
32
|
-
"
|
|
33
|
-
"
|
|
31
|
+
"better-auth": "^1.6.29",
|
|
32
|
+
"defu": "6.1.7",
|
|
33
|
+
"@ubean/logger": "0.2.0"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@types/node": "^26.
|
|
37
|
-
"hono": "4.
|
|
36
|
+
"@types/node": "^26.2.0",
|
|
37
|
+
"hono": "4.13.2",
|
|
38
38
|
"typescript": "7.0.2",
|
|
39
|
-
"vite": "npm:@voidzero-dev/vite-plus-core@0.2.
|
|
40
|
-
"vite-plus": "0.2.
|
|
41
|
-
"vue": "^3.5.
|
|
39
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@0.2.9",
|
|
40
|
+
"vite-plus": "0.2.9",
|
|
41
|
+
"vue": "^3.5.41"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"hono": "^4.0.0",
|