summaryrefslogtreecommitdiff
path: root/src/account/kid_link_code.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/account/kid_link_code.ts')
-rw-r--r--src/account/kid_link_code.ts23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/account/kid_link_code.ts b/src/account/kid_link_code.ts
new file mode 100644
index 0000000..a9d3c63
--- /dev/null
+++ b/src/account/kid_link_code.ts
@@ -0,0 +1,23 @@
+import { randomInt } from "node:crypto";
+
+const KID_LINK_CODE_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
+
+export const KID_LINK_CODE_TTL_SECONDS = 5 * 60;
+export const KID_LINK_CODE_REGEX = /^[A-Z0-9]{3}-[A-Z0-9]{3}$/;
+
+export function generateKidLinkCode(): string {
+ const rawCode = Array.from({ length: 6 }, () => {
+ const index = randomInt(0, KID_LINK_CODE_CHARACTERS.length);
+ return KID_LINK_CODE_CHARACTERS[index]!;
+ }).join("");
+
+ return `${rawCode.slice(0, 3)}-${rawCode.slice(3)}`;
+}
+
+export function normalizeKidLinkCode(value: string): string {
+ return value.trim().toUpperCase();
+}
+
+export function getKidLinkCodeRedisKey(code: string): string {
+ return `kid-link-code:${code}`;
+}