Skip to content

Commit

Permalink
feat(web): Generate new license format
Browse files Browse the repository at this point in the history
  • Loading branch information
flemzord committed Apr 29, 2024
1 parent 67b3cb9 commit 1346977
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
35 changes: 35 additions & 0 deletions apps/web/server/api/license/validate.get.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { schema } from '@getlicensed/db';
import { eq } from 'drizzle-orm';

export default defineEventHandler(async (event) => {
const query = getQuery(event);
if (query.license === undefined) {
throw createError({
statusCode: 400,
statusMessage: 'No license provided',
});
}

const getLicense = await useDB()
.select({
name: schema.tokens.name,
token: schema.tokens.token,
createdAt: schema.tokens.createdAt,
updatedAt: schema.tokens.updatedAt,
})
.from(schema.tokens)
.where(eq(schema.tokens.token, query.license as string))
.limit(1)
.execute();

if (getLicense.length === 0) {
throw createError({
statusCode: 404,
statusMessage: 'License not found',
});
}

return {
data: getLicense[0],
};
});
4 changes: 2 additions & 2 deletions apps/web/server/trpc/routers/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { schema } from '@getlicensed/db';
import { and, desc, eq } from 'drizzle-orm';
import { generateRandomString } from 'ts-randomstring/lib';
import { z } from 'zod';
import { protectedProcedure, t } from '~/server/trpc/trpc';
import { generateLicenseKey } from '~/server/utils/license';

const AddShape = z.object({
name: z.string(),
Expand All @@ -26,7 +26,7 @@ export const tokensRouter = t.router({
.values({
name: input.name,
userId: ctx.userId,
token: generateRandomString(),
token: generateLicenseKey,
})
.returning();
}),
Expand Down
11 changes: 11 additions & 0 deletions apps/web/server/utils/license.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const generateLicenseKey: string = `license_${createRandomString(24)}`;

function createRandomString(length: number) {
const chars =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}

0 comments on commit 1346977

Please sign in to comment.