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

pull/422/head
Stuart Long Chay Boon 2 years ago
parent 3c80296253
commit c188405de0

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

@ -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 { adjectives, animals,colors, uniqueNamesGenerator } from 'unique-names-generator';
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
@ -10,8 +11,7 @@ const customConfig: Config = {
separator: '-', separator: '-',
}; };
export async function generateRandomName(): Promise<string> {
export default async function generateRandomName(): Promise<string> {
let uniqueName: string = uniqueNamesGenerator(customConfig); let uniqueName: string = uniqueNamesGenerator(customConfig);
let sameNameProfiles = await prisma.offersProfile.findMany({ let sameNameProfiles = await prisma.offersProfile.findMany({
@ -31,3 +31,14 @@ export default async function generateRandomName(): Promise<string> {
return uniqueName 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)
}
Loading…
Cancel
Save