-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
727 additions
and
91 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
DB_USERNAME=postgres | ||
DB_PASSWORD=db_password | ||
DB_NAME=barefoot_nomad | ||
DB_HOST=127.0.0.1 | ||
DB_TEST_NAME=barefoot_test | ||
email = firestarbackend@gmail.com | ||
layer = Gmail | ||
password = fireSTARS2019&& | ||
SECRET_KEY_EMAIL_VERIFY_TOKEN = asghhfhfhg3636666shfhgd434 |
This file was deleted.
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
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
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,33 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import sinon from 'sinon'; | ||
import sinonChai from 'sinon-chai'; | ||
import app from '../index'; | ||
|
||
|
||
chai.use(chaiHttp); | ||
chai.use(sinonChai); | ||
|
||
const { expect } = chai; | ||
|
||
describe('/EMAIL VERIFICATION ROUTE', () => { | ||
it('It shoud return 200 success message', function (done) { | ||
this.timeout(20000); | ||
|
||
|
||
const body = { | ||
email: 'akp.ani@yahoo.com', | ||
firstName: 'Aniefiok', | ||
lastName: 'Akpan' | ||
}; | ||
chai.request(app) | ||
.post('/api/email-test') | ||
.send(body) | ||
.end((err, res) => { | ||
expect(res.body.status).to.equal(200); | ||
expect(res.body).to.be.a('object'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
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,20 @@ | ||
|
||
const signUp = async (req, res) => { | ||
const { verificationMailResponse } = req; | ||
|
||
try { | ||
return res.status(200).json({ | ||
status: 200, | ||
data: { | ||
message: | ||
'Message successfully sent, please check your email', | ||
verificationMailResponse | ||
} | ||
}); | ||
} catch (err) { | ||
return res.status(500).json({ status: 500, error: 'An Internal Error occured during the process.' }); | ||
} | ||
}; | ||
|
||
|
||
export default signUp; |
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,36 @@ | ||
import { verifyEmail } from '../services/mail/template/verifyEmail'; | ||
import { emailVerifyToken } from '../utils/index'; | ||
import { Mail } from '../services/mail/Mail'; | ||
|
||
const SendVerificationEmail = async (req, res, next) => { | ||
let { email, firstName, lastName } = req.body; | ||
email = email ? email.trim() : ''; | ||
firstName = firstName.trim(); | ||
lastName = lastName.trim(); | ||
const id = 'some_encoded_identiity'; | ||
const token = await emailVerifyToken(id); | ||
const emaildDetails = { | ||
Subject: 'Email Verification', | ||
Recipient: email, | ||
}; | ||
const link = `http://localhost:3000/api/v1/auth/verify?id=${token}`; | ||
const data = { | ||
email, | ||
firstName, | ||
lastName, | ||
link, | ||
}; | ||
|
||
try { | ||
const send = new Mail(emaildDetails, verifyEmail(data)); | ||
const { response } = await send.main(); | ||
if (response) { | ||
req.verificationMailResponse = response; | ||
next(); | ||
} | ||
} catch (err) { | ||
return res.status(400).json({ status: 400, error: err.message }); | ||
} | ||
}; | ||
|
||
export default SendVerificationEmail; |
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,82 +1,9 @@ | ||
import { Router } from 'express'; | ||
import passport from 'passport'; | ||
import SendVerificationEmail from '../../middlewares/mail'; | ||
import signUp from '../../controllers/user'; | ||
|
||
const router = Router(); | ||
|
||
router.get('/user', (req, res, next) => { | ||
User.findById(req.payload.id) | ||
.then((user) => { | ||
if (!user) { | ||
return res.sendStatus(401); | ||
} | ||
return res.json({ user: user.toAuthJSON() }); | ||
}) | ||
.catch(next); | ||
}); | ||
|
||
router.put('/user', (req, res, next) => { | ||
User.findById(req.payload.id) | ||
.then((user) => { | ||
if (!user) { | ||
return res.sendStatus(401); | ||
} | ||
|
||
// only update fields that were actually passed... | ||
if (typeof req.body.user.username !== 'undefined') { | ||
user.username = req.body.user.username; | ||
} | ||
if (typeof req.body.user.email !== 'undefined') { | ||
user.email = req.body.user.email; | ||
} | ||
if (typeof req.body.user.bio !== 'undefined') { | ||
user.bio = req.body.user.bio; | ||
} | ||
if (typeof req.body.user.image !== 'undefined') { | ||
user.image = req.body.user.image; | ||
} | ||
if (typeof req.body.user.password !== 'undefined') { | ||
user.setPassword(req.body.user.password); | ||
} | ||
|
||
return user.save().then(() => res.json({ user: user.toAuthJSON() })); | ||
}) | ||
.catch(next); | ||
}); | ||
|
||
router.post('/users/login', (req, res, next) => { | ||
if (!req.body.user.email) { | ||
return res.status(422).json({ errors: { email: "can't be blank" } }); | ||
} | ||
|
||
if (!req.body.user.password) { | ||
return res.status(422).json({ errors: { password: "can't be blank" } }); | ||
} | ||
passport.authenticate('local', { session: false }, ( | ||
err, | ||
user, | ||
info | ||
) => { | ||
if (err) { | ||
return next(err); | ||
} | ||
|
||
if (user) { | ||
return res.json({ user: user.toAuthJSON() }); | ||
} | ||
return res.status(422).json(info); | ||
})(req, res, next); | ||
}); | ||
|
||
router.post('/users', (req, res, next) => { | ||
const user = new User(); | ||
|
||
user.username = req.body.user.username; | ||
user.email = req.body.user.email; | ||
user.setPassword(req.body.user.password); | ||
|
||
user.save() | ||
.then(() => res.json({ user: user.toAuthJSON() })) | ||
.catch(next); | ||
}); | ||
router.post('/email-test', SendVerificationEmail, signUp); | ||
|
||
export default router; |
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,56 @@ | ||
/* eslint-disable import/prefer-default-export, no-console, require-jsdoc */ | ||
|
||
import nodemailer from 'nodemailer'; | ||
import dotenv from 'dotenv'; | ||
|
||
dotenv.config(); | ||
|
||
|
||
class Mail { | ||
constructor(emailDetails, html) { | ||
this.subject = emailDetails.Subject; | ||
this.recipient = emailDetails.Recipient; | ||
this.content = html; | ||
this.Recipients = this.formatRecipients(); | ||
} | ||
|
||
// When sending to multiple users; | ||
formatRecipients() { | ||
const array = this.recipient.split(','); | ||
array.map((el) => el.trim()); | ||
return array; | ||
} | ||
|
||
|
||
async main() { | ||
const { subject, recipient, content } = this; | ||
// create reusable transporter object using the default SMTP transport | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; | ||
const transporter = nodemailer.createTransport({ | ||
service: process.env.layer, | ||
auth: { | ||
user: process.env.email, | ||
pass: process.env.password, | ||
}, | ||
}); | ||
|
||
|
||
const mailOptions = { | ||
from: `"Firestar-Backend" <${process.env.email}>`, | ||
to: recipient, | ||
subject, | ||
html: content, | ||
}; | ||
|
||
try { | ||
const response = await transporter.sendMail(mailOptions); | ||
|
||
return response; | ||
} catch (error) { | ||
return error.message; | ||
} | ||
} | ||
} | ||
|
||
|
||
export { Mail }; |
Oops, something went wrong.