-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(web): Generate new license format
- Loading branch information
Showing
3 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |