-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Trying to move my commits from Brian's account to my account
- Loading branch information
Showing
31 changed files
with
29,341 additions
and
5,844 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
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,50 @@ | ||
import nodemailer from 'nodemailer' | ||
|
||
// @desc Send email | ||
// @route POST /api/send-email | ||
// @access Public | ||
const sendMail = async (req, res) => { | ||
// Get the subject and message the user entered from the request body | ||
const subject = req.body['subject']['subject']; | ||
const message = req.body['subject']['message']; | ||
|
||
// Make sure all fields are filled in | ||
if (!subject || !message) { | ||
// If all fields are not filled in, show an error message | ||
return res.status(400).json({ message: 'All fields are required' }); | ||
} | ||
|
||
// Create reusable transporter object using the default SMTP transport | ||
var transporter = nodemailer.createTransport({ | ||
service: 'gmail', | ||
auth: { | ||
// Email address defined in .env file | ||
user: process.env.EMAIL_ADDRESS, | ||
// Email app password defined in .env file | ||
pass: process.env.EMAIL_PASSWORD, | ||
} | ||
}); | ||
|
||
// Set the email options | ||
var mailOptions = { | ||
from: process.env.EMAIL_ADDRESS, | ||
to: process.env.EMAIL_ADDRESS, | ||
// Set the subject to the subject the user entered on the form | ||
subject: subject, | ||
// Set the message to the message the user entered on the form | ||
text: message | ||
}; | ||
|
||
// Send the email | ||
transporter.sendMail(mailOptions, function(error, info){ | ||
if (error) { | ||
// Log error if failed | ||
console.log(error); | ||
} else { | ||
// Log the email was sent | ||
console.log('Email sent: ' + info.response); | ||
} | ||
}); | ||
} | ||
|
||
export { sendMail }; |
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,42 +1,45 @@ | ||
import jwt from 'jsonwebtoken' | ||
import asyncHandler from 'express-async-handler' | ||
import User from '../models/userModel.js' | ||
import asyncHandler from 'express-async-handler'; | ||
import jwt from 'jsonwebtoken'; | ||
import User from '../models/userModel.js'; | ||
|
||
const protect = asyncHandler(async (req, res, next) => { | ||
let token | ||
let token; | ||
|
||
if ( | ||
req.headers.authorization && | ||
req.headers.authorization.startsWith('Bearer') | ||
) { | ||
try { | ||
token = req.headers.authorization.split(' ')[1] | ||
token = req.headers.authorization.split(' ')[1]; | ||
|
||
const decoded = jwt.verify(token, process.env.JWT_SECRET) | ||
const decoded = jwt.verify(token, process.env.JWT_SECRET); | ||
|
||
req.user = await User.findById(decoded.id).select('-password') | ||
req.user = await User.findById(decoded.id).select('-password'); | ||
|
||
next() | ||
next(); | ||
} catch (error) { | ||
console.error(error) | ||
res.status(401) | ||
throw new Error('Not authorized, token failed') | ||
console.error(error); | ||
res.status(401); | ||
throw new Error('Not authorized, token failed'); | ||
} | ||
} | ||
|
||
if (!token) { | ||
res.status(401) | ||
throw new Error('Not authorized, no token') | ||
res.status(401); | ||
console.error('Not authorized, no token'); | ||
throw new Error('Not authorized, no token'); | ||
} | ||
}) | ||
}); | ||
|
||
const admin = (req, res, next) => { | ||
if (req.user && req.user.isAdmin) { | ||
next() | ||
next(); | ||
} else { | ||
res.status(401) | ||
throw new Error('Not authorized as an admin') | ||
res.status(401); | ||
console.error('user', req.user); | ||
console.error('Not authorized as an admin'); | ||
throw new Error('Not authorized as an admin'); | ||
} | ||
} | ||
}; | ||
|
||
export { protect, admin } | ||
export { admin, protect }; |
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,8 @@ | ||
import express from 'express'; | ||
const router = express.Router(); | ||
import { sendMail } from '../controllers/contactController.js'; | ||
|
||
// Add a route for the new endpoint using the Express router. | ||
router.route('/').post(sendMail); | ||
|
||
export default router; |
Oops, something went wrong.