Compare commits

...

10 Commits

Author SHA1 Message Date
geonhee-min
ab74fd1a71 issue # gitlab-ci test 2025-11-24 11:16:51 +09:00
geonhee-min
7ea116dc8e issue # gitlab-ci 테스트 2025-11-24 10:47:40 +09:00
geonhee-min
5e65a70ce2 issue # gitlab-ci 테스트 2025-11-24 10:44:55 +09:00
geonhee-min
818aa659fc issue # yarn.lock 파일 gitignore 제외 2025-11-24 10:34:15 +09:00
geonhee-min
f810fc888d issue # gitlab-ci 테스트 2025-11-24 10:29:41 +09:00
geonhee-min
dbf96453b5 issue # gitlab-ci 테스트 2025-11-24 10:27:42 +09:00
geonhee-min
fb544e9e3a issue # gitignore 수정 2025-11-24 08:31:15 +09:00
민건희
dce509bad9 issue # 회원가입 로직 구현 완료 2025-11-23 23:02:45 +09:00
geonhee-min
8303a8ab19 issue # 이메일 인증 로직 구현 중 2025-11-21 16:31:33 +09:00
geonhee-min
6cd0361375 issue # 이메일 인증 코드 발송 로직 구현 2025-11-21 16:21:07 +09:00
35 changed files with 11159 additions and 78 deletions

28
.env
View File

@@ -1,28 +0,0 @@
# PostgreSQL 설정
PGHOST=bkdhome.p-e.kr
PGPORT=15454
PGDATABASE=scheduler
PGUSER=baekyangdan
PGPASSWORD=qwas745478!
PG_DATABASE_URL=postgres://baekyangdan:qwas745478!@bkdhome.p-e.kr:15454/scheduler
# Redis 설정
RD_HOST=bkdhome.p-e.kr
RD_PORT=16779
RD_URL=redis://bkdhome.p-e.kr:16779
# Express 서버 포트
PORT=3000
# Gmail SMTP 설정
GMAIL_USER=bkd.scheduler@gmail.com
GMAIL_PASS= # 앱 비밀번호 또는 OAuth2 토큰
GMAIL_CLIENT_ID=688417162908-iqvnj4ceb8t1dkbjr70dtcafo27m8kqe.apps.googleusercontent.com
GMAIL_CLIENT_SECRET=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
GMAIL_REFRESH_TOKEN=1//04_pSivNoGpPUCgYIARAAGAQSNwF-L9IrO0Kx6jSzq_eQNjdl65f0O2iqKSNpFeZ3gtIGMhOk0oiZsnKrPfWs8jvuEic1NhUoZ0g
# SMTP 추가 옵션
SMTP_AUTH=true
SMTP_STARTTLS_ENABLE=true
SMTP_STARTTLS_REQUIRED=true
SMTP_AUTH_MECHANISMS=XOAUTH2

15
.env.common Normal file
View File

@@ -0,0 +1,15 @@
# Nestjs 서버 포트
PORT=3000
# Gmail SMTP 설정
GMAIL_USER=bkd.scheduler@gmail.com
GMAIL_PASS= # 앱 비밀번호 또는 OAuth2 토큰
GMAIL_CLIENT_ID=688417162908-iqvnj4ceb8t1dkbjr70dtcafo27m8kqe.apps.googleusercontent.com
GMAIL_CLIENT_SECRET=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
GMAIL_REFRESH_TOKEN=1//04P8ekVQmkdtnCgYIARAAGAQSNwF-L9IrqPOyH8oYB-mdjUqw9jGHienVLBTWFdiZgpRnPgFmYnAdbjnstd9RkRVeJErB0NRAwg4
# SMTP 추가 옵션
SMTP_AUTH=true
SMTP_STARTTLS_ENABLE=true
SMTP_STARTTLS_REQUIRED=true
SMTP_AUTH_MECHANISMS=XOAUTH2

12
.env.dev Normal file
View File

@@ -0,0 +1,12 @@
# PostgreSQL 설정
PGHOST=bkdhome.p-e.kr
PGPORT=15454
PGDATABASE=scheduler
PGUSER=baekyangdan
PGPASSWORD=qwas745478!
PG_DATABASE_URL=postgres://baekyangdan:qwas745478!@bkdhome.p-e.kr:15454/scheduler
# Redis 설정
RD_HOST=bkdhome.p-e.kr
RD_PORT=6779
RD_URL=redis://bkdhome.p-e.kr:16779

