summaryrefslogtreecommitdiff
path: root/packages/backend/src/misc/generate-invite-code.ts
blob: 617b27361d1824fb651e03bb40ac1963aa82b5bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import { secureRndstr } from './secure-rndstr.js';

const CHARS = '23456789ABCDEFGHJKLMNPQRSTUVWXYZ'; // [0-9A-Z] w/o [01IO] (32 patterns)

export function generateInviteCode(): string {
	const code = secureRndstr(8, {
		chars: CHARS,
	});

	const uniqueId = [];
	let n = Math.floor(Date.now() / 1000 / 60);
	while (true) {
		uniqueId.push(CHARS[n % CHARS.length]);
		const t = Math.floor(n / CHARS.length);
		if (!t) break;
		n = t;
	}

	return code + uniqueId.reverse().join('');
}