summaryrefslogtreecommitdiff
path: root/src/account/kid_link_code.ts
blob: a9d3c63cdf46b2db15bfcce1d05f8e2d24f23a57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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}`;
}