-
Notifications
You must be signed in to change notification settings - Fork 0
Hw06 email #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Hw06 email #4
Changes from all commits
1478601
19abc55
20c9ea6
2d31825
d587ae4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"); | ||
|
|
||
|
|
@@ -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 = { | ||
| 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, | ||
|
|
@@ -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"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"); | ||
|
|
@@ -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); | ||
|
|
@@ -113,6 +163,8 @@ const updateAvatar = async (req, res, next) => { | |
|
|
||
| module.exports = { | ||
| register, | ||
| verifyEmail, | ||
| resendVerifyEmail, | ||
| login, | ||
| getCurrent, | ||
| logout, | ||
|
|
||
| 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Zmieniłbym to np na msgOpt lub opt.