@witchcraft/nuxt-auth 0.5.2 → 0.6.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/README.md +188 -336
- package/dist/module.d.mts +64 -41
- package/dist/module.json +1 -1
- package/dist/module.mjs +25 -35
- package/dist/runtime/components/ExternalCallback.vue +11 -11
- package/dist/runtime/components/LoginProviderButtons.client.d.vue.ts +10 -15
- package/dist/runtime/components/LoginProviderButtons.client.vue +4 -10
- package/dist/runtime/components/LoginProviderButtons.client.vue.d.ts +10 -15
- package/dist/runtime/components/MockBypassRegistrationFields.d.vue.ts +13 -0
- package/dist/runtime/components/MockBypassRegistrationFields.vue +18 -0
- package/dist/runtime/components/MockBypassRegistrationFields.vue.d.ts +13 -0
- package/dist/runtime/components/Mocker.d.vue.ts +5 -1
- package/dist/runtime/components/Mocker.vue +7 -10
- package/dist/runtime/components/Mocker.vue.d.ts +5 -1
- package/dist/runtime/components/SessionStatus.d.vue.ts +86 -42
- package/dist/runtime/components/SessionStatus.vue +29 -8
- package/dist/runtime/components/SessionStatus.vue.d.ts +86 -42
- package/dist/runtime/components/UserRegistration.d.vue.ts +5 -0
- package/dist/runtime/components/UserRegistration.vue +2 -0
- package/dist/runtime/components/UserRegistration.vue.d.ts +5 -0
- package/dist/runtime/composables/useAuth.d.ts +382 -19
- package/dist/runtime/composables/useAuth.js +191 -28
- package/dist/runtime/composables/useAuthExternalCallback.d.ts +13 -6
- package/dist/runtime/composables/useAuthExternalCallback.js +27 -16
- package/dist/runtime/composables/useAuthExternalCode.d.ts +2 -2
- package/dist/runtime/composables/useAuthExternalCode.js +19 -18
- package/dist/runtime/composables/useAuthMocker.d.ts +11 -1
- package/dist/runtime/composables/useAuthMocker.js +21 -11
- package/dist/runtime/composables/useAuthUserRegistration.d.ts +5 -10
- package/dist/runtime/composables/useAuthUserRegistration.js +12 -25
- package/dist/runtime/core/providerStyles.js +12 -4
- package/dist/runtime/electron/index.d.ts +1 -0
- package/dist/runtime/electron/index.js +1 -0
- package/dist/runtime/electron/main/ElectronAuth.d.ts +123 -0
- package/dist/runtime/electron/main/ElectronAuth.js +200 -0
- package/dist/runtime/electron/main/index.d.ts +2 -0
- package/dist/runtime/electron/main/index.js +2 -0
- package/dist/runtime/electron/main/setupElectronAuthMain.d.ts +665 -0
- package/dist/runtime/electron/main/setupElectronAuthMain.js +137 -0
- package/dist/runtime/electron/preload/createElectronAuthApi.d.ts +7 -0
- package/dist/runtime/electron/preload/createElectronAuthApi.js +15 -0
- package/dist/runtime/electron/preload/index.d.ts +2 -0
- package/dist/runtime/electron/preload/index.js +2 -0
- package/dist/runtime/electron/types.d.ts +54 -0
- package/dist/runtime/electron/types.js +2 -0
- package/dist/runtime/middleware/authGlobal.js +30 -14
- package/dist/runtime/middleware/authProtected.js +2 -2
- package/dist/runtime/plugins/client/electronNuxtAuthPlugin.d.ts +48 -0
- package/dist/runtime/plugins/client/electronNuxtAuthPlugin.js +86 -0
- package/dist/runtime/plugins/client/localOnlyUsers.d.ts +25 -0
- package/dist/runtime/plugins/client/localOnlyUsers.js +110 -0
- package/dist/runtime/plugins/server/devMock.d.ts +51 -0
- package/dist/runtime/plugins/server/devMock.js +162 -0
- package/dist/runtime/server/utils/assertEventWithAuthorizedUser.d.ts +11 -0
- package/dist/runtime/server/utils/assertEventWithAuthorizedUser.js +10 -0
- package/dist/runtime/server/utils/createAuthMiddleware.d.ts +4 -4
- package/dist/runtime/server/utils/createAuthMiddleware.js +16 -23
- package/dist/runtime/server/utils/createAuthSchema.d.ts +391 -94
- package/dist/runtime/server/utils/createAuthSchema.js +43 -13
- package/dist/runtime/server/utils/createBetterAuth.d.ts +34 -0
- package/dist/runtime/server/utils/createBetterAuth.js +160 -0
- package/dist/runtime/server/utils/getBetterAuthInstance.d.ts +3 -0
- package/dist/runtime/server/utils/getBetterAuthInstance.js +10 -0
- package/dist/runtime/types.d.ts +358 -248
- package/dist/runtime/types.js +48 -25
- package/dist/runtime/utils/deserializeAuthLocalUser.d.ts +2 -0
- package/dist/runtime/utils/deserializeAuthLocalUser.js +9 -0
- package/package.json +44 -19
- package/dist/runtime/core/providers/github.d.ts +0 -84
- package/dist/runtime/core/providers/github.js +0 -55
- package/dist/runtime/core/providers/google.d.ts +0 -65
- package/dist/runtime/core/providers/google.js +0 -49
- package/dist/runtime/server/helpers/getSafeSecretsInfo.d.ts +0 -4
- package/dist/runtime/server/helpers/getSafeSecretsInfo.js +0 -10
- package/dist/runtime/server/helpers/logSafeRoute.d.ts +0 -1
- package/dist/runtime/server/helpers/logSafeRoute.js +0 -3
- package/dist/runtime/server/helpers/signJwt.d.ts +0 -3
- package/dist/runtime/server/helpers/signJwt.js +0 -16
- package/dist/runtime/server/helpers/verifyJwt.d.ts +0 -5
- package/dist/runtime/server/helpers/verifyJwt.js +0 -19
- package/dist/runtime/server/utils/Auth.d.ts +0 -85
- package/dist/runtime/server/utils/Auth.js +0 -671
- package/dist/runtime/server/utils/SessionManager.d.ts +0 -26
- package/dist/runtime/server/utils/SessionManager.js +0 -94
- package/dist/runtime/server/utils/createAuthHandler.d.ts +0 -9
- package/dist/runtime/server/utils/createAuthHandler.js +0 -29
- package/dist/runtime/utils/createExternalAuthLoginHandler.d.ts +0 -37
- package/dist/runtime/utils/createExternalAuthLoginHandler.js +0 -23
- package/dist/runtime/utils/getAuthApiRoute.d.ts +0 -6
- package/dist/runtime/utils/getAuthApiRoute.js +0 -14
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { APIError, createAuthEndpoint, createAuthMiddleware } from "better-auth/api";
|
|
2
|
+
import { setSessionCookie } from "better-auth/cookies";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
function derivedUsername(id, maxUsernameLength) {
|
|
5
|
+
return `username_${id.replace(/\W/g, "").slice(0, maxUsernameLength - "username_".length)}`;
|
|
6
|
+
}
|
|
7
|
+
export function devMock({
|
|
8
|
+
generateUser,
|
|
9
|
+
authRoutes,
|
|
10
|
+
enabledProviders,
|
|
11
|
+
usernameValidator,
|
|
12
|
+
maxUsernameLength = 32,
|
|
13
|
+
transferCodeTtlMs = 1e3 * 60 * 5
|
|
14
|
+
}) {
|
|
15
|
+
function assertConfiguredProvider(provider) {
|
|
16
|
+
if (provider && enabledProviders.includes(provider)) return;
|
|
17
|
+
throw new APIError("BAD_REQUEST", {
|
|
18
|
+
message: `Unknown provider "${provider ?? ""}", the mock only serves the configured ones: ${enabledProviders.join(", ")}.`
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
async function resolveUser(ctx, {
|
|
22
|
+
username,
|
|
23
|
+
id,
|
|
24
|
+
devBypassRegistration
|
|
25
|
+
}) {
|
|
26
|
+
username ||= void 0;
|
|
27
|
+
if (username && usernameValidator && !await usernameValidator(username)) {
|
|
28
|
+
throw new APIError("BAD_REQUEST", {
|
|
29
|
+
message: `"${username}" is not a valid username for this app.`
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
if (!devBypassRegistration && username) {
|
|
33
|
+
throw new APIError("BAD_REQUEST", {
|
|
34
|
+
message: "A request that does not bypass registration cannot carry a username, the user picks one when they register."
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
const user = id ? await ctx.context.adapter.findOne({ model: "user", where: [{ field: "id", value: id }] }) : username ? await ctx.context.adapter.findOne({ model: "user", where: [{ field: "username", value: username }] }) : void 0;
|
|
38
|
+
if (user) {
|
|
39
|
+
if (devBypassRegistration) {
|
|
40
|
+
throw new APIError("BAD_REQUEST", {
|
|
41
|
+
message: "Registration can only be bypassed while the user is created."
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
return user;
|
|
45
|
+
}
|
|
46
|
+
const generated = generateUser?.();
|
|
47
|
+
const resolvedId = id ?? generated?.id ?? crypto.randomUUID();
|
|
48
|
+
const resolvedUsername = devBypassRegistration ? username ?? generated?.username ?? derivedUsername(resolvedId, maxUsernameLength) : void 0;
|
|
49
|
+
return await ctx.context.internalAdapter.createUser({
|
|
50
|
+
...generated,
|
|
51
|
+
id: resolvedId,
|
|
52
|
+
email: generated?.email ?? `${resolvedUsername ?? resolvedId}@dev.local`,
|
|
53
|
+
// a registered user is named after their username, an unregistered one has only an id
|
|
54
|
+
name: resolvedUsername ?? generated?.name ?? resolvedId,
|
|
55
|
+
username: resolvedUsername,
|
|
56
|
+
image: generated?.image ?? null,
|
|
57
|
+
emailVerified: true,
|
|
58
|
+
isRegistered: devBypassRegistration
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
async function transferToExternal(ctx, user, {
|
|
62
|
+
codeChallenge,
|
|
63
|
+
state,
|
|
64
|
+
verificationPrefix,
|
|
65
|
+
deeplinkScheme
|
|
66
|
+
}) {
|
|
67
|
+
const identifier = crypto.randomUUID();
|
|
68
|
+
await ctx.context.internalAdapter.createVerificationValue({
|
|
69
|
+
identifier: `${verificationPrefix}:${identifier}`,
|
|
70
|
+
value: JSON.stringify({ userId: user.id, codeChallenge, state }),
|
|
71
|
+
expiresAt: new Date(Date.now() + transferCodeTtlMs)
|
|
72
|
+
});
|
|
73
|
+
const token = Buffer.from(JSON.stringify({ identifier, state })).toString("base64url");
|
|
74
|
+
return `${deeplinkScheme}://${authRoutes.deeplink.replace(/^\//, "")}#token=${token}`;
|
|
75
|
+
}
|
|
76
|
+
function redirectFor(user, devBypassRegistration) {
|
|
77
|
+
return !user.isRegistered && !devBypassRegistration ? authRoutes.register : authRoutes.postRegisteredLogin;
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
id: "dev-mock",
|
|
81
|
+
hooks: {
|
|
82
|
+
before: [{
|
|
83
|
+
matcher: (context) => {
|
|
84
|
+
return typeof context?.path === "string" && context.path.startsWith("/callback/") && context?.query?.devBypass === "true";
|
|
85
|
+
},
|
|
86
|
+
handler: createAuthMiddleware(async (ctx) => {
|
|
87
|
+
assertConfiguredProvider(ctx.params?.id);
|
|
88
|
+
const query = ctx.query ?? {};
|
|
89
|
+
const id = query.id;
|
|
90
|
+
const devBypassRegistration = query.devBypassRegistration === "true";
|
|
91
|
+
const user = await resolveUser(ctx, {
|
|
92
|
+
username: query.username,
|
|
93
|
+
id,
|
|
94
|
+
devBypassRegistration
|
|
95
|
+
});
|
|
96
|
+
const session = await ctx.context.internalAdapter.createSession(user.id, ctx.request);
|
|
97
|
+
await setSessionCookie(ctx, { session, user }, false);
|
|
98
|
+
return ctx.redirect(redirectFor(user, devBypassRegistration));
|
|
99
|
+
})
|
|
100
|
+
}]
|
|
101
|
+
},
|
|
102
|
+
endpoints: {
|
|
103
|
+
devMock: createAuthEndpoint("/dev/mock", {
|
|
104
|
+
method: "POST",
|
|
105
|
+
body: z.object({
|
|
106
|
+
username: z.string().optional(),
|
|
107
|
+
id: z.string().optional(),
|
|
108
|
+
provider: z.string(),
|
|
109
|
+
code_challenge: z.string().optional(),
|
|
110
|
+
state: z.string().optional(),
|
|
111
|
+
verification_prefix: z.string().optional(),
|
|
112
|
+
deeplink_scheme: z.string().optional(),
|
|
113
|
+
devBypassRegistration: z.boolean().optional()
|
|
114
|
+
})
|
|
115
|
+
}, async (ctx) => {
|
|
116
|
+
assertConfiguredProvider(ctx.body.provider);
|
|
117
|
+
const {
|
|
118
|
+
code_challenge: codeChallenge,
|
|
119
|
+
state,
|
|
120
|
+
verification_prefix: verificationPrefix,
|
|
121
|
+
deeplink_scheme: deeplinkScheme,
|
|
122
|
+
devBypassRegistration
|
|
123
|
+
} = ctx.body;
|
|
124
|
+
const id = ctx.body.id;
|
|
125
|
+
const user = await resolveUser(ctx, {
|
|
126
|
+
username: ctx.body.username,
|
|
127
|
+
id,
|
|
128
|
+
devBypassRegistration: devBypassRegistration ?? false
|
|
129
|
+
});
|
|
130
|
+
if (codeChallenge && state) {
|
|
131
|
+
if (!deeplinkScheme) throw new Error("Mock transfer needs a deeplink_scheme.");
|
|
132
|
+
const url = await transferToExternal(ctx, user, { codeChallenge, state, verificationPrefix: verificationPrefix ?? "external", deeplinkScheme });
|
|
133
|
+
return ctx.json({ redirectUrl: url });
|
|
134
|
+
}
|
|
135
|
+
const session = await ctx.context.internalAdapter.createSession(user.id, ctx.request);
|
|
136
|
+
await setSessionCookie(ctx, { session, user }, false);
|
|
137
|
+
return ctx.json({ redirectUrl: redirectFor(user, devBypassRegistration ?? false) });
|
|
138
|
+
}),
|
|
139
|
+
/**
|
|
140
|
+
* Moves the session row's expiry back, so a test can run a short session without
|
|
141
|
+
* waiting for a real one.
|
|
142
|
+
*
|
|
143
|
+
* The token has to arrive as a `Cookie` header (`headers: { cookie: "name=value" }`),
|
|
144
|
+
* because `getSignedCookie` reads the request's parsed cookies and a signed value in a
|
|
145
|
+
* body cannot be verified by it. Without the header it finds no cookie and updates
|
|
146
|
+
* nothing.
|
|
147
|
+
*/
|
|
148
|
+
devMockSession: createAuthEndpoint("/dev/mock/session", {
|
|
149
|
+
method: "POST",
|
|
150
|
+
body: z.object({
|
|
151
|
+
/** Epoch ms, the only unit that crosses the wire. `Session.expiresAt` is a date. */
|
|
152
|
+
expiresAt: z.number()
|
|
153
|
+
})
|
|
154
|
+
}, async (ctx) => {
|
|
155
|
+
const token = await ctx.getSignedCookie(ctx.context.authCookies.sessionToken.name, ctx.context.secret);
|
|
156
|
+
if (!token) return ctx.json({ updated: false });
|
|
157
|
+
await ctx.context.internalAdapter.updateSession(token, { expiresAt: new Date(ctx.body.expiresAt) });
|
|
158
|
+
return ctx.json({ updated: true });
|
|
159
|
+
})
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type H3Event } from "h3";
|
|
2
|
+
import type { AuthUser } from "../../types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Throws a 401 when the event has no user and narrows the event type past
|
|
5
|
+
* the check. Pair with `createAuthMiddleware`, which fills `context.user`.
|
|
6
|
+
*/
|
|
7
|
+
export declare function assertEventWithAuthorizedUser(event: H3Event): asserts event is H3Event & {
|
|
8
|
+
context: {
|
|
9
|
+
user: AuthUser;
|
|
10
|
+
};
|
|
11
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
export declare function createAuthMiddleware(
|
|
1
|
+
import type { Auth } from "better-auth";
|
|
2
|
+
import type { AuthSession } from "./createAuthSchema.js";
|
|
3
|
+
import type { AuthUser } from "../../types.js";
|
|
4
|
+
export declare function createAuthMiddleware(auth: Auth<any>, logger: any): import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<void>>;
|
|
5
5
|
declare module "h3" {
|
|
6
6
|
interface H3EventContext {
|
|
7
7
|
user: AuthUser | null;
|
|
@@ -1,28 +1,21 @@
|
|
|
1
|
-
import { defineEventHandler
|
|
2
|
-
export function createAuthMiddleware(
|
|
1
|
+
import { defineEventHandler } from "h3";
|
|
2
|
+
export function createAuthMiddleware(auth, logger) {
|
|
3
3
|
return defineEventHandler(async (event) => {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
try {
|
|
5
|
+
const session = await auth.api.getSession({
|
|
6
|
+
headers: event.headers
|
|
7
|
+
});
|
|
8
|
+
if (session) {
|
|
9
|
+
event.context.user = session.user;
|
|
10
|
+
event.context.session = session.session;
|
|
11
|
+
} else {
|
|
12
|
+
event.context.user = null;
|
|
13
|
+
event.context.session = null;
|
|
14
|
+
}
|
|
15
|
+
} catch (e) {
|
|
16
|
+
logger?.error?.({ ns: "auth:middleware", error: e });
|
|
7
17
|
event.context.user = null;
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
const { session, user, fresh } = await sessionManager.validateSessionToken(sessionToken);
|
|
11
|
-
logger.debug({
|
|
12
|
-
ns: "auth:middleware:session",
|
|
13
|
-
user,
|
|
14
|
-
fresh,
|
|
15
|
-
redact: { sessionToken, session }
|
|
16
|
-
});
|
|
17
|
-
if (fresh && session) {
|
|
18
|
-
const cookie = sessionManager.createSessionCookie(sessionToken);
|
|
19
|
-
setCookie(event, cookie.name, cookie.value, cookie.attributes);
|
|
20
|
-
}
|
|
21
|
-
if (!session) {
|
|
22
|
-
const cookie = sessionManager.createBlankSessionCookie();
|
|
23
|
-
setCookie(event, cookie.name, cookie.value, cookie.attributes);
|
|
18
|
+
event.context.session = null;
|
|
24
19
|
}
|
|
25
|
-
event.context.session = session;
|
|
26
|
-
event.context.user = user;
|
|
27
20
|
});
|
|
28
21
|
}
|