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

#165401006 setting up postgres db with elephantsql #60

Merged
merged 2 commits into from
Apr 17, 2019
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "banking web app",
"main": "app.js",
"scripts": {
"server": "DEBUG=querysuccess nodemon --exec babel-node ./server/v1/config/database.js",
"start": "node ./server/build/app.js",
"build": "babel -d ./server/build ./server/v1 -s",
"dev": "nodemon --exec babel-node ./server/v1/app.js",
Expand Down
22 changes: 22 additions & 0 deletions server/v1/config/database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Client } from 'pg';
import debug from 'debug';
import dotenv from 'dotenv';

dotenv.config();

const conString = process.env.DB_CONFIG;

const client = new Client(conString);

client.connect((err) => {
if (err) {
return debug('connecterror')('could not connect to postgres', err);
}
client.query('SELECT NOW() AS "theTime"', (error, result) => {
if (error) {
return debug('queryerror')('error running query', err);
}
debug('querysuccess')(result.rows[0].theTime);
client.end();
});
});
5 changes: 4 additions & 1 deletion server/v1/controllers/login.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import jwt from 'jsonwebtoken';
import debug from 'debug';
import dotenv from 'dotenv';
import LoginService from '../services/login';

dotenv.config();

const LoginController = {
loginUser(req, res) {
const userData = req.body;
const loggedUser = LoginService.loginUser(userData);

return jwt.sign({ loggedUser }, '5634', (err, token) => {
return jwt.sign({ loggedUser }, process.env.JWTSECRETKEY, (err, token) => {
if (err) { debug('jwterror')(err); }
if (loggedUser[0] === 'Invalid format' || loggedUser[0] === 'incorrect credentials') {
res.json({
Expand Down
5 changes: 4 additions & 1 deletion server/v1/middleware/jwt.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import jwt from 'jsonwebtoken';
import dotenv from 'dotenv';

dotenv.config();

const jwtMiddleware = {
checkToken(req, res, next) {
Expand All @@ -14,7 +17,7 @@ const jwtMiddleware = {
}
},
verifyJwt(req, res, next) {
jwt.verify(req.token, '5634', (err, authorizedData) => {
jwt.verify(req.token, process.env.JWTSECRETKEY, (err, authorizedData) => {
if (err) {
return res.sendStatus(403);
}
Expand Down