mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-26 08:56:40 -05:00
chore: use generate random string helper (#6140)
This commit is contained in:
@@ -4,6 +4,7 @@ import { defineErrorCodes } from "@better-auth/core/utils";
|
||||
import { base64Url } from "@better-auth/utils/base64";
|
||||
import { createHash } from "@better-auth/utils/hash";
|
||||
import { APIError } from "../../api";
|
||||
import { generateRandomString } from "../../crypto/random";
|
||||
import { mergeSchema } from "../../db";
|
||||
import { getDate } from "../../utils/date";
|
||||
import { getIp } from "../../utils/get-request-ip";
|
||||
@@ -119,14 +120,8 @@ export const apiKey = (options?: ApiKeyOptions | undefined) => {
|
||||
const keyGenerator =
|
||||
opts.customKeyGenerator ||
|
||||
(async (options: { length: number; prefix: string | undefined }) => {
|
||||
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||
let apiKey = `${options.prefix || ""}`;
|
||||
for (let i = 0; i < options.length; i++) {
|
||||
const randomIndex = Math.floor(Math.random() * characters.length);
|
||||
apiKey += characters[randomIndex];
|
||||
}
|
||||
|
||||
return apiKey;
|
||||
const key = generateRandomString(options.length, "a-z", "A-Z");
|
||||
return `${options.prefix || ""}${key}`;
|
||||
});
|
||||
|
||||
const routes = createApiKeyRoutes({ keyGenerator, opts, schema });
|
||||
|
||||
Reference in New Issue
Block a user