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

submission #44

Open
wants to merge 5 commits into
base: master
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
142 changes: 142 additions & 0 deletions controllers/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
var sha256 = require('js-sha256');
var SALT = "tweedr";


module.exports = (db) => {

const registerControllerCallback(request,response) => {

let hashedPassword = sha256(request.body.password + SALT);

const queryString = 'INSERT INTO users (username, password) VALUES ($1, $2) RETURNING *';

const values = [
request.body.username,
hashedPassword,
];

db.tweedr.getUsers(queryString, values, (err,result) =>{


if (err) {
console.error('query error:', err.stack);
response.send( 'query error' );
} else {
console.log('query result:', result);

response.redirect('/login');

}

});


const indexControllerCallback(request,response) => {

db.tweedr.getAll(queryString, (err, result) => {
let tweets = {};
tweets.list=[];
for(let i = 0; i < result.rows.length; i++){
tweets.list.push(result.rows[i]);
}
response.render('Home', tweets);
});

}

const loginControllerCallback(request,response) =>{

let requestUsername = request.body.username;
let requestPassword = request.body.password;

const queryString = "SELECT * FROM users WHERE username= '"+requestUsername+"'";

db.tweedr.getUsers(queryString, (err, result) => {

if (err) {
console.error('query error: ', err.stack);
response.send('query error');
} else {
console.log('query result: ', result.rows);


if (result.rows.length > 0) {

let hashedRequestPassword = sha256(requestPassword + SALT);

if (hashedRequestPassword === result.rows[0].password) {
let user_id = result.rows[0].id
let hashedCookie = sha256(SALT+ user_id);


response.cookie('user_id', user_id);
response.cookie('hasLoggedIn', hashedCookie);

response.redirect('/');
} else {
response.status(403).send('wrong password');
}

}

else {
response.status(403).send('NO USERNAME!');
}
}

});
}

const tweetControllerCallback(request,response) => {
response.render('tweet');
}

const newTweetControllerCallback(request,response) => {

const newTweet = 'INSERT INTO tweets (content) VALUES ($1)';
console.log(request.body);
let values = [request.body.tweets];
console.log(values);

db.tweedr.tweedr(newTweet, values, (err, result) => {

let tweets =[];

for(let i = 0; i < result.rows.length; i++){
tweets.push(result.rows[i]);
}
console.log(result.rows);
response.redirect('/');
});

}

const cookieControllerCallback(request,response) =>{
let user_id = request.cookies['user_id'];
let hashedValue = sha256(SALT + user_id);


if (request.cookies['hasLoggedIn'] === hashedValue) {
response.send("LOGGED IN");
} else {
response.redirect('/login');
}


}



}

return{
register : registerControllerCallback,
index : indexControllerCallback,
login : loginControllerCallback,
tweet : tweetControllerCallback,
newTweet : newTweetControllerCallback,
cookie : cookieControllerCallback
}


}
57 changes: 57 additions & 0 deletions db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const pg = require('pg');
const url = require('url');


var configs;

if( process.env.DATABASE_URL ){

const params = url.parse(process.env.DATABASE_URL);
const auth = params.auth.split(':');

configs = {
user: auth[0],
password: auth[1],
host: params.hostname,
port: params.port,
database: params.pathname.split('/')[1],
ssl: true
};

}else{
configs = {
user: 'kellylim',
host: '127.0.0.1',
database: 'tweedr',
port: 5432
};
}

const pool = new pg.Pool(configs);

pool.on('error', function (err) {
console.log('idle client error', err.message, err.stack);
});

//Require Model Files

const allUsersModelsFunction = require('./models/users');

const usersModelsObject = allUsersModelsFunction( pool );

module.exports = {
//make queries directly from here
queryInterface: (text, params, callback) => {
return pool.query(text, params, callback);
},

// get a reference to end the connection pool at server end
pool:pool,

/*
* ADD APP MODELS HERE
*/

// users: userModelsObject,
users: usersModelsObject
};
Loading