-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement SMTP email service, adding env also adding email veri…
…fication (#153) This creates backend email service support. 1, send verification email 2, confirm token for User email 3, resend email 4, template for verification email Frontend: 1, register to tell user check email 2, after user click confirm email link. Frontend should send request to backend to do check Vedio: https://jam.dev/c/1e63139a-e24f-4baf-968c-a9d2e771fef0 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Introduced a robust email verification workflow that requires users to confirm their email addresses upon registration. - Added the ability to resend confirmation emails and a dedicated confirmation page that provides real-time success or error feedback. - **Chores** - Expanded configuration options to support customizable email settings via environment variables. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
f92c788
commit af3af8e
Showing
18 changed files
with
2,149 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { MailerModule } from '@nestjs-modules/mailer'; | ||
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter'; | ||
import { Module } from '@nestjs/common'; | ||
import { MailService } from './mail.service'; | ||
import { join } from 'path'; | ||
import { ConfigModule, ConfigService } from '@nestjs/config'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
import { User } from 'src/user/user.model'; | ||
import { JwtModule } from '@nestjs/jwt'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule, | ||
TypeOrmModule.forFeature([User]), | ||
MailerModule.forRootAsync({ | ||
// imports: [ConfigModule], // import module if not enabled globally | ||
useFactory: async (config: ConfigService) => ({ | ||
// transport: config.get("MAIL_TRANSPORT"), | ||
// or | ||
transport: { | ||
host: config.get('MAIL_HOST'), | ||
port: config.get<number>('MAIL_PORT'), | ||
secure: false, | ||
auth: { | ||
user: config.get('MAIL_USER'), | ||
pass: config.get('MAIL_PASSWORD'), | ||
}, | ||
}, | ||
defaults: { | ||
from: `"Your App" <${config.get<string>('MAIL_FROM')}>`, | ||
}, | ||
template: { | ||
dir: join(__dirname, 'templates'), | ||
adapter: new HandlebarsAdapter(), | ||
options: { | ||
strict: true, | ||
}, | ||
}, | ||
}), | ||
inject: [ConfigService], | ||
}), | ||
JwtModule, | ||
], | ||
providers: [MailService], | ||
exports: [MailService], | ||
}) | ||
export class MailModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { MailerService } from '@nestjs-modules/mailer'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { User } from 'src/user/user.model'; | ||
import { Repository } from 'typeorm'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
@Injectable() | ||
export class MailService { | ||
constructor( | ||
@InjectRepository(User) | ||
private userRepository: Repository<User>, | ||
private readonly mailerService: MailerService, | ||
private configService: ConfigService, | ||
) {} | ||
|
||
async sendConfirmationEmail(email: string, token: string) { | ||
const confirmUrl = `https://${this.configService.get('MAIL_DOMAIN')}/auth/confirm?token=${token}`; | ||
|
||
await this.mailerService.sendMail({ | ||
to: email, | ||
subject: 'Confirm Your Email', | ||
template: './confirmation', | ||
context: { confirmUrl }, // Data for template | ||
}); | ||
} | ||
|
||
async sendPasswordResetEmail(user: User, token: string) { | ||
const frontendUrl = this.configService.get('FRONTEND_URL'); | ||
const url = `${frontendUrl}/reset-password?token=${token}`; | ||
|
||
await this.mailerService.sendMail({ | ||
to: user.email, | ||
subject: 'Password Reset Request', | ||
template: './passwordReset', | ||
context: { | ||
name: user.username, | ||
firstName: user.username, | ||
url, | ||
}, | ||
}); | ||
} | ||
|
||
// async sendConfirmationEmail(user: User, token: string) { | ||
// const frontendUrl = this.configService.get('FRONTEND_URL'); | ||
// const url = `${frontendUrl}/confirm-email?token=${token}`; | ||
|
||
// await this.mailerService.sendMail({ | ||
// to: user.email, | ||
// subject: 'Welcome! Confirm Your Email', | ||
// template: './confirmation', // This will use the confirmation.hbs template | ||
// context: { // Data to be sent to the template | ||
// name: user.username, | ||
// firstName: user.firstName || user.username, | ||
// url, | ||
// }, | ||
// }); | ||
// } | ||
} |
Oops, something went wrong.