findEmail.js 1.03 KB
import { prisma, generateSecret, isAuthenticated } from "../../../utils";
import twilio from "twilio";

export default {
  Query: {
    findEmail: async (_, args) => {
      if (isAuthenticated) {
        const { phoneNumber } = args;
        const accountSid = process.env.TWILIO_SID;
        const authToken = process.env.TWILIO_AUTH_TOKEN;
        const client = new twilio(accountSid, authToken);
        const randomWords = generateSecret();
        client.messages
          .create({
            body: `Please enter this word : ${randomWords}`,
            to: `${phoneNumber}`,
            from: "KhuChat",
          })
          .then((message) => {
            console.log(message.sid);
            console.log(" --- " + message);
          });

        const user = await prisma.user.update({
          where: {
            phoneNumber,
          },
          data: {
            phoneSecret: randomWords,
          },
        });
        return user.email;
      } else {
        throw new Error("You need to login first");
      }
    },
  },
};