password reset
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import Restaurant from "../models/restaurant.js";
|
||||
import Dish from "../models/dish.js";
|
||||
import User from "../models/users.js";
|
||||
import mongoose from "mongoose";
|
||||
import sanitizer from "string-sanitizer";
|
||||
import { newError } from "./services.js";
|
||||
|
||||
export async function changeUserPass(userId, newPass) {
|
||||
|
||||
@@ -1,2 +1,47 @@
|
||||
import nodemailer from "nodemailer";
|
||||
import makeResetPassMessage from "../config/mailTemplateReset";
|
||||
import path from "path";
|
||||
import { MAIL_PASS } from "../config/index.js";
|
||||
import makeResetPassMessage from "../config/mailTemplateReset.js";
|
||||
import { newError, generatePasswordResetLink } from "../services/services.js";
|
||||
|
||||
const images = path.resolve("images");
|
||||
|
||||
async function sendMail(reciever, subject, textMessage, htmlMessage) {
|
||||
let transporter = nodemailer.createTransport({
|
||||
host: "smtp.dpoczta.pl",
|
||||
port: 587,
|
||||
secure: false,
|
||||
auth: {
|
||||
user: "noreply@menui.pl",
|
||||
pass: MAIL_PASS,
|
||||
},
|
||||
});
|
||||
|
||||
let info = await transporter.sendMail({
|
||||
from: '"Menui" <noreply@menui.pl>',
|
||||
to: reciever,
|
||||
subject: subject,
|
||||
text: textMessage,
|
||||
html: htmlMessage,
|
||||
attachments: [
|
||||
{
|
||||
filename: "logo.svg",
|
||||
path: images + "/logo.svg",
|
||||
cid: "logo",
|
||||
},
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
export async function resetPassword(email) {
|
||||
const resetLink = generatePasswordResetLink(email);
|
||||
const message = makeResetPassMessage(resetLink);
|
||||
await sendMail(
|
||||
email,
|
||||
"Menui - Resetowanie hasła",
|
||||
message.text,
|
||||
message.html
|
||||
).catch((err) => {
|
||||
throw newError("Nieznany błąd podczas resetu hasła", 500);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import Restaurant from "../models/restaurant.js";
|
||||
import {} from "./dataPrepServices.js";
|
||||
import crypto from "crypto";
|
||||
import Dish from "../models/dish.js";
|
||||
import User from "../models/users.js";
|
||||
import mongoose from "mongoose";
|
||||
@@ -59,6 +59,22 @@ export function generateAuthToken(user) {
|
||||
return token;
|
||||
}
|
||||
|
||||
function generatePasswordResetToken(email) {
|
||||
const token = jwt.sign(
|
||||
{
|
||||
email: email,
|
||||
},
|
||||
jwtSecret,
|
||||
{ expiresIn: "15m" }
|
||||
);
|
||||
return token;
|
||||
}
|
||||
|
||||
export function generatePasswordResetLink(email) {
|
||||
const token = generatePasswordResetToken(email);
|
||||
const link = `htt`;
|
||||
}
|
||||
|
||||
export async function checkEmailTaken(email) {
|
||||
if (!email) throw newError("No input email", 204);
|
||||
await User.exists({ email: email }).then((res) => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { validateRestaurant, toShortDate } from "./services";
|
||||
import { toShortDate, generateNewPassword } from "./services";
|
||||
|
||||
jest.mock("@azure/storage-blob", () => {
|
||||
return {
|
||||
@@ -17,3 +17,8 @@ jest.mock("bcrypt", () => {
|
||||
test("should return false for no date on input", () => {
|
||||
expect(toShortDate()).toBe(false);
|
||||
});
|
||||
|
||||
test("should generate random 10 characters long password", () => {
|
||||
let generatedPass = generateNewPassword();
|
||||
expect(generatedPass.length).toBe(10);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user