Add account API key management
This commit is contained in:
1 parent
98b824ce48
commit
f1c5b4f7bb
32 files changed
+1618
-15
No files matched your search
@@ -0,0 +1,54 @@
|
||||
import prisma from '@/lib/prisma';
|
||||
|
||||
export async function createApiKey(data: {
|
||||
id: string;
|
||||
userId: string;
|
||||
name: string;
|
||||
keyHash: string;
|
||||
keyPrefix: string;
|
||||
}) {
|
||||
return prisma.client.apiKey.create({
|
||||
data,
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
keyPrefix: true,
|
||||
createdAt: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function getApiKeyByHash(keyHash: string) {
|
||||
return prisma.client.apiKey.findUnique({
|
||||
where: { keyHash },
|
||||
});
|
||||
}
|
||||
|
||||
export async function getUserApiKeys(userId: string) {
|
||||
return prisma.client.apiKey.findMany({
|
||||
where: { userId },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
keyPrefix: true,
|
||||
lastUsedAt: true,
|
||||
createdAt: true,
|
||||
},
|
||||
orderBy: { createdAt: 'desc' },
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteApiKey(id: string, userId: string) {
|
||||
const { count } = await prisma.client.apiKey.deleteMany({
|
||||
where: { id, userId },
|
||||
});
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
export async function updateApiKeyLastUsed(id: string) {
|
||||
return prisma.client.apiKey.update({
|
||||
where: { id },
|
||||
data: { lastUsedAt: new Date() },
|
||||
});
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
export * from './annotation';
|
||||
export * from './apiKey';
|
||||
export * from './board';
|
||||
export * from './link';
|
||||
export * from './pixel';
|
||||
|
||||
Reference in new issue
Block a user