@witchcraft/nuxt-auth 0.5.1 → 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 +189 -337
- 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 +12 -11
- 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,34 @@
|
|
|
1
|
+
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
|
2
|
+
import { type BetterAuthOptions } from "better-auth";
|
|
3
|
+
import type { AuthAccountsTable, AuthSessionsTable, AuthUsersTable, AuthVerificationsTable } from "./createAuthSchema.js";
|
|
4
|
+
export declare function createBetterAuth({ appUrl, db, schema, usernameSchema, minUsernameLength, maxUsernameLength, mockTransferCodeTtlMs, generateUser, betterAuthOptions, logger }: {
|
|
5
|
+
db: any;
|
|
6
|
+
schema: {
|
|
7
|
+
users: AuthUsersTable;
|
|
8
|
+
authAccounts: AuthAccountsTable;
|
|
9
|
+
authSessions: AuthSessionsTable;
|
|
10
|
+
authVerifications: AuthVerificationsTable;
|
|
11
|
+
};
|
|
12
|
+
/** App url, defaults to the runtime config `auth.publicServerUrl`. */
|
|
13
|
+
appUrl?: string;
|
|
14
|
+
/** The username schema, used to validate usernames ON TOP of better-auth's limits. Note that if you change min/max length you *also* need to change minUsernameLength and maxUsernameLength (which are passed on to better-auth) or you will run into problems. */
|
|
15
|
+
usernameSchema: StandardSchemaV1<string, string>;
|
|
16
|
+
/** Shortest allowed username, passed to better-auth. */
|
|
17
|
+
minUsernameLength?: number;
|
|
18
|
+
/** Longest allowed username, passed to better-auth. */
|
|
19
|
+
maxUsernameLength?: number;
|
|
20
|
+
/** How long a dev-mock transfer code stays valid, defaults to 5 minutes. */
|
|
21
|
+
mockTransferCodeTtlMs?: number;
|
|
22
|
+
/**
|
|
23
|
+
* Escape hatch, deep-merged (via defuArrayFn) over the module defaults.
|
|
24
|
+
* Use it to add better-auth plugins, hooks, or extra options.
|
|
25
|
+
* Replacing a block the module sets (e.g. `databaseHooks.user.update.before`)
|
|
26
|
+
* replaces the module behavior for that block. Array values (e.g. `plugins`)
|
|
27
|
+
* concatenate by default, pass a function receiving the defaults to take
|
|
28
|
+
* full control over them.
|
|
29
|
+
*/
|
|
30
|
+
betterAuthOptions?: BetterAuthOptions;
|
|
31
|
+
generateUser?: (() => any);
|
|
32
|
+
/** Optional logger, used for warnings such as providers skipped for missing secrets. */
|
|
33
|
+
logger?: any;
|
|
34
|
+
}): import("better-auth").Auth<BetterAuthOptions>;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { capitalize } from "@alanscodelog/utils/capitalize";
|
|
2
|
+
import { betterAuth } from "better-auth";
|
|
3
|
+
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
|
4
|
+
import { createAuthEndpoint } from "better-auth/api";
|
|
5
|
+
import { username } from "better-auth/plugins";
|
|
6
|
+
import { defuArrayFn } from "defu";
|
|
7
|
+
import { z } from "zod";
|
|
8
|
+
import { registerAuthInstance } from "./getBetterAuthInstance.js";
|
|
9
|
+
import { useRuntimeConfig } from "#imports";
|
|
10
|
+
import { devMock } from "../../plugins/server/devMock.js";
|
|
11
|
+
const MIN_AUTH_SECRET_BYTES = 32;
|
|
12
|
+
export function createBetterAuth({
|
|
13
|
+
appUrl = useRuntimeConfig().public?.auth?.publicServerUrl,
|
|
14
|
+
db,
|
|
15
|
+
schema,
|
|
16
|
+
usernameSchema,
|
|
17
|
+
minUsernameLength,
|
|
18
|
+
maxUsernameLength,
|
|
19
|
+
mockTransferCodeTtlMs,
|
|
20
|
+
generateUser,
|
|
21
|
+
betterAuthOptions,
|
|
22
|
+
logger
|
|
23
|
+
}) {
|
|
24
|
+
const usernameValidator = async (username2) => {
|
|
25
|
+
try {
|
|
26
|
+
return !("issues" in await usernameSchema["~standard"].validate(username2));
|
|
27
|
+
} catch {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
const rc = useRuntimeConfig();
|
|
32
|
+
const authRoutes = rc.public?.auth?.authRoutes;
|
|
33
|
+
const providersFromConfig = {};
|
|
34
|
+
for (const name of rc.public?.auth?.enabledProviders ?? []) {
|
|
35
|
+
const clientId = rc[`auth${capitalize(name)}ClientId`];
|
|
36
|
+
const clientSecret = rc[`auth${capitalize(name)}ClientSecret`];
|
|
37
|
+
if (clientId && clientSecret) {
|
|
38
|
+
providersFromConfig[name] = { clientId, clientSecret };
|
|
39
|
+
} else {
|
|
40
|
+
logger?.warn({
|
|
41
|
+
ns: "auth:init:missingSecrets",
|
|
42
|
+
error: `Missing auth${capitalize(name)}ClientId or auth${capitalize(name)}ClientSecret for ${name}. Disabling.`
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const socialProviders = {};
|
|
47
|
+
for (const [name, cfg] of Object.entries(providersFromConfig)) {
|
|
48
|
+
socialProviders[name] = { clientId: cfg.clientId, clientSecret: cfg.clientSecret };
|
|
49
|
+
}
|
|
50
|
+
const authSecretValue = rc.authSecret || "";
|
|
51
|
+
if (!authSecretValue) {
|
|
52
|
+
throw new Error("FATAL: Missing or empty runtimeConfig.authSecret (i.e. should be set via NUXT_AUTH_SECRET) in production.");
|
|
53
|
+
}
|
|
54
|
+
if (Buffer.byteLength(authSecretValue, "utf8") < MIN_AUTH_SECRET_BYTES) {
|
|
55
|
+
throw new Error(`FATAL: runtimeConfig.authSecret must be at least ${MIN_AUTH_SECRET_BYTES} bytes (e.g. \`openssl rand -base64 32\`) in production.`);
|
|
56
|
+
}
|
|
57
|
+
const plugins = [
|
|
58
|
+
username({
|
|
59
|
+
minUsernameLength,
|
|
60
|
+
maxUsernameLength,
|
|
61
|
+
usernameValidator,
|
|
62
|
+
usernameNormalization: false,
|
|
63
|
+
displayUsername: false,
|
|
64
|
+
immutableUsername: false
|
|
65
|
+
}),
|
|
66
|
+
{
|
|
67
|
+
/**
|
|
68
|
+
* Thin GET wrapper over Better Auth's `POST /is-username-available`.
|
|
69
|
+
*
|
|
70
|
+
* Unlike better-auth returns a boolean to make client side usage easier with the useAsyncValidation composable.
|
|
71
|
+
*
|
|
72
|
+
* Added as an extension of better-auth's handler so it's always under the configured catch-all base path.
|
|
73
|
+
*/
|
|
74
|
+
id: "username-valid",
|
|
75
|
+
endpoints: {
|
|
76
|
+
usernameValid: createAuthEndpoint("/username/valid", {
|
|
77
|
+
method: "GET",
|
|
78
|
+
query: z.object({ username: z.string() })
|
|
79
|
+
}, async (ctx) => {
|
|
80
|
+
try {
|
|
81
|
+
if (!await usernameValidator(ctx.query.username)) return false;
|
|
82
|
+
const existing = await ctx.context.adapter.findOne({
|
|
83
|
+
model: "user",
|
|
84
|
+
where: [{ field: "username", value: ctx.query.username }]
|
|
85
|
+
});
|
|
86
|
+
return !existing;
|
|
87
|
+
} catch {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
})
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
];
|
|
94
|
+
if (rc.public?.auth?.devBypassAuth && import.meta.dev) {
|
|
95
|
+
plugins.push(devMock({
|
|
96
|
+
generateUser,
|
|
97
|
+
authRoutes,
|
|
98
|
+
enabledProviders: rc.public?.auth?.enabledProviders ?? [],
|
|
99
|
+
usernameValidator,
|
|
100
|
+
maxUsernameLength,
|
|
101
|
+
transferCodeTtlMs: mockTransferCodeTtlMs
|
|
102
|
+
}));
|
|
103
|
+
}
|
|
104
|
+
const auth = betterAuth(defuArrayFn(betterAuthOptions ?? {}, {
|
|
105
|
+
database: drizzleAdapter(db, {
|
|
106
|
+
provider: "pg",
|
|
107
|
+
camelCase: true,
|
|
108
|
+
schema: {
|
|
109
|
+
user: schema.users,
|
|
110
|
+
account: schema.authAccounts,
|
|
111
|
+
session: schema.authSessions,
|
|
112
|
+
verification: schema.authVerifications
|
|
113
|
+
}
|
|
114
|
+
}),
|
|
115
|
+
secret: authSecretValue || void 0,
|
|
116
|
+
baseURL: appUrl,
|
|
117
|
+
trustedOrigins: [appUrl],
|
|
118
|
+
logger: logger ? {
|
|
119
|
+
log: (level, message, ...args) => {
|
|
120
|
+
const method = level === "success" ? "info" : level;
|
|
121
|
+
logger[method]?.({ ns: "auth:better-auth", message, args });
|
|
122
|
+
}
|
|
123
|
+
} : void 0,
|
|
124
|
+
advanced: {
|
|
125
|
+
cookiePrefix: rc.public.auth.cookiePrefix,
|
|
126
|
+
// better-auth skips this check whenever it detects a test run, which hides
|
|
127
|
+
// rejected origins in tests, so it is forced on here
|
|
128
|
+
disableOriginCheck: false,
|
|
129
|
+
database: { generateId: () => crypto.randomUUID() },
|
|
130
|
+
cookies: {
|
|
131
|
+
session_token: {
|
|
132
|
+
attributes: { ...rc.public?.auth?.sessionCookieOpts ?? {} }
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
session: { expiresIn: rc.public.auth.sessionExpiresAt / 1e3 },
|
|
137
|
+
socialProviders,
|
|
138
|
+
emailAndPassword: { enabled: false },
|
|
139
|
+
user: {
|
|
140
|
+
additionalFields: {
|
|
141
|
+
isRegistered: { type: "boolean", defaultValue: false, input: false, returned: true }
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
databaseHooks: {
|
|
145
|
+
user: {
|
|
146
|
+
update: {
|
|
147
|
+
before: async (user) => {
|
|
148
|
+
if (user && "username" in user && user.username) {
|
|
149
|
+
return { data: { ...user, isRegistered: true } };
|
|
150
|
+
}
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
plugins
|
|
157
|
+
}));
|
|
158
|
+
registerAuthInstance(auth);
|
|
159
|
+
return auth;
|
|
160
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
let currentAuth;
|
|
2
|
+
export function registerAuthInstance(auth) {
|
|
3
|
+
currentAuth = auth;
|
|
4
|
+
}
|
|
5
|
+
export function useAuthInstance() {
|
|
6
|
+
if (!currentAuth) {
|
|
7
|
+
throw new Error("Auth instance is not registered. Call createBetterAuth (e.g. by importing server/auth) before handling requests.");
|
|
8
|
+
}
|
|
9
|
+
return currentAuth;
|
|
10
|
+
}
|