issue # 이메일 인증 코드 발송 로직 구현
This commit is contained in:
0
src/util/converter.ts
Normal file
0
src/util/converter.ts
Normal file
5
src/util/generator.ts
Normal file
5
src/util/generator.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export class Generator {
|
||||
static getVerificationCode() {
|
||||
return Math.random().toString().slice(2, 8);
|
||||
}
|
||||
}
|
||||
9
src/util/mailer/mailer.module.ts
Normal file
9
src/util/mailer/mailer.module.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { Module, Global } from '@nestjs/common';
|
||||
import { MailerService } from './mailer.service';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [MailerService],
|
||||
exports: [MailerService]
|
||||
})
|
||||
export class MailerModule {}
|
||||
46
src/util/mailer/mailer.service.ts
Normal file
46
src/util/mailer/mailer.service.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import nodemailer from 'nodemailer';
|
||||
import { google } from 'googleapis';
|
||||
import SMTPTransport from 'nodemailer/lib/smtp-transport';
|
||||
|
||||
@Injectable()
|
||||
export class MailerService {
|
||||
private oauth2Client = new google.auth.OAuth2(
|
||||
process.env.GMAIL_CLIENT_ID,
|
||||
process.env.GMAIL_CLIENT_SECRET,
|
||||
'https://developers.google.com/oauthplayground'
|
||||
);
|
||||
|
||||
constructor() {
|
||||
this.oauth2Client.setCredentials({
|
||||
refresh_token: process.env.GMAIL_REFRESH_TOKEN
|
||||
});
|
||||
}
|
||||
|
||||
async sendMail(to: string, subject: string, html: string) {
|
||||
const accessToken = await this.oauth2Client.getAccessToken();
|
||||
console.log(accessToken);
|
||||
const options: SMTPTransport.Options = {
|
||||
host: "smtp.gmail.com",
|
||||
port: 465,
|
||||
secure: true,
|
||||
auth: {
|
||||
type: "OAuth2",
|
||||
user: process.env.GMAIL_USER,
|
||||
clientId: process.env.GMAIL_CLIENT_ID,
|
||||
clientSecret: process.env.GMAIL_CLIENT_SECRET,
|
||||
refreshToken: process.env.GMAIL_REFRESH_TOKEN,
|
||||
accessToken: accessToken?.token || ''
|
||||
}
|
||||
}
|
||||
|
||||
const transporter = nodemailer.createTransport(options);
|
||||
|
||||
return transporter.sendMail({
|
||||
from: `Scheduler ${process.env.GMAIL_USER}>`,
|
||||
to,
|
||||
subject,
|
||||
html
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user