sdy

add packages (email, jwt)

1 -import jwt from "jsonwebtoken";
2 import { PrismaClient } from "@prisma/client"; 1 import { PrismaClient } from "@prisma/client";
2 +import { nouns, adjectives } from "./words";
3 +import jwt from "jsonwebtoken";
4 +import nodemailer from "nodemailer";
5 +import sgTransport from "nodemailer-sendgrid-transport";
3 6
4 export const prisma = new PrismaClient(); 7 export const prisma = new PrismaClient();
5 8
...@@ -12,3 +15,31 @@ export const getUserId = (context) => { ...@@ -12,3 +15,31 @@ export const getUserId = (context) => {
12 } 15 }
13 throw new Error("There is no vaild user"); 16 throw new Error("There is no vaild user");
14 }; 17 };
18 +
19 +export const generateSecret = () => {
20 + const randomNumber = Math.floor(Math.random() * adjectives.length);
21 + return `${adjectives[randomNumber]} ${nouns[randomNumber]}`;
22 +};
23 +
24 +const sendEmail = (email) => {
25 + const options = {
26 + auth: {
27 + api_user: process.env.SENDGRID_USERNAME,
28 + api_password: process.env.SENDGRID_PASSWORD,
29 + },
30 + };
31 + const client = nodemailer.createTransport(sgTransport(options));
32 + return client.sendMail(email);
33 +};
34 +
35 +export const sendSecretMail = (address, emailSecret, value) => {
36 + const email = {
37 + from: "vel1024@khu.ac.kr",
38 + to: address,
39 + subject: `Authentication key for forgotten ${value}`,
40 + html: `Hello, This is khuchat, authentication key is <b>${emailSecret}</b>, copy and paste it, Thanks.`,
41 + };
42 + return sendEmail(email);
43 +};
44 +
45 +export const generateToken = (id) => jwt.sign({ id }, process.env.JWT_SECRET);
......