12
.env.local Normal file
View File

@@ -0,0 +1,12 @@
# PostgreSQL 설정
PGHOST=bkdhome.p-e.kr
PGPORT=15454
PGDATABASE=scheduler
PGUSER=baekyangdan
PGPASSWORD=qwas745478!
PG_DATABASE_URL=postgres://192.168.219.107:5454/scheduler
# Redis 설정
RD_HOST=bkdhome.p-e.kr
RD_PORT=6779
RD_URL=redis://192.168.219.107:6779

12
.env.prod Normal file
View File

@@ -0,0 +1,12 @@
# PostgreSQL 설정
PGHOST=db
PGPORT=5454
PGDATABASE=scheduler
PGUSER=baekyangdan
PGPASSWORD=qwas745478!
PG_DATABASE_URL=postgres://baekyangdan:qwas745478!@db:5454/scheduler
# Redis 설정
RD_HOST=redis
RD_PORT=6779
RD_URL=redis://redis:6779

14
.gitignore vendored
View File

@@ -3,11 +3,19 @@ node_modules/
npm-debug.log* npm-debug.log*
yarn-error.log* yarn-error.log*
package-lock.json package-lock.json
yarn.lock # yarn.lock
# Yarn Berry
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
.pnp.* .pnp.*
.pnp.loader.mjs # .pnp.loader.mjs
.yarn/install-state.gz # .yarn/install-state.gz
# TypeScript # TypeScript
dist/ dist/

View File

@@ -19,25 +19,24 @@
# stages: # List of stages for jobs, and their order of execution # stages: # List of stages for jobs, and their order of execution
# - build # - build
# cache: cache:
# key: key: "${CI_COMMIT_REF_SLUG}"
# files: paths:
# - package-lock.json - .yarn/cache/
# paths:
# - node_modules/
# build: # This job runs in the build stage, which runs first. build: # This job runs in the build stage, which runs first.
# stage: build stage: build
# tags: tags:
# - local-runner - local-runner
# before_script: before_script:
# script: script:
# - echo "Compiling the code..." - echo "Compiling the code..."
# - echo $DOCKER_VOLUME - echo $DOCKER_VOLUME
# - echo $DOCKER_COMPOSE_VOLUME - echo $DOCKER_COMPOSE_VOLUME
# - npm install - rm -rf node_modules .yarn/install-state.gz
# - npm run build - yarn install
# - sudo cp -r $PWD/dist/. $DOCKER_VOLUME/scheduler/back/dist - yarn build --webpack
# - sudo cp $PWD/package.json $DOCKER_VOLUME/scheduler/back/dist - sudo cp -r $PWD/dist/. $DOCKER_VOLUME/scheduler/back/dist
# - docker compose -f $DOCKER_COMPOSE_VOLUME/scheduler/docker-compose.yaml up -d back - sudo cp $PWD/package.json $DOCKER_VOLUME/scheduler/back/dist
# - echo "Compile complete." - docker compose -f $DOCKER_COMPOSE_VOLUME/scheduler/docker-compose.yaml up -d back
- echo "Compile complete."

942
.yarn/releases/yarn-4.11.0.cjs vendored Normal file

File diff suppressed because one or more lines are too long

2
.yarnrc.yml Normal file
View File

@@ -0,0 +1,2 @@
yarnPath: .yarn/releases/yarn-4.11.0.cjs
nodeLinker: node-modules

View File

