fix: core schema model name definition on api-key

This commit is contained in:
KinfeMichael Tariku
2025-05-04 06:29:13 -07:00
committed by GitHub
parent e75c67accc
commit f876b3ffa2
9 changed files with 23 additions and 21 deletions

View File

@@ -42,6 +42,8 @@ export const ERROR_CODES = {
"The property you're trying to set can only be set from the server auth instance only.",
};
export const API_KEY_TABLE_NAME = "apikey";
export const apiKey = (options?: ApiKeyOptions) => {
const opts = {
...options,
@@ -156,7 +158,7 @@ export const apiKey = (options?: ApiKeyOptions) => {
});
const apiKey = await ctx.context.adapter.findOne<ApiKey>({
model: schema.apikey.modelName,
model: API_KEY_TABLE_NAME,
where: [
{
field: "key",
@@ -226,6 +228,6 @@ export const apiKey = (options?: ApiKeyOptions) => {
deleteApiKey: routes.deleteApiKey,
listApiKeys: routes.listApiKeys,
},
schema: schema,
schema,
} satisfies BetterAuthPlugin;
};

View File

@@ -1,6 +1,6 @@
import { z } from "zod";
import { APIError, createAuthEndpoint, getSessionFromCtx } from "../../../api";
import { ERROR_CODES } from "..";
import { API_KEY_TABLE_NAME, ERROR_CODES } from "..";
import { getDate } from "../../../utils/date";
import { apiKeySchema } from "../schema";
import type { ApiKey } from "../types";
@@ -423,7 +423,7 @@ export function createApiKey({
Omit<ApiKey, "id">,
ApiKey
>({
model: schema.apikey.modelName,
model: API_KEY_TABLE_NAME,
data: data,
});

View File

@@ -5,7 +5,7 @@ import type { apiKeySchema } from "../schema";
import type { ApiKey } from "../types";
import type { AuthContext } from "../../../types";
import type { PredefinedApiKeyOptions } from ".";
import { API_KEY_TABLE_NAME } from "..";
export function deleteApiKey({
opts,
schema,
@@ -79,7 +79,7 @@ export function deleteApiKey({
});
}
const apiKey = await ctx.context.adapter.findOne<ApiKey>({
model: schema.apikey.modelName,
model: API_KEY_TABLE_NAME,
where: [
{
field: "id",
@@ -96,7 +96,7 @@ export function deleteApiKey({
try {
await ctx.context.adapter.delete<ApiKey>({
model: schema.apikey.modelName,
model: API_KEY_TABLE_NAME,
where: [
{
field: "id",

View File

@@ -1,6 +1,6 @@
import { z } from "zod";
import { APIError, createAuthEndpoint, sessionMiddleware } from "../../../api";
import { ERROR_CODES } from "..";
import { API_KEY_TABLE_NAME, ERROR_CODES } from "..";
import type { apiKeySchema } from "../schema";
import type { ApiKey } from "../types";
import type { AuthContext } from "../../../types";
@@ -172,7 +172,7 @@ export function getApiKey({
const session = ctx.context.session;
let apiKey = await ctx.context.adapter.findOne<ApiKey>({
model: schema.apikey.modelName,
model: API_KEY_TABLE_NAME,
where: [
{
field: "id",

View File

@@ -8,6 +8,7 @@ import { updateApiKey } from "./update-api-key";
import { verifyApiKey } from "./verify-api-key";
import { listApiKeys } from "./list-api-keys";
import { deleteAllExpiredApiKeysEndpoint } from "./delete-all-expired-api-keys";
import { API_KEY_TABLE_NAME } from "..";
export type PredefinedApiKeyOptions = ApiKeyOptions &
Required<
@@ -59,7 +60,7 @@ export function createApiKeyRoutes({
lastChecked = new Date();
try {
return ctx.adapter.deleteMany({
model: schema.apikey.modelName,
model: API_KEY_TABLE_NAME,
where: [
{
field: "expiresAt" satisfies keyof ApiKey,

View File

@@ -4,7 +4,7 @@ import type { ApiKey } from "../types";
import type { AuthContext } from "../../../types";
import type { PredefinedApiKeyOptions } from ".";
import { safeJSONParse } from "../../../utils/json";
import { API_KEY_TABLE_NAME } from "..";
export function listApiKeys({
opts,
schema,
@@ -165,7 +165,7 @@ export function listApiKeys({
async (ctx) => {
const session = ctx.context.session;
let apiKeys = await ctx.context.adapter.findMany<ApiKey>({
model: schema.apikey.modelName,
model: API_KEY_TABLE_NAME,
where: [
{
field: "userId",

View File

@@ -7,7 +7,7 @@ import { getDate } from "../../../utils/date";
import type { AuthContext } from "../../../types";
import type { PredefinedApiKeyOptions } from ".";
import { safeJSONParse } from "../../../utils/json";
import { API_KEY_TABLE_NAME } from "..";
export function updateApiKey({
opts,
schema,
@@ -272,7 +272,7 @@ export function updateApiKey({
}
const apiKey = await ctx.context.adapter.findOne<ApiKey>({
model: schema.apikey.modelName,
model: API_KEY_TABLE_NAME,
where: [
{
field: "id",
@@ -383,7 +383,7 @@ export function updateApiKey({
let newApiKey: ApiKey = apiKey;
try {
let result = await ctx.context.adapter.update<ApiKey>({
model: schema.apikey.modelName,
model: API_KEY_TABLE_NAME,
where: [
{
field: "id",

View File

@@ -1,6 +1,6 @@
import { z } from "zod";
import { createAuthEndpoint } from "../../../api";
import { ERROR_CODES } from "..";
import { API_KEY_TABLE_NAME, ERROR_CODES } from "..";
import type { apiKeySchema } from "../schema";
import type { ApiKey } from "../types";
import { base64Url } from "@better-auth/utils/base64";
@@ -76,7 +76,7 @@ export function verifyApiKey({
});
const apiKey = await ctx.context.adapter.findOne<ApiKey>({
model: schema.apikey.modelName,
model: API_KEY_TABLE_NAME,
where: [
{
field: "key",
@@ -116,7 +116,7 @@ export function verifyApiKey({
if (now > expiresAt) {
try {
ctx.context.adapter.delete({
model: schema.apikey.modelName,
model: API_KEY_TABLE_NAME,
where: [
{
field: "id",
@@ -184,7 +184,7 @@ export function verifyApiKey({
// if there is no more remaining requests, and there is no refill amount, than the key is revoked
try {
ctx.context.adapter.delete({
model: schema.apikey.modelName,
model: API_KEY_TABLE_NAME,
where: [
{
field: "id",
@@ -244,7 +244,7 @@ export function verifyApiKey({
opts,
);
const newApiKey = await ctx.context.adapter.update<ApiKey>({
model: schema.apikey.modelName,
model: API_KEY_TABLE_NAME,
where: [
{
field: "id",

View File

@@ -7,7 +7,6 @@ export const apiKeySchema = ({
}: { timeWindow: number; rateLimitMax: number }) =>
({
apikey: {
modelName: "apikey",
fields: {
/**
* The name of the key.