Skip to content

Commit

Permalink
fix: hardcoded phone number
Browse files Browse the repository at this point in the history
  • Loading branch information
ClementNumericite committed Feb 26, 2024
1 parent 849a6e6 commit 7be2308
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 43 deletions.
4 changes: 2 additions & 2 deletions webapp/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import ChakraNextImage from "~/components/ChakraNextImage";
import FormInput from "~/components/forms/FormInput";
import { loginAnimation } from "~/utils/animations";
import { api } from "~/utils/api";
import { frenchPhoneNumber } from "~/utils/tools";
import { addSpaceToTwoCharacters, frenchPhoneNumber } from "~/utils/tools";

type LoginForm = {
phone_number: string;
Expand Down Expand Up @@ -174,7 +174,7 @@ export default function Home() {
Vous avez reçu un code à 4 chiffres par SMS
</Heading>
<Text fontSize={"sm"} fontWeight="medium" color="secondaryText">
Saisissez le code envoyé au 06 15 29 20 93 pour pouvoir créer votre
Saisissez le code envoyé au {addSpaceToTwoCharacters(formValues.phone_number)} pour pouvoir créer votre
compte
</Text>
<Box my={8}>
Expand Down
90 changes: 49 additions & 41 deletions webapp/src/utils/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,49 @@ import crypto from 'crypto';
import { Where } from 'payload/types';

export const convertFrenchDateToEnglish = (
frenchDate: string
frenchDate: string
): string | null => {
const match = frenchDate.match(/^(\d{2})\/(\d{2})\/(\d{2})$/);
const match = frenchDate.match(/^(\d{2})\/(\d{2})\/(\d{2})$/);

if (match) {
const [, day, month, year] = match.map(Number);
if (match) {
const [, day, month, year] = match.map(Number);

if (year !== undefined && month !== undefined && day !== undefined) {
// Creating a Date object with a four-digit year
const englishFormattedDate = new Date(2000 + year, month - 1, day);
if (year !== undefined && month !== undefined && day !== undefined) {
// Creating a Date object with a four-digit year
const englishFormattedDate = new Date(2000 + year, month - 1, day);

// Using toISOString to get the date in ISO format (YYYY-MM-DD)
return englishFormattedDate.toISOString().split("T")[0] || null;
}
}
// Using toISOString to get the date in ISO format (YYYY-MM-DD)
return englishFormattedDate.toISOString().split("T")[0] || null;
}
}

// Return null if the input format is incorrect
return null;
// Return null if the input format is incorrect
return null;
};

export const frenchPhoneNumber = /^(?:\+33[1-9](\d{8})|(?!.*\+\d{2}).{10})$/;

export const payloadOrPhoneNumberCheck = (phone_number: string): Where => {
const hasDialingCode = phone_number.startsWith('+')

return {
or: [
{
phone_number: {
equals: hasDialingCode
? phone_number
: `+33${phone_number.substring(1)}`,
},
},
{
phone_number: {
equals: hasDialingCode
? `0${phone_number.substring(3)}`
: phone_number,
},
},
],
};
return {
or: [
{
phone_number: {
equals: hasDialingCode
? phone_number
: `+33${phone_number.substring(1)}`,
},
},
{
phone_number: {
equals: hasDialingCode
? `0${phone_number.substring(3)}`
: phone_number,
},
},
],
};
};

export const payloadWhereOfferIsValid = (prefix?: string): Where => {
Expand Down Expand Up @@ -77,18 +77,26 @@ export const payloadWhereOfferIsValid = (prefix?: string): Where => {
}

export const generateRandomCode = (): string => {
const min = 1000;
const max = 9999;
const randomCode = Math.floor(Math.random() * (max - min + 1) + min);
return randomCode.toString();
const min = 1000;
const max = 9999;
const randomCode = Math.floor(Math.random() * (max - min + 1) + min);
return randomCode.toString();
};

export const generateRandomPassword = (length: number): string => {
const buffer = crypto.randomBytes(length);
const password = buffer
.toString("base64")
.replace(/[/+=]/g, "")
.slice(0, length);
const buffer = crypto.randomBytes(length);
const password = buffer
.toString("base64")
.replace(/[/+=]/g, "")
.slice(0, length);

return password;
return password;
};

export const addSpaceToTwoCharacters = (input: string) => {
var result = input.replace(/(\d{2})/g, '$1 ');

result = result.trim();

return result;
}

0 comments on commit 7be2308

Please sign in to comment.