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

Bcrypt #23

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
48 changes: 48 additions & 0 deletions dist/bundle.js

Large diffs are not rendered by default.

95 changes: 84 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"cors": "^2.8.5",
"dotenv": "^8.0.0",
"express": "^4.17.1",
"passport": "^0.4.0",
"passport-google-oauth20": "^2.0.0",
"pg": "^7.11.0",
"pg-hstore": "^2.3.3",
"react": "^16.8.6",
Expand Down
62 changes: 62 additions & 0 deletions src/backend/authController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const User = require('./db.js');

const authController = {};

authController.findUser = profile => {
return new Promise((resolve, reject) => {
const profileId = profile.id;
User.query(`SELECT * FROM users WHERE profile_id = '${profileId}';`, (err, result) => {
if (err) reject(err);
else {
console.log('find user result', result.rows[0]);
const user = result.rows[0];
resolve(user);
}
});
});
};

authController.createUser = profile => {
return new Promise((resolve, reject) => {
console.log(profile);
const target = 'Insert INTO users("username", "profile_id") VALUES($1, $2) RETURNING *;';
const values = [profile.displayName, profile.id];
User.query(target, values, (err, result) => {
if (err) reject(err);
else {
console.log('create user result', result.rows[0]);
const user = result.rows[0];
resolve(user);
}
});
});
};

authController.setCookie = (req, res, next) => {
console.log('cookie controller');
if (req.user.id) {
res.locals.cookie = 'COOOOKIES'; // req.user.id;
res.cookie('ssid', res.locals.cookie, { httpOnly: true });
}
return next();
};

// // function that creates user
// createUser(profile) {
// const username = profile.displayName;
// const profileId = profile.id;
// // query string to insert app_user table
// User.query(
// `INSERT INTO app_user(username, profile_id) VALUES ('${username}', '${profileId}') RETURNING *`,
// (err, result) => {
// if (err) {
// console.log(err);
// return err;
// }
// const user = result.rows[0];
// return result(user);
// }
// );
// },

module.exports = authController;
35 changes: 3 additions & 32 deletions src/backend/habitController.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const habitController = {
try {
await Habit.query('BEGIN');
const { rows } = await Habit.query(
`INSERT INTO habit(habit_title, user_id, start_date, end_date) VALUES ('${habitTitle}', '${userId}', '${startDate}', '${endDate}' ) returning *;`
`INSERT INTO habits(habit_name, habit_description, start_date, end_date) VALUES ('${habitTitle}', '${userId}', '${startDate}', '${endDate}' ) returning *;`
);
console.log('ROWS', rows);
// const userIdFromDb = await Habit.query(`SELECT _id FROM app_user JOIN app_user._id = `);
Expand All @@ -38,50 +38,21 @@ const habitController = {
console.log(err);
}
},
// function that creates user
createUser(req, res, next) {
const { username } = req.body;
const { password } = req.body;
// query string to insert app_user table
Habit.query(
`INSERT INTO app_user(username, password) VALUES ('${username}', '${password}');`,
err => {
if (err) throw err;
return next();
}
);
},

// function that creates log with day, userid, habitid, and checked boolean
createLog(req, res, next) {
const { day, userId, habitId, checked } = req.body;
// query string to insert log table
Habit.query(
`INSERT INTO log(day, checked, user_id, habit_id) VALUES ('${day}','${checked}','${userId}', '${habitId}');`,
(err, result) => {
if (err) throw err;
if (err) next(err);
res.locals.day = result;
return next();
}
);
},

loginUser(req, res, next) {
const { username, password } = req.body;
Habit.query(
`SELECT username, password, _id FROM app_user WHERE username = '${username}'`,
(err, result) => {
if (err) console.log(err);
const user = result.rows[0];
const usernameFromDb = user.username;
const passwordFromDb = user.password;
if (password === passwordFromDb) {
res.locals.user = user;
return next();
}
}
);
},

sendMessage(req, res, next) {
const { text, username, habitIndex, userId } = req.body;
const { habitId } = req.params;
Expand Down
Loading