Skip to content

Commit

Permalink
feat: reset password email
Browse files Browse the repository at this point in the history
  • Loading branch information
Nictheboy committed Nov 14, 2024
1 parent 725c086 commit 2c8c084
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
8 changes: 8 additions & 0 deletions sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ CORS_METHODS=GET,POST,PUT,PATCH,DELETE
CORS_HEADERS=Content-Type,Authorization
CORS_CREDENTIALS=true

# This url means the frontend url, usually it is the same as the CORS_ORIGINS
# It is used to send the password reset email
FRONTEND_BASE_URL=http://localhost:3000

# The prefix of the password reset link in the email
# This prefix will be appended to FRONTEND_BASE_URL
PASSWORD_RESET_PREFIX=/account/recover/password/verify?token=

# additionally setup the following if you want to use docker-compose
# to setup environment
POSTGRES_DB=${DB_NAME}
Expand Down
4 changes: 4 additions & 0 deletions src/common/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export default () => {
// expiresIn: process.env.JWT_EXPIRES_IN,
},
cookieBasePath: process.env.COOKIE_BASE_PATH || '/',
frontendBaseUrl: process.env.FRONTEND_BASE_URL || '',
passwordResetPath:
process.env.PASSWORD_RESET_PREFIX ||
'/account/recover/password/verify?token=',
};
};

Expand Down
2 changes: 2 additions & 0 deletions src/email/email.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Module } from '@nestjs/common';
import { join } from 'path';
import { EmailRuleService } from './email-rule.service';
import { EmailService } from './email.service';
import { ConfigModule } from '@nestjs/config';

@Module({
imports: [
Expand Down Expand Up @@ -40,6 +41,7 @@ import { EmailService } from './email.service';
},
},
}),
ConfigModule,
],
providers: [EmailService, EmailRuleService],
exports: [EmailService, EmailRuleService],
Expand Down
7 changes: 6 additions & 1 deletion src/email/email.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
import { MailerService } from '@nestjs-modules/mailer';
import { Injectable } from '@nestjs/common';
import { EmailRuleService } from './email-rule.service';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class EmailService {
constructor(
private readonly mailerService: MailerService,
private readonly emailRuleService: EmailRuleService,
private readonly configService: ConfigService,
) {}

async sendPasswordResetEmail(
Expand All @@ -30,7 +32,10 @@ export class EmailService {
template: './password-reset.english.hbs',
context: {
username,
token,
resetUrl:
this.configService.get('frontendBaseUrl') +
this.configService.get('passwordResetPath') +
token,
},
});
}
Expand Down
13 changes: 2 additions & 11 deletions src/resources/email-templates/password-reset.english.hbs
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
<p>Hello, {{ username }}. You are trying to reset your password.</p>
<p>
Here should be a link, so that if you click it,
you will be directed to a page to reset your password.
</p>
<p>
Unfortunatelly, it is not implemented, but here is your password reset token: {{ token }}.
</p>
<p>
Use postman to post a request to (backend)/users/recover/password/verify with body {"token": "{{token}}", "new_password": "&lt;new password&gt;"}.
</p>
<p>I'm sure you are a super great developer, so you know how to do it.</p>
<p>Please use the following link to reset your password:</p>
<p><a href="{{ resetUrl }}">{{ resetUrl }}</a></p>

0 comments on commit 2c8c084

Please sign in to comment.