Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test heroku review #12

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/controllers/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {

const Auth = {
async create(req, res) {
console.log(req.body);
const {
// eslint-disable-next-line camelcase
email, first_name, last_name, password, userType
Expand Down Expand Up @@ -48,10 +49,11 @@ const Auth = {
* @returns {object} user object
*/
async login(req, res) {
console.log(req.body);
const userQuery = 'SELECT * FROM Users WHERE email = $1';
const { email, password } = req.body;
try {
const { rows } = await db.query(userQuery, [email]);
const { rows } = await db.query(userQuery, [email.trim().toLowerCase()]);
if (!rows[0]) {
return handleServerResponseError(res, 404, 'Account with Email not found');
}
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/Trip.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const Trip = {
* @returns {object} response object
*/
async create(req, res) {
console.log(req.body);
const {
bus_id, origin, destination, trip_date, fare
} = req.body;
Expand Down Expand Up @@ -80,6 +81,7 @@ const Trip = {
* @returns {object} response object
*/
async getTrips(req, res) {
console.log(req.body);
try {
const findAllQuery = 'SELECT * FROM Trips';
const { rows } = await db.query(findAllQuery);
Expand Down
23 changes: 14 additions & 9 deletions src/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ export const handleServerResponse = (response, status, data) => response.status(
* @returns {*} error response
*/
// eslint-disable-next-line max-len
export const handleServerResponseError = (response, status, message) => response.status(status).send({
status: 'error',
error: message
});
export const handleServerResponseError = (response, status, message) => {
logger().error(message);
return response.status(status).send({
status: 'error',
error: message
});
};

export const handleServerError = (res, error) => {
logger().error(error);
Expand Down Expand Up @@ -91,10 +94,11 @@ export const createToken = (id, isAdmin) => {
* @returns {Object} response object
*/
export const hasToken = async (req, res, next) => {
const token = req.body.token || req.headers['x-access-token'];
const token = req.body.token || req.headers['x-access-token'] || req.headers.Authorization || req.body.Authorization;
try {
if (token) {
const decoded = await jwt.verify(token, process.env.SECRET);
const noBearer = token.replace(/Bearer\s/gi, '');
const decoded = await jwt.verify(noBearer, process.env.SECRET);
const text = 'SELECT * FROM Users WHERE id = $1';
const { rows } = await db.query(text, [decoded.id]);
if (!rows[0]) {
Expand All @@ -110,16 +114,17 @@ export const hasToken = async (req, res, next) => {
};

/**
* @method hasToken
* @method isAdmin
* @param {*} req
* @param {*} res
* @param {*} next
* @returns {Object} response object
*/
export const isAdmin = async (req, res, next) => {
const token = req.body.token || req.headers['x-access-token'];
const token = req.body.token || req.headers['x-access-token'] || req.headers.Authorization;
try {
const decoded = await jwt.verify(token, process.env.SECRET);
const noBearer = token.replace(/Bearer\s/gi, '');
const decoded = await jwt.verify(noBearer, process.env.SECRET);
if (req.body.is_admin) {
return next();
}
Expand Down