sdy

update sendmail

1 +import dotenv from "dotenv";
2 +dotenv.config();
1 import { PrismaClient } from "@prisma/client"; 3 import { PrismaClient } from "@prisma/client";
2 import { nouns, adjectives } from "./words"; 4 import { nouns, adjectives } from "./words";
3 import jwt from "jsonwebtoken"; 5 import jwt from "jsonwebtoken";
4 -import nodemailer from "nodemailer"; 6 +import sgMail from "@sendgrid/mail";
5 -import sgTransport from "nodemailer-sendgrid-transport"; 7 +sgMail.setApiKey(process.env.SENDGRID_API_KEY);
6 8
7 export const prisma = new PrismaClient(); 9 export const prisma = new PrismaClient();
8 10
...@@ -25,7 +27,7 @@ export const isAuthenticated = (request) => { ...@@ -25,7 +27,7 @@ export const isAuthenticated = (request) => {
25 27
26 export const changePhoneNumber = (phoneNum, locationNum) => { 28 export const changePhoneNumber = (phoneNum, locationNum) => {
27 var leftStr = locationNum; 29 var leftStr = locationNum;
28 - var rightStr = phoneNum.slice(1, phoneNum.length()); 30 + var rightStr = phoneNum.slice(1, phoneNum.length);
29 var newStr = leftStr + rightStr; 31 var newStr = leftStr + rightStr;
30 return newStr; 32 return newStr;
31 }; 33 };
...@@ -35,25 +37,26 @@ export const generateSecret = () => { ...@@ -35,25 +37,26 @@ export const generateSecret = () => {
35 return `${adjectives[randomNumber]} ${nouns[randomNumber]}`; 37 return `${adjectives[randomNumber]} ${nouns[randomNumber]}`;
36 }; 38 };
37 39
38 -const sendEmail = (email) => { 40 +export const generateToken = (id) => jwt.sign({ id }, process.env.JWT_SECRET);
39 - const options = {
40 - auth: {
41 - api_user: process.env.SENDGRID_USERNAME,
42 - api_password: process.env.SENDGRID_PASSWORD,
43 - },
44 - };
45 - const client = nodemailer.createTransport(sgTransport(options));
46 - return client.sendMail(email);
47 -};
48 41
49 -export const sendSecretMail = (address, emailSecret, value) => { 42 +export const createEMessage = (userEmail, emailSecret) => {
50 - const email = { 43 + const emailMessage = {
51 - from: "vel1024@khu.ac.kr", 44 + to: `${userEmail}`,
52 - to: address, 45 + from: "KhuChat@KhuChat.com",
53 - subject: `Authentication key for forgotten ${value}`, 46 + subject: "Email from KhuChat",
54 - html: `Hello, This is khuchat, authentication key is <b>${emailSecret}</b>, copy and paste it, Thanks.`, 47 + html: `We send email for reset your password, enter this key : <b>${emailSecret}</b>`,
55 }; 48 };
56 - return sendEmail(email); 49 + return emailMessage;
57 }; 50 };
58 51
59 -export const generateToken = (id) => jwt.sign({ id }, process.env.JWT_SECRET); 52 +export const sendEmail = (userEmail, emailSecret) => {
53 + sgMail.send(createEMessage(userEmail, emailSecret)).then(
54 + () => {},
55 + (error) => {
56 + console.error(error);
57 + if (error.response) {
58 + console.error(error.response.body);
59 + }
60 + }
61 + );
62 +};
......