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 { IllegalJWTError, InvalidCredentialsError, JwtNotProvidedError, PasswordNeededError, RefreshTokenCountInvalidError, UsernameOrEmailNeededError } from '../errors/AuthError';
import { UserNotFoundError } from '../errors/UserErrors';
import { Mailer } from '../mailer';
import { CreateAuth } from '../models/actions/create/CreateAuth';
import { CreateResetToken } from '../models/actions/create/CreateResetToken';
import { HandleLogout } from '../models/actions/HandleLogout';
import { RefreshAuth } from '../models/actions/RefreshAuth';
import { ResetPassword } from '../models/actions/ResetPassword';
import { ResponseAuth } from '../models/responses/ResponseAuth';
import { ResponseEmpty } from '../models/responses/ResponseEmpty';
import { Logout } from '../models/responses/ResponseLogout';
@JsonController('/auth')
export class AuthController {
private mailer: Mailer;
constructor() {
this.mailer = new Mailer();
}
@Post("/login")
@ -82,13 +88,14 @@ export class AuthController {
}
@Post("/reset")
@ResponseSchema(ResponseAuth)
@ResponseSchema(UserNotFoundError)
@ResponseSchema(UsernameOrEmailNeededError)
@ResponseSchema(ResponseEmpty, { statusCode: 200 })
@ResponseSchema(UserNotFoundError, { statusCode: 404 })
@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}." })
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.
return { "resetToken": await passwordReset.toResetToken() };
const reset_token: String = await passwordReset.toResetToken();
await this.mailer.sendResetMail(passwordReset.email, reset_token);
return new ResponseEmpty();
}
@Post("/reset/:token")

View File

@ -3,7 +3,6 @@ import { MailOptions } from 'nodemailer/lib/json-transport';
import Mail from 'nodemailer/lib/mailer';
import { config } from './config';
import { MailServerConfigError } from './errors/MailErrors';
import { User } from './models/entities/User';
/**
* 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 mail: MailOptions = {
to: user.email,
to: to_address,
subject: "LfK! Password Reset",
html: `<b>${reset_link}</b>`
};