@@ -54,7 +54,7 @@ export const account = pgTable("account", {
birthday: date(), birthday: date(),
accountId: varchar("account_id").notNull(), accountId: varchar("account_id").notNull(),
nickname: varchar().notNull(), nickname: varchar().notNull(),
status: varchar().default('wait').notNull(), status: varchar().default('active').notNull(),
isDeleted: boolean("is_deleted").default(false).notNull(), isDeleted: boolean("is_deleted").default(false).notNull(),
createdAt: date("created_at").defaultNow().notNull(), createdAt: date("created_at").defaultNow().notNull(),
id: uuid().default(sql`uuid_generate_v4()`).primaryKey().notNull(), id: uuid().default(sql`uuid_generate_v4()`).primaryKey().notNull(),

View File

@@ -6,12 +6,13 @@
"private": true, "private": true,
"license": "UNLICENSED", "license": "UNLICENSED",
"scripts": { "scripts": {
"build": "nest build", "build": "NODE_ENV=prod nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start", "start": "nest start",
"start:dev": "nest start --watch", "start:local": "NODE_ENV=local nest start --watch",
"start:dev": "NODE_ENV=dev nest start --watch",
"start:debug": "nest start --debug --watch", "start:debug": "nest start --debug --watch",
"start:prod": "node dist/main", "start:prod": "NODE_ENV=prod node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest", "test": "jest",
"test:watch": "jest --watch", "test:watch": "jest --watch",
@@ -23,12 +24,16 @@
"@nestjs/class-transformer": "^0.4.0", "@nestjs/class-transformer": "^0.4.0",
"@nestjs/class-validator": "^0.13.4", "@nestjs/class-validator": "^0.13.4",
"@nestjs/common": "^11.0.1", "@nestjs/common": "^11.0.1",
"@nestjs/config": "^4.0.2",
"@nestjs/core": "^11.0.1", "@nestjs/core": "^11.0.1",
"@nestjs/platform-express": "^11.0.1", "@nestjs/platform-express": "^11.0.1",
"bcrypt": "^6.0.0",
"dotenv": "^17.2.3", "dotenv": "^17.2.3",
"drizzle-kit": "^0.31.7", "drizzle-kit": "^0.31.7",
"drizzle-orm": "^0.44.7", "drizzle-orm": "^0.44.7",
"googleapis": "^166.0.0",
"ioredis": "^5.8.2", "ioredis": "^5.8.2",
"nodemailer": "^7.0.10",
"pg": "^8.16.3", "pg": "^8.16.3",
"reflect-metadata": "^0.2.2", "reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1" "rxjs": "^7.8.1"
@@ -39,10 +44,12 @@
"@nestjs/cli": "^11.0.0", "@nestjs/cli": "^11.0.0",
"@nestjs/schematics": "^11.0.0", "@nestjs/schematics": "^11.0.0",
"@nestjs/testing": "^11.0.1", "@nestjs/testing": "^11.0.1",
"@types/bcrypt": "^6",
"@types/express": "^5.0.0", "@types/express": "^5.0.0",
"@types/ioredis": "^5.0.0", "@types/ioredis": "^5.0.0",
"@types/jest": "^30.0.0", "@types/jest": "^30.0.0",
"@types/node": "^22.10.7", "@types/node": "^22.10.7",
"@types/nodemailer": "^7.0.4",
"@types/pg": "^8.15.6", "@types/pg": "^8.15.6",
"@types/supertest": "^6.0.2", "@types/supertest": "^6.0.2",
"eslint": "^9.18.0", "eslint": "^9.18.0",
@@ -76,5 +83,6 @@
], ],
"coverageDirectory": "../coverage", "coverageDirectory": "../coverage",
"testEnvironment": "node" "testEnvironment": "node"
} },
"packageManager": "yarn@4.11.0"
} }

View File

@@ -3,9 +3,12 @@ import { AppController } from './app.controller';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { DbModule } from './db/db.module'; import { DbModule } from './db/db.module';
import { RedisModule } from './redis/redis.module'; import { RedisModule } from './redis/redis.module';
import { AccountModule } from './modules/account/account.module';
import { MailerModule } from './util/mailer/mailer.module';
import { AppConfigModule } from './config/config.module';
@Module({ @Module({
imports: [DbModule, RedisModule], imports: [AppConfigModule, DbModule, RedisModule, MailerModule, AccountModule],
controllers: [AppController], controllers: [AppController],
providers: [AppService], providers: [AppService],
}) })

View File

@@ -0,0 +1,15 @@
import { Module } from "@nestjs/common";
import { ConfigModule } from '@nestjs/config';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: [
'.env.common',
`.env.${process.env.NODE_ENV}`
]
})
]
})
export class AppConfigModule{}

View File

