79 lines
2 KiB
TypeScript
79 lines
2 KiB
TypeScript
import { ref } from 'vue'
|
|
import { uniqueNamesGenerator, NumberDictionary, adjectives, animals } from 'unique-names-generator'
|
|
|
|
export interface GeneratedCredentials {
|
|
username: string
|
|
password: string
|
|
email: string
|
|
}
|
|
|
|
export function useDemoAccountGenerator() {
|
|
const isLoading = ref(false)
|
|
const error = ref('')
|
|
const generatedCredentials = ref<GeneratedCredentials | null>(null)
|
|
|
|
// Generate a random password
|
|
function generateRandomPassword(): string {
|
|
const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*'
|
|
let password = ''
|
|
for (let i = 0; i < 12; i++) {
|
|
password += chars.charAt(Math.floor(Math.random() * chars.length))
|
|
}
|
|
return password
|
|
}
|
|
|
|
// Generate unique username < 20 characters long
|
|
function generateUsername() {
|
|
const maxLength = 20;
|
|
const numberDictionary = NumberDictionary.generate({ min: 100, max: 999 });
|
|
|
|
while (true) {
|
|
const username = uniqueNamesGenerator({
|
|
dictionaries: [adjectives, animals, numberDictionary],
|
|
separator: '',
|
|
length: 3,
|
|
style: 'capital'
|
|
});
|
|
|
|
if (username.length <= maxLength) {
|
|
return username;
|
|
}
|
|
// otherwise reroll
|
|
}
|
|
}
|
|
|
|
// Generate unique username and random password
|
|
function generateCredentials(): GeneratedCredentials {
|
|
const username = generateUsername()
|
|
|
|
const password = generateRandomPassword()
|
|
const email = `${username}@demo.local`
|
|
return { username, password, email }
|
|
}
|
|
|
|
// Generate new credentials
|
|
function generateNewCredentials(): GeneratedCredentials {
|
|
const credentials = generateCredentials()
|
|
generatedCredentials.value = credentials
|
|
return credentials
|
|
}
|
|
|
|
// Reset state
|
|
function reset() {
|
|
isLoading.value = false
|
|
error.value = ''
|
|
generatedCredentials.value = null
|
|
}
|
|
|
|
return {
|
|
// State
|
|
isLoading,
|
|
error,
|
|
generatedCredentials,
|
|
|
|
// Actions
|
|
generateCredentials,
|
|
generateNewCredentials,
|
|
reset
|
|
}
|
|
}
|