mirror of
https://github.com/yangshun/tech-interview-handbook.git
synced 2026-04-27 03:52:34 +08:00
[offers][chore] create random string generator for token
This commit is contained in:
@@ -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({
|
||||||
@@ -30,4 +30,15 @@ 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)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user