2022-04-04 00:25:29 +02:00
|
|
|
import { shuffleString } from '@/utils/random';
|
|
|
|
|
|
|
|
export function createToken({
|
|
|
|
withUppercase = true,
|
|
|
|
withLowercase = true,
|
|
|
|
withNumbers = true,
|
|
|
|
withSymbols = false,
|
2024-03-03 09:44:53 +01:00
|
|
|
deniedChars = '',
|
2022-04-04 00:25:29 +02:00
|
|
|
length = 64,
|
2022-08-24 00:09:16 +02:00
|
|
|
alphabet,
|
2022-04-04 00:25:29 +02:00
|
|
|
}: {
|
2023-05-28 23:13:24 +02:00
|
|
|
withUppercase?: boolean
|
|
|
|
withLowercase?: boolean
|
|
|
|
withNumbers?: boolean
|
|
|
|
withSymbols?: boolean
|
2024-03-03 09:44:53 +01:00
|
|
|
deniedChars?: string
|
2023-05-28 23:13:24 +02:00
|
|
|
length?: number
|
|
|
|
alphabet?: string
|
2022-04-04 00:25:29 +02:00
|
|
|
}) {
|
2024-08-03 15:03:43 +02:00
|
|
|
const allAlphabet = (alphabet ?? (
|
|
|
|
(withUppercase ? 'ABCDEFGHIJKLMOPQRSTUVWXYZ' : '')
|
|
|
|
+ (withLowercase ? 'abcdefghijklmopqrstuvwxyz' : '')
|
|
|
|
+ (withNumbers ? '0123456789' : '')
|
|
|
|
+ (withSymbols ? '.,;:!?./-"\'#{([-|\\@)]=}*+' : '')
|
|
|
|
)).split('').filter(c => !(deniedChars?.includes(c))).join('');
|
2022-04-04 00:25:29 +02:00
|
|
|
|
2024-09-22 19:03:02 +02:00
|
|
|
const len = length < 1 ? 1 : length;
|
|
|
|
return shuffleString(allAlphabet.repeat(len)).substring(0, len);
|
2022-04-04 00:25:29 +02:00
|
|
|
}
|