Skip to content

Commit

Permalink
Merge 9cee59d into 79f5bc5
Browse files Browse the repository at this point in the history
  • Loading branch information
Akinmyde committed Apr 4, 2019
2 parents 79f5bc5 + 9cee59d commit 346be19
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
38 changes: 37 additions & 1 deletion server/controllers/auth.controller.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import db from '../models';
import authHelper from '../helpers/auth';
import searchDatabase from '../helpers/searchDatabase';
import emailSender from '../helpers/emailSender';

const { findUser } = searchDatabase;
const { User } = db;
const error = ['invalid username and/or password'];
const serverError = {
Expand Down Expand Up @@ -79,6 +82,13 @@ const signupController = async (req, res) => {
const { id, is_admin: isAdmin, bio, image_url: image } = user;
const token = authHelper.encode({ id, email, isAdmin });

const verificationToken = authHelper.encode({ email });
const verificationLink = `${req.protocol}://${req.get(
'host'
)}/api/v1/auth/verification/${verificationToken}`;

await emailSender.signupEmail(email, verificationLink);

return res.send({
status: 200,
user: { email, token, bio, image },
Expand All @@ -89,6 +99,32 @@ const signupController = async (req, res) => {
}
};

const authController = { loginController, signupController };
const verifyEmail = async (req, res) => {
try {
const { token } = req.params;
const decodedToken = authHelper.decode(token);
const { email } = decodedToken.userObj;

const user = await findUser(email);
if (user.is_activated) {
return res.send({
status: 403,
errors: {
body: ['Your account has already been verified'],
},
});
}
const updateValue = { is_activated: true };
await user.update(updateValue);
return res.send({
status: 200,
message: 'Account verification was successful',
});
} catch (err) {
return res.send(serverError);
}
};

const authController = { loginController, signupController, verifyEmail };

export default authController;
11 changes: 11 additions & 0 deletions server/helpers/emailSender.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sendEmail from '../config/email';

const signupEmail = (email, link) => {
const titile = 'Welcome to Authors Haven';
const body = `<p>click <a href=${link}>here</a> to confirm your email</p>`;
sendEmail(email, titile, body);
};

const emailSender = { signupEmail };

export default emailSender;
2 changes: 2 additions & 0 deletions server/routes/user.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ authRoute.post(
authController.signupController
);

authRoute.patch('/verification/:token', authController.verifyEmail);

export default authRoute;
36 changes: 35 additions & 1 deletion tests/auth.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import app from '../server/app';
import authHelper from '../server/helpers/auth';

const baseUrl = '/api/v1/auth';

Expand Down Expand Up @@ -79,7 +80,7 @@ describe('SIGNUP CONTROLLER TEST', () => {
.send({
firstname: 'Mr test',
lastname: 'tester',
email: 'test@gmail.com',
email: 'test@test.com',
password: 'testing1',
confirmPassword: 'testing1',
})
Expand All @@ -94,3 +95,36 @@ describe('SIGNUP CONTROLLER TEST', () => {
});
});
});

describe('EMAIL VERIFICATION TEST', () => {
const token = authHelper.encode({ email: 'test@test.com' });
it('should verify a user', done => {
chai
.request(app)
.patch(`${baseUrl}/verification/${token}`)
.end((_err, res) => {
const { status, message } = res.body;
expect(status).to.be.equal(200);
expect(res).to.be.a('object');
expect(res.body).to.have.keys('status', 'message');
expect(message).to.be.equal('Account verification was successful');
done();
});
});
it('should return an a user', done => {
chai
.request(app)
.patch(`${baseUrl}/verification/${token}`)
.end((err, res) => {
const { status, errors } = res.body;
expect(status).to.be.equal(403);
expect(res.body).to.have.keys('status', 'errors');
expect(errors).to.have.keys('body');
expect(errors.body).to.be.a('array');
expect(errors.body[0]).to.be.equal(
'Your account has already been verified'
);
done();
});
});
});

0 comments on commit 346be19

Please sign in to comment.