@@ -1,20 +1,22 @@
import { Global, Module } from "@nestjs/common"; import { Global, Module } from "@nestjs/common";
import { Pool } from "pg"; import { Pool } from "pg";
import { drizzle, NodePgDatabase } from "drizzle-orm/node-postgres"; import { drizzle, NodePgDatabase } from "drizzle-orm/node-postgres";
import { ConfigModule, ConfigService } from "@nestjs/config";
import * as schema from '../../drizzle/schema'; import * as schema from '../../drizzle/schema';
@Global() @Global()
@Module({ @Module({
imports: [ConfigModule],
providers: [ providers: [
{ {
provide: "DRIZZLE", provide: "DRIZZLE",
useFactory: (): NodePgDatabase<typeof schema> => { useFactory: (configService: ConfigService): NodePgDatabase<typeof schema> => {
const pool = new Pool({ const pool = new Pool({
connectionString: process.env.PG_DATABASE_URL connectionString: configService.get<string>('PG_DATABASE_URL')
}); });
return drizzle(pool, { schema: schema }); return drizzle(pool, { schema: schema });
} },
inject: [ConfigService]
} }
], ],
exports: ["DRIZZLE"] exports: ["DRIZZLE"]

View File

@@ -1,8 +1,29 @@
import { NestFactory } from '@nestjs/core'; import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
import dotenv from 'dotenv';
dotenv.config();
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
app.enableCors({
origin: (origin, callback) => {
// origin이 없는 경우(local file, curl 등) 허용
if (!origin) return callback(null, true);
// 특정 도메인만 막고 싶은 경우 whitelist 가능
const whitelist = ["http://localhost:5173", "https://scheduler.bkdhome.p-e.kr"];
if (whitelist.includes(origin)) {
return callback(null, true);
}
// 그 외 모든 도메인 허용 → 사실상 wildcard
return callback(null, true);
},
credentials: true,
});
await app.listen(process.env.PORT ?? 3000); await app.listen(process.env.PORT ?? 3000);
} }
bootstrap(); bootstrap();

View File

@@ -1,14 +1,43 @@
import { Controller, Get, Post, Query } from "@nestjs/common"; import { Body, Controller, Get, Post, Query } from "@nestjs/common";
import { CheckDuplicationRequestDto } from "./dto/checkDuplication/check-duplication-request.dto";
import { CheckDuplicationResponseDto } from "./dto/checkDuplication/check-duplication-response.dto";
import { AccountService } from "./account.service"; import { AccountService } from "./account.service";
import {
CheckDuplicationRequest, CheckDuplicationResponse,
SendVerificationCodeRequest, SendVerificationCodeResponse,
VerifyCodeRequest, VerifyCodeResponse,
LoginRequest, LoginResponse,
SignupRequest, SignupResponse
} from "./dto";
@Controller('account') @Controller('account')
export class AccountController { export class AccountController {
constructor(private readonly accountService: AccountService) {} constructor(private readonly accountService: AccountService) {}
@Get('/')
async test() {
return "Test"
}
@Get('check-duplication') @Get('check-duplication')
async checkDuplication(@Query() query: CheckDuplicationRequestDto): Promise<CheckDuplicationResponseDto> { async checkDuplication(@Query() query: CheckDuplicationRequest): Promise<CheckDuplicationResponse> {
return this.accountService.checkDuplication(query); return await this.accountService.checkDuplication(query);
}
@Post('send-verification-code')
async sendVerificationCode(@Body() body: SendVerificationCodeRequest): Promise<SendVerificationCodeResponse> {
const result = await this.accountService.sendVerificationCode(body);
return result;
}
@Post('verify-code')
async verifyCode(@Body() body: VerifyCodeRequest): Promise<VerifyCodeResponse> {
console.log(body.email);
const result = await this.accountService.verifyCode(body);
return result;
}
@Post('signup')
async signup(@Body() body: SignupRequest): Promise<LoginResponse> {
const result = await this.accountService.signup(body);
return result;
} }
} }

View File

@@ -5,6 +5,7 @@ import { AccountService } from "./account.service";
@Module({ @Module({
controllers: [AccountController], controllers: [AccountController],
providers: [AccountService, AccountRepo] providers: [AccountService, AccountRepo],
exports: [AccountService, AccountRepo]
}) })
export class AccountModule {} export class AccountModule {}

View File

@@ -18,4 +18,25 @@ export class AccountRepo {
return result[0].count; return result[0].count;
} }
async signup(
accountId: string,
name: string,
nickname: string,
email: string,
password: string
) {
return this
.db
.insert(schema.account)
.values({
accountId: accountId,
name: name,
nickname: nickname,
email: email,
password: password
});
}
async
} }

View File

