Implementes mail sending on pw reset request

ref #118
This commit is contained in:
Nicolai Ort 2021-01-26 17:35:03 +01:00
parent d02e9dec56
commit e26744b792
2 changed files with 14 additions and 8 deletions

View File

@ -2,17 +2,23 @@ import { Body, CookieParam, JsonController, Param, Post, Req, Res } from 'routin
import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi';
import { IllegalJWTError, InvalidCredentialsError, JwtNotProvidedError, PasswordNeededError, RefreshTokenCountInvalidError, UsernameOrEmailNeededError } from '../errors/AuthError'; import { IllegalJWTError, InvalidCredentialsError, JwtNotProvidedError, PasswordNeededError, RefreshTokenCountInvalidError, UsernameOrEmailNeededError } from '../errors/AuthError';
import { UserNotFoundError } from '../errors/UserErrors'; import { UserNotFoundError } from '../errors/UserErrors';
import { Mailer } from '../mailer';
import { CreateAuth } from '../models/actions/create/CreateAuth'; import { CreateAuth } from '../models/actions/create/CreateAuth';
import { CreateResetToken } from '../models/actions/create/CreateResetToken'; import { CreateResetToken } from '../models/actions/create/CreateResetToken';
import { HandleLogout } from '../models/actions/HandleLogout'; import { HandleLogout } from '../models/actions/HandleLogout';
import { RefreshAuth } from '../models/actions/RefreshAuth'; import { RefreshAuth } from '../models/actions/RefreshAuth';
import { ResetPassword } from '../models/actions/ResetPassword'; import { ResetPassword } from '../models/actions/ResetPassword';
import { ResponseAuth } from '../models/responses/ResponseAuth'; import { ResponseAuth } from '../models/responses/ResponseAuth';
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
import { Logout } from '../models/responses/ResponseLogout'; import { Logout } from '../models/responses/ResponseLogout';
@JsonController('/auth') @JsonController('/auth')
export class AuthController { export class AuthController {
private mailer: Mailer;
constructor() { constructor() {
this.mailer = new Mailer();
} }
@Post("/login") @Post("/login")
@ -82,13 +88,14 @@ export class AuthController {
} }
@Post("/reset") @Post("/reset")
@ResponseSchema(ResponseAuth) @ResponseSchema(ResponseEmpty, { statusCode: 200 })
@ResponseSchema(UserNotFoundError) @ResponseSchema(UserNotFoundError, { statusCode: 404 })
@ResponseSchema(UsernameOrEmailNeededError) @ResponseSchema(UsernameOrEmailNeededError, { statusCode: 406 })
@OpenAPI({ description: "Request a password reset token. <br> This will provide you with a reset token that you can use by posting to /api/auth/reset/{token}." }) @OpenAPI({ description: "Request a password reset token. <br> This will provide you with a reset token that you can use by posting to /api/auth/reset/{token}." })
async getResetToken(@Body({ validate: true }) passwordReset: CreateResetToken) { async getResetToken(@Body({ validate: true }) passwordReset: CreateResetToken) {
//This really shouldn't just get returned, but sent via mail or sth like that. But for dev only this is fine. const reset_token: String = await passwordReset.toResetToken();
return { "resetToken": await passwordReset.toResetToken() }; await this.mailer.sendResetMail(passwordReset.email, reset_token);
return new ResponseEmpty();
} }
@Post("/reset/:token") @Post("/reset/:token")

View File

@ -3,7 +3,6 @@ import { MailOptions } from 'nodemailer/lib/json-transport';
import Mail from 'nodemailer/lib/mailer'; import Mail from 'nodemailer/lib/mailer';
import { config } from './config'; import { config } from './config';
import { MailServerConfigError } from './errors/MailErrors'; import { MailServerConfigError } from './errors/MailErrors';
import { User } from './models/entities/User';
/** /**
* This class is responsible for all things mail sending. * This class is responsible for all things mail sending.
*/ */
@ -27,10 +26,10 @@ export class Mailer {
}); });
} }
public async sendResetMail(user: User, token: String) { public async sendResetMail(to_address: string, token: String) {
const reset_link = `${config.app_url}/reset/${token}` const reset_link = `${config.app_url}/reset/${token}`
const mail: MailOptions = { const mail: MailOptions = {
to: user.email, to: to_address,
subject: "LfK! Password Reset", subject: "LfK! Password Reset",
html: `<b>${reset_link}</b>` html: `<b>${reset_link}</b>`
}; };