Skip to content

Commit

Permalink
Merge pull request #41 from chidimo/bg-checkuser-during-signin-166149777
Browse files Browse the repository at this point in the history
#166149777 Fix user existence check during sign in
  • Loading branch information
chidimo authored May 21, 2019
2 parents 6ddcc91 + 0da5852 commit 8b2a0a8
Show file tree
Hide file tree
Showing 16 changed files with 280 additions and 259 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
env:
global:
- CC_TEST_REPORTER_ID=03990f10d1bc434a08af1af70b30627f5333955eb21bf21c3b45eade086b92b5
- CC_TEST_REPORTER_ID=f9b005dd05d6cf7849f22ee7d91f197a81979d400077e3d9f9fcc6de3c4159da
- NODE_ENV=test
- DBNAME=testdb
- PGPASSWORD=
Expand All @@ -18,6 +18,11 @@ addons:
- postgresql-client-10
before_script:
- psql -c 'create database testdb;' -U postgres
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build
after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
before_install:
- sudo sed -i -e '/local.*peer/s/postgres/all/' -e 's/peer\|md5/trust/g' /etc/postgresql/*/main/pg_hba.conf
- sudo service postgresql restart
Expand Down
24 changes: 14 additions & 10 deletions UI/js/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,24 @@ sign_up_button.addEventListener('click', e => {
window.location = './dashboard.html';
});

const swap_classes = (dom_1, dom_2) => {
dom_1.classList.add('hide_form');
dom_2.classList.remove('hide_form');
};

const activate_form = (form_1, form_2) => {
form_1.classList.add('selected');
form_2.classList.remove('selected');
};

activate_signin.addEventListener('click', e => {
e.preventDefault();
signup_form.classList.add('hide_form');
signin_form.classList.remove('hide_form');

activate_signin.classList.add('selected');
activate_signup.classList.remove('selected');
swap_classes(signup_form, signin_form);
activate_form(activate_signin, activate_signup);
});

activate_signup.addEventListener('click', e => {
e.preventDefault();
signin_form.classList.add('hide_form');
signup_form.classList.remove('hide_form');

activate_signup.classList.add('selected');
activate_signin.classList.remove('selected');
swap_classes(signin_form, signup_form);
activate_form(activate_signup, activate_signin);
});
52 changes: 30 additions & 22 deletions UI/js/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,36 @@ profile_pix.addEventListener('click', e => {
img_uploader.click();
});

const update_profile_pix = file => {
const reader = new FileReader();
reader.addEventListener('load', () => {
const { result } = reader;
profile_pix.src = result;
});
if (file) {
reader.readAsDataURL(file);
}
else {
profile_pix.src = 'https://s3.eu-west-2.amazonaws.com/quick-credit/profile_photos/1';
}
};
const id = 1;
const bucket = 'quick-credit';
const folder = 'profile_photos';
const endpoint = 's3.eu-west-2.amazonaws.com';
// const base_url = 'https://qcredit.herokuapp.com';
const base_url = 'http://localhost:3000';

// const reload_pix = () => {
// const container = document.getElementById('photo_window');
// const content = container.innerHTML;
// container.innerHTML = content;

// // this line is to watch the result in console , you can remove it later
// console.log('Refreshed');
// };

// const update_profile_pix = file => {
// const reader = new FileReader();
// reader.addEventListener('load', () => {
// const { result } = reader;
// profile_pix.src = result;
// });
// if (file) {
// reader.readAsDataURL(file);
// }
// else {
// const src = `https://${endpoint}/quick-credit/profile_photos/${id}`;
// profile_pix.src = src;
// }
// };

img_uploader.onchange = async e => {
e.preventDefault();
Expand All @@ -32,9 +49,6 @@ img_uploader.onchange = async e => {
alert(`File is ${size_in_mb}MB. Allowed size is 1MB.`);
return;
}
// update_profile_pix(file);
const id = 4;

// const filename = file.name;
// const ext = filename.slice(filename.lastIndexOf('.') + 1);
const signed_upload_url = await axios_get_signed_url(id, filetype);
Expand All @@ -43,9 +57,6 @@ img_uploader.onchange = async e => {
return user;
};

const base_url = 'https://qcredit.herokuapp.com';
// const base_url = 'http://localhost:3000';

// step 1: get a signed URL
const axios_get_signed_url = async (id, filetype) => {
const url = `${base_url}/users/${id}/photo/upload`;
Expand Down Expand Up @@ -75,9 +86,6 @@ const upload_to_aws = async (id, file, signed_url) => {
// eslint-disable-next-line no-undef
const resp = await axios.put(signed_url, file, config);
if (resp.status === 200) {
const endpoint = 's3.eu-west-2.amazonaws.com';
const bucket = 'quick-credit';
const folder = 'profile_photos';
const aws_url = `https://${endpoint}/${bucket}/${folder}/${id}`;
return aws_url;
}
Expand Down
26 changes: 13 additions & 13 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from 'express';
import createError from 'http-errors';
// import createError from 'http-errors';
import path from 'path';
import cookieParser from 'cookie-parser';
import logger from 'morgan';
Expand All @@ -23,19 +23,19 @@ app.use(cors());
app.use('/', indexRouter);

// catch 404 and forward to error handler
app.use((req, res, next) => {
next(createError(404));
});
// app.use((req, res, next) => {
// next(createError(404));
// });

// error handler
app.use((err, req, res, next) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});
// app.use((err, req, res, next) => {
// // set locals, only providing error in development
// res.locals.message = err.message;
// res.locals.error = req.app.get('env') === 'development' ? err : {};

// // render the error page
// res.status(err.status || 500);
// res.render('error');
// });

export default app;
12 changes: 8 additions & 4 deletions controllers/AuthController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import
add_user_to_db,
check_password
} from './helpers/AuthController';
// import { dev_logger } from '../utils/loggers';

const users_model = new Model('users');

Expand All @@ -32,12 +31,17 @@ const AuthController = {
signin: async (req, res) => {
const { email, password } = req.body;
const clause = `WHERE email='${email}'`;
const err_msg = `User with email ${email} does not exist.`;
// check user exists
const user_exists = await check_user_exists(
users_model, `WHERE email='${email}'`, res
);
if (!user_exists) {
return res.status(404)
.json({ error: `User with email ${email} does not exist.` });
}
const match = await check_password(users_model, email, password, res);
if (match) {
const user = await get_existing_user(
users_model, res, clause, err_msg);
users_model, res, clause);
return res
.status(200).json({ data: { ...user, token: req.token } });
}
Expand Down
42 changes: 28 additions & 14 deletions controllers/LoansController.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

import Model from '../models/Model';
import { InternalServerError } from '../utils/errorHandlers';
import { dev_logger } from '../utils/loggers';
Expand Down Expand Up @@ -38,18 +39,24 @@ const LoansController = {
}
return res.status(200).json({ data: data.rows });
}
catch (e) { return InternalServerError(res, e); }
catch (e) { throw InternalServerError(res, e); }
},

get_loan: async (req, res) => {
const { id } = req.params;
return get_loan_by_id(loans_model, id, res, 200);
try {
return get_loan_by_id(loans_model, id, res, 200);
}
catch (e) { return; }
},

create_loan: async (req, res) => {
const { rows } = await add_loan_to_db(loans_model, req, res);
const [ { id }, ] = rows;
return get_loan_by_id(loans_model, id, res, 201);
try {
const { rows } = await add_loan_to_db(loans_model, req, res);
const [ { id }, ] = rows;
return get_loan_by_id(loans_model, id, res, 201);
}
catch (e) { return; }
},

approve_or_reject_loan: async (req, res) => {
Expand All @@ -63,7 +70,7 @@ const LoansController = {
await update_loan_status(loans_model, req, res);
return get_loan_by_id(loans_model, id, res, 200);
}
catch (e) { return InternalServerError(res, e); }
catch (e) { throw InternalServerError(res, e); }
},

loan_repayment_history: async (req, res) => {
Expand All @@ -73,20 +80,27 @@ const LoansController = {
return await loan_repayment_history(repayments_model, req, res);
}
}
catch (e) { return InternalServerError(res, e); }
catch (e) { throw InternalServerError(res, e); }
},

post_repayment: async (req, res) => {
const { rows } = await add_repayment_to_db(repayments_model, req, res);
const [ { id }, ] = rows;
await update_loan_balance(loans_model, req, res);
return await get_repayment_by_id(repayments_model, id, res, 201);
try {
const { rows } = await add_repayment_to_db(
repayments_model, req, res
);
const [ { id }, ] = rows;
await update_loan_balance(loans_model, req, res);
return await get_repayment_by_id(repayments_model, id, res, 201);
}
catch (e) { return; }
},

get_repayment: async (req, res) => {
const { id } = req.params;
dev_logger(`id ******** ${id}`)
return get_repayment_by_id(repayments_model, id, res, 200);
try {
return get_repayment_by_id(repayments_model, id, res, 200);
}
catch (e) { return; }
},

get_all_repayments: async (req, res) => {
Expand All @@ -96,7 +110,7 @@ const LoansController = {
);
return res.status(200).json({ data: data.rows });
}
catch (e) { return InternalServerError(res, e); }
catch (e) { throw InternalServerError(res, e); }
},
};

Expand Down
Loading

0 comments on commit 8b2a0a8

Please sign in to comment.