@@ -1,15 +1,73 @@
import { Injectable } from "@nestjs/common"; import { Inject, Injectable } from "@nestjs/common";
import { AccountRepo } from "./account.repo"; import { AccountRepo } from "./account.repo";
import { CheckDuplicationRequestDto } from "./dto/checkDuplication/check-duplication-request.dto"; import * as DTO from './dto';
import { CheckDuplicationResponseDto } from "./dto/checkDuplication/check-duplication-response.dto"; import { MailerService } from "src/util/mailer/mailer.service";
import { Generator } from "src/util/generator";
import Redis from "ioredis";
import { Converter } from "src/util/converter";
@Injectable() @Injectable()
export class AccountService { export class AccountService {
constructor(private readonly accountRepo: AccountRepo) {} constructor(
private readonly accountRepo: AccountRepo
, private readonly mailerService: MailerService
, @Inject("REDIS") private readonly redis: Redis
) {}
async checkDuplication(data: CheckDuplicationRequestDto): Promise<CheckDuplicationResponseDto> { async checkDuplication(data: DTO.CheckDuplicationRequest): Promise<DTO.CheckDuplicationResponse> {
const count = await this.accountRepo.checkDuplication(data.type, data.value); const { type, value } = data;
const count = await this.accountRepo.checkDuplication(type, value);
return { isDuplicated: count > 0 }; return { isDuplicated: count > 0 };
} }
async sendVerificationCode(data: DTO.SendVerificationCodeRequest): Promise<DTO.SendVerificationCodeResponse> {
const { email } = data;
const code = Generator.getVerificationCode();
const html = `<p>Your verification code is: <strong style="font-size:16px;">${code}</strong></p>`;
const result = await this.mailerService.sendMail(email, "<Scheduler> 이메일 인증 코드", html);
if (result.rejected.length > 0) {
return { success: false, error: result.response }
} else {
await this.redis.set(`verify:${email}`, code, 'EX', 600);
return { success: true, message: "이메일 발송 완료" };
}
}
async verifyCode(data: DTO.VerifyCodeRequest): Promise<DTO.VerifyCodeResponse> {
const { email, code } = data;
const storedCode = await this.redis.get(`verify:${email}`);
if (!storedCode) {
return { verified: false, error: '잘못된 이메일이거나 코드가 만료되었습니다.'};
}
if (storedCode !== code) {
return { verified: false, error: "잘못된 코드입니다." };
}
await this.redis.del(`verify:${email}`);
return { verified: true, message: "이메일 인증이 완료되었습니다." };
}
async signup(data: DTO.SignupRequest): Promise<DTO.SignupResponse> {
const { accountId, name, nickname, email, password } = data;
const hashedPassword = Converter.getHashedPassword(password);
const result = await this.accountRepo.signup(accountId, name, nickname, email, hashedPassword);
if (result.rowCount) {
return {
success: true,
message: "회원가입이 완료되었습니다."
};
} else {
return {
success: false,
error: "회원가입에 실패하였습니다."
};
}
}
} }

View File

@@ -0,0 +1,14 @@
export { CheckDuplicationRequestDto as CheckDuplicationRequest } from './checkDuplication/check-duplication-request.dto';
export { CheckDuplicationResponseDto as CheckDuplicationResponse } from './checkDuplication/check-duplication-response.dto';
export { SendVerificationCodeRequestDto as SendVerificationCodeRequest } from './sendVerification/send-verification-code-request.dto';
export { SendVerificationCodeResponseDto as SendVerificationCodeResponse } from './sendVerification/send-verification-code-response.dto';
export { VerifyCodeRequestDto as VerifyCodeRequest } from './verifyCode/verify-code-request.dto';
export { VerifyCodeResponseDto as VerifyCodeResponse } from './verifyCode/verify-code-response.dto';
export { SignupRequestDto as SignupRequest } from './signup/signup-request.dto';
export { SignupResponseDto as SignupResponse } from './signup/signup-response.dto';
export { LoginRequestDto as LoginRequest } from './login/login-request.dto';
export { LoginResponseDto as LoginResponse } from './login/login-response.dto'

View File

@@ -0,0 +1,11 @@
import { IsString } from "@nestjs/class-validator";
export class LoginRequestDto {
type: 'email' | 'accountId';
@IsString()
id: string;
@IsString()
password: string;
}

View File

@@ -0,0 +1,5 @@
export class LoginResponseDto {
success: boolean;
message?: string;
error?: string;
}

View File

@@ -0,0 +1,6 @@
import { IsEmail } from "@nestjs/class-validator";
export class SendVerificationCodeRequestDto {
@IsEmail()
email: string;
}

