diff --git a/sample.env b/sample.env index c463522d..ab53b85b 100644 --- a/sample.env +++ b/sample.env @@ -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} diff --git a/src/common/config/configuration.ts b/src/common/config/configuration.ts index 63a0b4ff..4ed2a86f 100644 --- a/src/common/config/configuration.ts +++ b/src/common/config/configuration.ts @@ -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=', }; }; diff --git a/src/email/email.module.ts b/src/email/email.module.ts index 36a0a8f8..35e4362b 100644 --- a/src/email/email.module.ts +++ b/src/email/email.module.ts @@ -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: [ @@ -40,6 +41,7 @@ import { EmailService } from './email.service'; }, }, }), + ConfigModule, ], providers: [EmailService, EmailRuleService], exports: [EmailService, EmailRuleService], diff --git a/src/email/email.service.ts b/src/email/email.service.ts index 195d81e7..708b7315 100644 --- a/src/email/email.service.ts +++ b/src/email/email.service.ts @@ -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( @@ -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, }, }); } diff --git a/src/resources/email-templates/password-reset.english.hbs b/src/resources/email-templates/password-reset.english.hbs index b3290e78..f1e5bbd4 100644 --- a/src/resources/email-templates/password-reset.english.hbs +++ b/src/resources/email-templates/password-reset.english.hbs @@ -1,12 +1,3 @@
Hello, {{ username }}. You are trying to reset your password.
-- Here should be a link, so that if you click it, - you will be directed to a page to reset your password. -
-- Unfortunatelly, it is not implemented, but here is your password reset token: {{ token }}. -
-- Use postman to post a request to (backend)/users/recover/password/verify with body {"token": "{{token}}", "new_password": "<new password>"}. -
-I'm sure you are a super great developer, so you know how to do it.
+Please use the following link to reset your password:
+