Skip to content

Commit

Permalink
Merge e7acf1e into 9c336f9
Browse files Browse the repository at this point in the history
  • Loading branch information
zanio committed Aug 22, 2019
2 parents 9c336f9 + e7acf1e commit d9ed001
Show file tree
Hide file tree
Showing 14 changed files with 727 additions and 91 deletions.
9 changes: 9 additions & 0 deletions .env
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
7 changes: 0 additions & 7 deletions .env.example

This file was deleted.

3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Created by https://www.gitignore.io/api/node
# Edit at https://www.gitignore.io/?templates=node

Expand Down Expand Up @@ -72,7 +71,7 @@ typings/
.yarn-integrity

# dotenv environment variables file
.env

.env.test

# parcel-bundler cache (https://parceljs.org/)
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
## Vision

Make company global travel and accommodation easy and convenient for the strong workforce of savvy members of staff, by leveraging the modern web.

---

[![Build Status](https://travis-ci.com/andela/firestar-backend.svg?branch=stage)](https://travis-ci.com/andela/firestar-backend) [![Coverage Status](https://coveralls.io/repos/github/andela/firestar-backend/badge.svg?branch=coveralls)](https://coveralls.io/github/andela/firestar-backend?branch=coveralls) [![](https://img.shields.io/badge/Protected_by-Hound-a873d1.svg)](https://houndci.com)
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "A Social platform for the creative at heart",
"main": "index.js",
"scripts": {
"test": "cross-env NODE_ENV=development nyc mocha --require @babel/register ./src/__tests__/*.js",
"test": "cross-env NODE_ENV=development nyc mocha --require @babel/register --require @babel/polyfill -r dotenv/config ./src/__tests__/*.js --exit",
"coverage": "nyc report --reporter=text",
"coveralls": "nyc report --reporter=text-lcov | coveralls",
"lint": "eslint \"**/*.js\" --ignore-pattern node_modules/ --fix",
Expand Down Expand Up @@ -40,6 +40,7 @@
"method-override": "^2.3.10",
"methods": "^1.1.2",
"morgan": "^1.9.1",
"nodemailer": "^6.3.0",
"passport": "^0.4.0",
"passport-local": "^1.0.0",
"pg": "^7.12.1",
Expand All @@ -59,13 +60,16 @@
"@babel/plugin-proposal-optional-chaining": "^7.2.0",
"@babel/preset-env": "^7.5.5",
"chai": "^4.2.0",
"chai-http": "^4.3.0",
"coveralls": "^3.0.6",
"eslint": "^6.1.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-plugin-import": "^2.18.2",
"mocha": "^6.2.0",
"mocha-lcov-reporter": "^1.3.0",
"nodemon": "^1.19.1",
"nyc": "^14.1.1"
"nyc": "^14.1.1",
"sinon": "^7.4.1",
"sinon-chai": "^3.3.0"
}
}
}
2 changes: 1 addition & 1 deletion src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { equal } from 'assert';
import arrayTest from '../utils/index';
import { arrayTest } from '../utils/index';

describe('Array', () => {
describe('#indexOf()', () => {
Expand Down
33 changes: 33 additions & 0 deletions src/__tests__/mail.js
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();
});
});
});

20 changes: 20 additions & 0 deletions src/controllers/user.js
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;
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,5 @@ app.use((err, req, res, next) => {
const server = app.listen(process.env.PORT || 3000, () => {
serverLog(`Listening on port ${server.address().port}`);
});

export default app;
36 changes: 36 additions & 0 deletions src/middlewares/mail.js
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;
79 changes: 3 additions & 76 deletions src/routes/api/users.js
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;
56 changes: 56 additions & 0 deletions src/services/mail/Mail.js
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 };
Loading

0 comments on commit d9ed001

Please sign in to comment.