View File

@@ -0,0 +1,5 @@
export class SendVerificationCodeResponseDto {
success: boolean;
message?: string;
error?: string;
}

View File

@@ -0,0 +1,18 @@
import { IsEmail, IsString } from "@nestjs/class-validator";
export class SignupRequestDto {
@IsEmail()
email: string;
@IsString()
name: string;
@IsString()
nickname: string;
@IsString()
accountId: string;
@IsString()
password: string;
}

View File

@@ -0,0 +1,5 @@
export class SignupResponseDto {
success: boolean;
message?: string;
error?: string;
}

View File

@@ -0,0 +1,9 @@
import { IsEmail, IsString } from "@nestjs/class-validator";
export class VerifyCodeRequestDto {
@IsEmail()
email: string;
@IsString()
code: string;
}

View File

@@ -0,0 +1,5 @@
export class VerifyCodeResponseDto {
verified: boolean;
message?: string;
error?: string;
}

View File

@@ -1,4 +1,5 @@
import { Global, Module } from "@nestjs/common"; import { Global, Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import Redis from "ioredis"; import Redis from "ioredis";
@Global() @Global()
@@ -6,12 +7,13 @@ import Redis from "ioredis";
providers: [ providers: [
{ {
provide: "REDIS", provide: "REDIS",
useFactory: () => { useFactory: (configService: ConfigService): Redis => {
return new Redis({ return new Redis({
host: process.env.RD_HOST!, host: configService.get<string>('RD_HOST')!,
port: Number(process.env.RD_PORT || 6779) port: configService.get<number>('RD_PORT')
}); });
} },
inject: [ConfigService]
}, },
], ],
exports: ["REDIS"] exports: ["REDIS"]

11
src/util/converter.ts Normal file
View File

@@ -0,0 +1,11 @@
import bcrypt from 'bcrypt';
export class Converter {
static getHashedPassword(password: string) {
return bcrypt.hashSync(password, 10);
}
static comparePassword(rawPassword: string, hashedPassword: string) {
return bcrypt.compareSync(rawPassword, hashedPassword);
}
}

5
src/util/generator.ts Normal file
View File

@@ -0,0 +1,5 @@
export class Generator {
static getVerificationCode() {
return Math.random().toString().slice(2, 8);
}
}

View 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 {}

View File

@@ -0,0 +1,57 @@
import { Injectable } from '@nestjs/common';
import nodemailer from 'nodemailer';
import { google } from 'googleapis';
import { OAuth2Client } from 'google-auth-library';
import SMTPTransport from 'nodemailer/lib/smtp-transport';
import { ConfigService } from '@nestjs/config';
@Injectable()
export class MailerService {
private oauth2Client: OAuth2Client;
private readonly gmailUser: string;
constructor(private readonly configService: ConfigService) {
const clientId = this.configService.get<string>('GMAIL_CLIENT_ID');
const clientSecret = this.configService.get<string>('GMAIL_CLIENT_SECRET');
const refreshToken = this.configService.get<string>('GMAIL_REFRESH_TOKEN');
this.gmailUser = this.configService.get<string>('GMAIL_USER')!;
this.oauth2Client = new google.auth.OAuth2(
clientId,
clientSecret,
'https://developers.google.com/oauthplayground'
);
this.oauth2Client.setCredentials({
refresh_token: refreshToken,
});
}
async sendMail(to: string, subject: string, html: string) {
const accessToken = await this.oauth2Client.getAccessToken();
const options: SMTPTransport.Options = {
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
type: "OAuth2",
user: this.gmailUser,
clientId: this.configService.get<string>('GMAIL_CLIENT_ID'),
clientSecret: this.configService.get<string>('GMAIL_CLIENT_SECRET'),
refreshToken: this.configService.get<string>('GMAIL_REFRESH_TOKEN'),
accessToken: accessToken?.token || '',
}
}
const transporter = nodemailer.createTransport(options);
return transporter.sendMail({
from: `Scheduler ${this.gmailUser}>`,
to,
subject,
html
})
}
}

View File

@@ -1,4 +1,10 @@
{ {
"compilerOptions": {
"noEmitOnError": true,
"sourceMap": false,
"incremental": true,
"tsBuildInfoFile": ".tsbuildinfo"
},
"extends": "./tsconfig.json", "extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"] "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
} }

9781
yarn.lock Normal file

File diff suppressed because it is too large Load Diff