Implement License Key API
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import { error } from '@sveltejs/kit';
|
||||
import type { RequestHandler } from './$types';
|
||||
import { prisma } from '$lib/db';
|
||||
|
||||
// Get the key and email form the URL
|
||||
// First, check if the key even exists, throw error if not
|
||||
// Second, check that the email is attached to a real user, throw error
|
||||
// Third, check that the key retrieved from the database belongs to the user, throw error
|
||||
// Return response
|
||||
export const GET: RequestHandler = async ({ url }) => {
|
||||
const key = url.searchParams.get('key') ?? '';
|
||||
const email = url.searchParams.get('email') ?? '';
|
||||
if (key === '') {
|
||||
throw error(400, 'license key required');
|
||||
}
|
||||
|
||||
const storedKey = await prisma.licenseKey.findFirst({
|
||||
where: {
|
||||
key: key
|
||||
}
|
||||
});
|
||||
|
||||
if (!storedKey) {
|
||||
throw error(400, 'license key not found');
|
||||
}
|
||||
|
||||
const userAccount = await prisma.user.findUniqueOrThrow({ where: { email: email } });
|
||||
|
||||
if (!userAccount) {
|
||||
throw error(400, 'user does not exist');
|
||||
}
|
||||
|
||||
if (storedKey.userId != userAccount.id) {
|
||||
throw error(400, 'key does not belong to specified user');
|
||||
}
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
key: storedKey.key,
|
||||
active: storedKey.active,
|
||||
level: storedKey.level
|
||||
})
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user