MCPcopy
hub / github.com/msgbyte/tianji / verifyUserApiKey

Function verifyUserApiKey

src/server/model/user.ts:414–451  ·  view source on GitHub ↗
(apiKey: string)

Source from the content-addressed store, hash-verified

412 * Verify User Api Key
413 */
414export async function verifyUserApiKey(apiKey: string) {
415 const result = await prisma.userApiKey.findUnique({
416 where: {
417 apiKey,
418 },
419 select: {
420 user: true,
421 expiredAt: true,
422 },
423 });
424
425 if (result?.expiredAt && result.expiredAt.valueOf() < Date.now()) {
426 throw new Error('Api Key has been expired.');
427 }
428
429 if (!result) {
430 throw new Error(
431 'Api Key not found, input api key: ' + apiKey.slice(0, 10) + '...'
432 );
433 }
434
435 prisma.userApiKey
436 .update({
437 where: {
438 apiKey,
439 },
440 data: {
441 usage: {
442 increment: 1,
443 },
444 },
445 })
446 .catch((err) => {
447 logger.error('Failed to update API key usage', err);
448 });
449
450 return result.user;
451}

Callers 7

authFunction · 0.85
buildOpenAIModelsHandlerFunction · 0.85
verifyRequestApiKeyFunction · 0.85
trpc.tsFile · 0.85
initSocketioFunction · 0.85

Calls 3

nowMethod · 0.80
catchMethod · 0.80
errorMethod · 0.80

Tested by

no test coverage detected