generated from agiledev-students-fall2023/generic-project-repository
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from agiledev-students-fall2023/charlotte
SendRecoveryEmail done
- Loading branch information
Showing
8 changed files
with
168 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,5 @@ SESSION_SECRET = 'myKey123!' | |
JWT_SECRET = 'myKey123!' | ||
CLIENT_URL = "http://localhost:5173" | ||
MONGODB_URI = "mongodb+srv://yz5835:[email protected]/bakerdb" | ||
EMAIL_USER='[email protected]' | ||
EMAIL_PASS='Goodoldmap123' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,39 @@ | ||
//to-do add recovery page, now we only send token to client's email | ||
import User from '../models/User.mjs'; | ||
import bcrypt from 'bcryptjs'; | ||
import jwt from 'jsonwebtoken'; | ||
import sendRecoveryEmail from './sendEmail.mjs'; // Your email sending function | ||
import sendRecoveryEmail from './sendEmail.mjs'; | ||
|
||
const forgetpasswordRouter = async (req, res) => { | ||
const { email } = req.body; | ||
|
||
console.log("Received forgot password request for email:", email); | ||
|
||
try { | ||
const user = await User.findOne({ email: email.toLowerCase() }); | ||
console.log("User found:", user); | ||
|
||
if (!user) { | ||
console.log("User not found for email:", email); | ||
return res.status(404).json({ message: "User not found." }); | ||
} | ||
|
||
// Generate a token for password reset | ||
const resetToken = jwt.sign({ id: user.id, email: user.email }, process.env.JWT_SECRET, { expiresIn: "1h" }); | ||
|
||
// Store the token in the database (you'll need to implement this logic) | ||
// E.g., user.resetToken = resetToken; await user.save(); | ||
|
||
// Send the token to the user's email | ||
// Store the reset token in the user's record in the database | ||
user.resetToken = resetToken; | ||
await user.save(); | ||
|
||
await sendRecoveryEmail(email, resetToken); | ||
console.log("Password reset email sent to:", email); | ||
|
||
return res.status(200).json({ message: "Password reset email sent." }); | ||
} catch (error) { | ||
console.error(error); | ||
console.error("Error in /forgotpassword route:", error); | ||
return res.status(500).json({ message: "Internal server error." }); | ||
} | ||
|
||
|
||
}; | ||
|
||
export default forgetpasswordRouter; | ||
|
||
// const forgetpasswordRouter = async (req, res) => { | ||
// // req.body: email, userID | ||
// // TODO: compare email to see if that match, | ||
// // if match, send recovery data to email | ||
// try { | ||
// return res.sendStatus(200) | ||
// } catch (error) { | ||
// } | ||
// } | ||
|
||
// export default forgetpasswordRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import nodemailer from 'nodemailer'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
const testEmailSending = async () => { | ||
let transporter = nodemailer.createTransport({ | ||
host: 'smtp.gmail.com', | ||
port: 587, | ||
secure: false, | ||
auth: { | ||
user: '[email protected]', | ||
pass: 'ebro wnec snzf fcqt', // Use environment variables to keep this secure | ||
}, | ||
}); | ||
|
||
let mailOptions = { | ||
from: `"The GoodOldMap" <${process.env.EMAIL_USER}>`, | ||
to: '[email protected]', // Replace with your test recipient email | ||
subject: 'Test Email', | ||
text: 'This is a test email from my Node.js application.', | ||
}; | ||
|
||
try { | ||
let info = await transporter.sendMail(mailOptions); | ||
console.log('Message sent: %s', info.messageId); | ||
} catch (error) { | ||
console.error('Error sending email:', error); | ||
} | ||
}; | ||
|
||
testEmailSending(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.