[offers][chore] create random string generator for token

This commit is contained in:
Stuart Long Chay Boon
2022-10-24 21:39:49 +08:00
parent 3c80296253
commit c188405de0
2 changed files with 16 additions and 5 deletions

View File

@@ -10,7 +10,7 @@ import {
} from '~/mappers/offers-mappers';
import { baseCurrencyString } from '~/utils/offers/currency';
import { convert } from '~/utils/offers/currency/currencyExchange';
import generateRandomName from '~/utils/offers/randomNameGenerator';
import { generateRandomName, generateRandomStringForToken } from '~/utils/offers/randomGenerator';
import { createValidationRegex } from '~/utils/offers/zodRegex';
import { createRouter } from '../context';
@@ -264,7 +264,7 @@ export const offersProfileRouter = createRouter()
// TODO: add more
const token = crypto
.createHash('sha256')
.update(Date.now().toString())
.update(Date.now().toString() + generateRandomStringForToken())
.digest('hex');
// Generate random name until unique

View File

@@ -1,4 +1,5 @@
import type { Config } from 'unique-names-generator';
import type { Config} from 'unique-names-generator';
import { countries, names } from 'unique-names-generator';
import { adjectives, animals,colors, uniqueNamesGenerator } from 'unique-names-generator';
import { PrismaClient } from '@prisma/client';
@@ -10,8 +11,7 @@ const customConfig: Config = {
separator: '-',
};
export default async function generateRandomName(): Promise<string> {
export async function generateRandomName(): Promise<string> {
let uniqueName: string = uniqueNamesGenerator(customConfig);
let sameNameProfiles = await prisma.offersProfile.findMany({
@@ -30,4 +30,15 @@ export default async function generateRandomName(): Promise<string> {
}
return uniqueName
}
const tokenConfig: Config = {
dictionaries: [adjectives, colors, animals, countries, names]
.sort((_a, _b) => 0.5 - Math.random()),
length: 5,
separator: '-',
};
export function generateRandomStringForToken(): string {
return uniqueNamesGenerator(tokenConfig)
}