sdy

update sendmail

import dotenv from "dotenv";
dotenv.config();
import { PrismaClient } from "@prisma/client";
import { nouns, adjectives } from "./words";
import jwt from "jsonwebtoken";
import nodemailer from "nodemailer";
import sgTransport from "nodemailer-sendgrid-transport";
import sgMail from "@sendgrid/mail";
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
export const prisma = new PrismaClient();
......@@ -25,7 +27,7 @@ export const isAuthenticated = (request) => {
export const changePhoneNumber = (phoneNum, locationNum) => {
var leftStr = locationNum;
var rightStr = phoneNum.slice(1, phoneNum.length());
var rightStr = phoneNum.slice(1, phoneNum.length);
var newStr = leftStr + rightStr;
return newStr;
};
......@@ -35,25 +37,26 @@ export const generateSecret = () => {
return `${adjectives[randomNumber]} ${nouns[randomNumber]}`;
};
const sendEmail = (email) => {
const options = {
auth: {
api_user: process.env.SENDGRID_USERNAME,
api_password: process.env.SENDGRID_PASSWORD,
},
};
const client = nodemailer.createTransport(sgTransport(options));
return client.sendMail(email);
};
export const generateToken = (id) => jwt.sign({ id }, process.env.JWT_SECRET);
export const sendSecretMail = (address, emailSecret, value) => {
const email = {
from: "vel1024@khu.ac.kr",
to: address,
subject: `Authentication key for forgotten ${value}`,
html: `Hello, This is khuchat, authentication key is <b>${emailSecret}</b>, copy and paste it, Thanks.`,
export const createEMessage = (userEmail, emailSecret) => {
const emailMessage = {
to: `${userEmail}`,
from: "KhuChat@KhuChat.com",
subject: "Email from KhuChat",
html: `We send email for reset your password, enter this key : <b>${emailSecret}</b>`,
};
return sendEmail(email);
return emailMessage;
};
export const generateToken = (id) => jwt.sign({ id }, process.env.JWT_SECRET);
export const sendEmail = (userEmail, emailSecret) => {
sgMail.send(createEMessage(userEmail, emailSecret)).then(
() => {},
(error) => {
console.error(error);
if (error.response) {
console.error(error.response.body);
}
}
);
};
......