I have a users.json file that looks something like this: `[{ "email": "a@a.com" }, { "email": "a@c.c...
j
I have a users.json file that looks something like this:
[{ "email": "a@a.com" }, { "email": "a@c.com" }, { "email": "a@d.com" }]
I am allowing users email those who are in this json file; otherwise, I am throwing an error. The code for that is like this:
Copy code
emailPasswordSignUpPOST: async function (input: any) {
try {
  if (
   originalImplementation.emailPasswordSignUpPOST ===
   undefined
   ) {
       throw Error('Should never come here');
     }
     const inputObject: any = {};
     for (let index = 0; index < input.formFields.length; index++) {
      const element = input.formFields[index];
      inputObject[element.id] = element.value;
     }
     const { email, firstName, lastName, phoneNumber } = inputObject;
     fs.readFile(usersFilePath, 'utf8', (error, data) => {
       if (error) {
         throw new HttpException(error.message, 500);
       } else {
           const users = JSON.parse(data);
           const userEmails = users.map((user: any) => user.email);
           if (!userEmails.includes(email)) {
             throw new HttpException(
               'You are not allowed to register.',
               HttpStatus.BAD_REQUEST
              );
           
        }
   });
   let response: any = await originalImplementation.emailPasswordSignUpPOST(input);
   response['user'] = {
           ...response['user'],
           email,
           firstName,
           lastName,
           phoneNumber,
         };
    return response;
} catch (error) {
    throw new HttpException(
       error.message,
       HttpStatus.BAD_REQUEST
     );
  }
},
If I try to register with the wrong credentials, then it is throwing an error something like this:
HttpException: You are not allowed to register.
and it shuts down the server (that is what I think is happening), which is expected, but then if I try to register with the correct credentials then it is not working. (Nothing ,no log at all) How do I troubleshoot this server shut down?
n
Hi @johnoliver2342 So any errors thrown from the function will be treated as if the API returns a 500, in which case if your server doesnt handle errors correctly it will crash. One way would be to use general errors that are meant to be handled on a frontend in a specific way. To know more read here: https://supertokens.com/docs/emailpassword/advanced-customizations/apis-override/custom-response/general-error
About the issue of the flow not working, what is the value of
response
before you modify the user property?
j
No worries working now
4 Views