Add BetterAuth API Key dependency

This commit is contained in:
Louis Lam
2026-06-29 12:16:43 +08:00
parent 6554622f20
commit 0e8437df73
5 changed files with 67 additions and 1 deletions
@@ -0,0 +1,40 @@
/*
* The api key schema from: https://www.better-auth.com/docs/plugins/api-key/reference#schema
* Plugin: @better-auth/api-key (modelName: better_auth_apikey)
*/
exports.up = function (knex) {
return knex.schema.createTable("better_auth_apikey", (t) => {
t.string("id").primary();
t.string("configId").notNullable().defaultTo("default").index();
t.string("name");
t.string("start");
t.string("prefix");
t.string("key").notNullable().index();
t.string("referenceId")
.notNullable()
.index()
.references("id")
.inTable("better_auth_user")
.onDelete("CASCADE")
.onUpdate("CASCADE");
t.integer("refillInterval");
t.integer("refillAmount");
t.timestamp("lastRefillAt");
t.boolean("enabled").defaultTo(true);
t.boolean("rateLimitEnabled").defaultTo(true);
t.bigInteger("rateLimitTimeWindow");
t.integer("rateLimitMax");
t.integer("requestCount").defaultTo(0);
t.integer("remaining");
t.timestamp("lastRequest");
t.timestamp("expiresAt");
t.timestamp("createdAt").notNullable();
t.timestamp("updatedAt").notNullable();
t.text("permissions");
t.text("metadata");
});
};
exports.down = function (knex) {
return knex.schema.dropTableIfExists("better_auth_apikey");
};
+15
View File
@@ -9,6 +9,7 @@
"version": "3.0.0-beta.0",
"license": "MIT",
"dependencies": {
"@better-auth/api-key": "^1.6.11",
"@grpc/grpc-js": "~1.8.22",
"@inquirer/core": "~11.2.1",
"@inquirer/prompts": "~8.5.2",
@@ -323,6 +324,20 @@
"dev": true,
"license": "Apache-2.0"
},
"node_modules/@better-auth/api-key": {
"version": "1.6.11",
"resolved": "https://registry.npmjs.org/@better-auth/api-key/-/api-key-1.6.11.tgz",
"integrity": "sha512-717Bmbs1Y2h0KrPIwrutxI/HwdqKlga1a1OHlL5TrRbN35IxHeY3GilqvyI/92n7RxFGa/AQhIajX0OL+FJkvw==",
"license": "MIT",
"dependencies": {
"zod": "^4.3.6"
},
"peerDependencies": {
"@better-auth/core": "^1.6.11",
"@better-auth/utils": "0.4.0",
"better-auth": "^1.6.11"
}
},
"node_modules/@better-auth/core": {
"version": "1.6.11",
"resolved": "https://registry.npmjs.org/@better-auth/core/-/core-1.6.11.tgz",
+1
View File
@@ -63,6 +63,7 @@
"generate-changelog": "node ./extra/generate-changelog.mjs"
},
"dependencies": {
"@better-auth/api-key": "1.6.11",
"@grpc/grpc-js": "~1.8.22",
"@inquirer/core": "~11.2.1",
"@inquirer/prompts": "~8.5.2",
+9
View File
@@ -9,6 +9,7 @@ import { admin } from "better-auth/plugins";
import { Socket } from "socket.io";
import { haveIBeenPwned } from "better-auth/plugins";
import { twoFactor } from "better-auth/plugins";
import { apiKey } from "@better-auth/api-key";
import { createAuthMiddleware, APIError } from "better-auth/api";
// @ts-ignore
import * as oldAuth from "./auth.js";
@@ -99,6 +100,14 @@ function createAuthInstance() {
},
}),
apiKey({
schema: {
apikey: {
modelName: "better_auth_apikey",
},
},
}),
// It is not suitable for "Disable Auth", because it can not turn on/off after init.
//anonymous(),
],
+2 -1
View File
@@ -1,5 +1,6 @@
import { createAuthClient } from "better-auth/vue";
import { twoFactorClient, usernameClient } from "better-auth/client/plugins";
import { apiKeyClient } from "@better-auth/api-key/client";
import { reconnectSocket } from "./mixins/socket";
export const baseURL =
@@ -9,7 +10,7 @@ export const baseURL =
export const authClient = createAuthClient({
baseURL,
plugins: [usernameClient(), twoFactorClient()],
plugins: [usernameClient(), twoFactorClient(), apiKeyClient()],
});
authClient.signIn;