mirror of
https://github.com/better-auth/better-auth.git
synced 2026-06-02 12:26:43 -05:00
fix: core schema model name definition on api-key
This commit is contained in:
committed by
GitHub
parent
e75c67accc
commit
f876b3ffa2
@@ -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;
|
||||
};
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -7,7 +7,6 @@ export const apiKeySchema = ({
|
||||
}: { timeWindow: number; rateLimitMax: number }) =>
|
||||
({
|
||||
apikey: {
|
||||
modelName: "apikey",
|
||||
fields: {
|
||||
/**
|
||||
* The name of the key.
|
||||
|
||||
Reference in New Issue
Block a user