Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 57 additions & 5 deletions controllers/authControll.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const bcrypt = require("bcrypt");
const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");
const { User } = require("../schemas/authSchema");
const { HttpError } = require("../helpers/HttpError");
const { SECRET_KEY } = process.env;
const gravatar = require("gravatar");
const path = require("path");
const fs = require("fs/promises");
const jimp = require("jimp");
const { nanoid } = require("nanoid");
const { User } = require("../schemas/authSchema");
const HttpError = require("../helpers/HttpError");
const sendEmail = require("../helpers/sendEmail");
const { SECRET_KEY } = process.env;

const avatarsDir = path.join(__dirname, "../", "public", "avatars");

Expand All @@ -19,12 +21,20 @@ const register = async (req, res) => {
}
const hashPassword = await bcrypt.hash(password, 10);
const avatarURL = gravatar.url(email);
const verificationToken = nanoid();

const newUser = await User.create({
...req.body,
password: hashPassword,
avatarURL,
verificationToken,
});
const verifyEmail = {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const verifyEmail = {
const msgOpt = {

Zmieniłbym to np na msgOpt lub opt.

to: email,
subject: "Verify email",
html: `<a target="_blank" href="http://localhost:3000/api/user/verify/${verificationToken}">Click verify email</a>`,
};
await sendEmail(verifyEmail);

res.status(201).json({
name: newUser.name,
Expand All @@ -33,12 +43,53 @@ const register = async (req, res) => {
});
};

const verifyEmail = async (req, res) => {
const { verificationToken } = req.params;
const user = await User.findOne({ verificationToken });
if (!user) {
throw HttpError(404, "User not found");
}

await User.findByIdAndUpdate(user._id, {
verify: true,
verificationToken: "",
});

res.status(200).json({ message: "Verification successful" });
};

const resendVerifyEmail = async (req, res) => {
const { email } = req.body;
const user = await User.findOne({ email });
if (!user) {
throw HttpError(404, "User not found");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zamiast throw w tych najwyższych ścieżkach trzeba zwrócić błąd użytkownikowi. Bo zastanawiam się co przechwyci ten throw? Jeśli robisz tutaj throw to znaczy że gdzieś wyżej, czyli w miejscu gdzie jest użyta funkcja resendVerifyEmail jest blok catch. Jeśli tego bloku nie ma, to aplikacja przestanie działać.

I to się tyczy wielu miejsc.

}

if (user.verify) {
throw HttpError(400, "Verification has already been passed");
}

const verifyEmail = {
to: email,
subject: "Verify email",
html: `<a target="_blank" href="http://localhost:3000/api/user/verify/${user.verificationToken}">Click verify email</a>`,
};

await sendEmail(verifyEmail);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Te najwyższe funkcje async powinny być otoczone w jakimś try catch statement aby obsługiwać błędy które pojawiają się w kodzie.


res.status(200).json({ message: "Verification email sent" });
};

const login = async (req, res) => {
const { email, password } = req.body;
const user = await User.findOne({ email });
if (!user) {
throw HttpError(401, "Email or password is wrong");
}
if (!user.verify) {
throw HttpError(404, "User not found");
}

const passwordCompare = await bcrypt.compare(password, user.password);
if (!passwordCompare) {
throw HttpError(401, "Email or password is wrong");
Expand Down Expand Up @@ -82,7 +133,6 @@ const logout = async (req, res) => {

const updateAvatar = async (req, res, next) => {
const { path: tempUpload } = req.file;
// const { path: tempUpload } = req.file;
try {
const avatar = await jimp.read(tempUpload);
await avatar.resize(250, 250).write(tempUpload);
Expand Down Expand Up @@ -113,6 +163,8 @@ const updateAvatar = async (req, res, next) => {

module.exports = {
register,
verifyEmail,
resendVerifyEmail,
login,
getCurrent,
logout,
Expand Down
13 changes: 13 additions & 0 deletions helpers/sendEmail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const sgMail = require("@sendgrid/mail");
require("dotenv").config();

const { SENDGRID_API_KEY } = process.env;

sgMail.setApiKey(SENDGRID_API_KEY);

const sendEmail = async (data) => {
const email = { ...data, from: "agap454@gmail.com" };
await sgMail.send(email);
return true;
};
module.exports = sendEmail;
Loading