-
Notifications
You must be signed in to change notification settings - Fork 0
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 #25 from jareerzeenam/dev
User Authentication, reset password improvements and email services added
- Loading branch information
Showing
5 changed files
with
146 additions
and
68 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,55 @@ | ||
const { User } = require('../models/User.model'); | ||
const bcrypt = require('bcryptjs'); | ||
const jwt = require('jsonwebtoken'); | ||
|
||
class UserRepository { | ||
async create(payload) { | ||
const username = payload.username; | ||
const email = payload.email; | ||
const password = payload.password; | ||
// Encrypt Password | ||
var encryptedPassword = await bcrypt.hash(password, 10); | ||
|
||
// Build out the mongoose model | ||
const newUser = new User({ | ||
username: username, | ||
email: email.toLowerCase(), | ||
password: encryptedPassword, | ||
}); | ||
|
||
// Create our JWT (attach to user model) | ||
const token = jwt.sign( | ||
{ | ||
user_id: newUser._id, | ||
email, | ||
}, | ||
process.env.JWT_HASH_TOKEN_KEY, | ||
{ | ||
expiresIn: '2h', | ||
} | ||
); | ||
newUser.token = token; | ||
|
||
// Save user to MongoDB | ||
const res = await newUser.save(); | ||
|
||
return { | ||
id: res.id, | ||
...res._doc, | ||
}; | ||
} | ||
|
||
/** | ||
* Find a user by email. | ||
* | ||
* @param {String} email - The email of the user. | ||
* @returns {User} | ||
*/ | ||
|
||
async findByEmail(email) { | ||
const user = await User.findOne({ email }); | ||
return user; | ||
} | ||
} | ||
|
||
module.exports = UserRepository; |
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,24 @@ | ||
const nodemailer = require('nodemailer'); | ||
|
||
const transporter = nodemailer.createTransport({ | ||
host: process.env.MAIL_HOST, | ||
port: process.env.MAIL_PORT, | ||
auth: { | ||
user: process.env.MAIL_USERNAME, | ||
pass: process.env.MAIL_PASSWORD, | ||
}, | ||
}); | ||
|
||
const sendEmail = async (to, subject, html) => { | ||
const mailOptions = { | ||
from: process.env.MAIL_FROM_ADDRESS, | ||
to, | ||
subject, | ||
html, | ||
}; | ||
await transporter.sendMail(mailOptions); | ||
}; | ||
|
||
module.exports = { | ||
sendEmail, | ||
}; |