Skip to content
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
8 changes: 8 additions & 0 deletions server/data/schema.sql
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
DROP TABLE IF EXISTS Positions
CASCADE;
DROP TABLE IF EXISTS Users
CASCADE;
DROP TABLE IF EXISTS ProjectTags
CASCADE;
DROP TABLE IF EXISTS Projects;
DROP TABLE IF EXISTS Tags;
--Position Table
CREATE TABLE Positions
(
id SERIAL PRIMARY KEY,
position VARCHAR(255)
);

-- TODO: remigrate the DB here
CREATE TABLE Users
Expand Down
84 changes: 57 additions & 27 deletions server/routes/github_oauth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// const cookieSession = require('cookie-session');
const { URLSearchParams } = require('url');
const {
URLSearchParams
} = require('url');
const express = require('express');
const router = express.Router();
const pg = require('pg');
Expand Down Expand Up @@ -79,21 +81,19 @@ router.get("/auth/github/callback", async (req, res) => {
});

async function getAccessToken(code, client_id, client_secret) {
const result = await axios(
{
const result = await axios({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove all automatic formatting like this added to this file that does not directly part of this PR.

method: 'post',
url: "https://github.com/login/oauth/access_token",
data: {
client_id,
client_secret,
code
}
}
).then(data => {
const params = new URLSearchParams(data.data);
const access_token = params.get("access_token");
return access_token;
})
}).then(data => {
const params = new URLSearchParams(data.data);
const access_token = params.get("access_token");
return access_token;
})
.catch(err => {
throw err;
});
Expand All @@ -116,10 +116,12 @@ async function checkUser(user_data, github_token, res) {
.then(async (result) => {
const user = result.rows[0];
if (user !== undefined) {
const auth_token = jwt.sign(
{ userId: user.id },
process.env.TOKEN_SECRET,
{ expiresIn: '24h' });
const auth_token = jwt.sign({
userId: user.id
},
process.env.TOKEN_SECRET, {
expiresIn: '24h'
});
let SQL = 'UPDATE Users SET auth_token=$1 WHERE github_id=$2;';
let values = [auth_token, user_data.id];
client.query(SQL, values)
Expand All @@ -140,10 +142,12 @@ async function checkUser(user_data, github_token, res) {
}

async function createUser(user_data, github_token) {
const auth_token = jwt.sign(
{ userId: user_data.id },
process.env.TOKEN_SECRET,
{ expiresIn: '24h' });
const auth_token = jwt.sign({
userId: user_data.id
},
process.env.TOKEN_SECRET, {
expiresIn: '24h'
});

const newUser = new User({
auth_token: auth_token,
Expand Down Expand Up @@ -179,8 +183,7 @@ async function getUser(auth_token) {
const user = result.rows[0];
if (user !== undefined) {
return user;
} else {
}
} else {}
})
.catch(err => {
throw err;
Expand All @@ -195,8 +198,7 @@ async function getUserByID(userID) {
const user = result.rows[0];
if (user !== undefined) {
return user;
} else {
}
} else {}
})
.catch(err => {
throw err;
Expand Down Expand Up @@ -255,7 +257,11 @@ async function getProject(is_auth = false) {
if (projects !== undefined) {
for (let project of projects) {
const lead = await getUserByID(project.lead_id);
let lead_obj = { name: lead.name, position: lead.position, experience: lead.experience_lvl };
let lead_obj = {
name: lead.name,
position: lead.position,
experience: lead.experience_lvl
};
if (is_auth) {
lead_obj['email'] = lead.email;
} else {
Expand Down Expand Up @@ -308,14 +314,18 @@ router.get("/projects/logout/", (req, res) => {
let values = [newAuth_token, auth_token];
client.query(SQL, values)
.then(result => {
return res.json({ result: "success" });
return res.json({
result: "success"
});
})
.catch(err => {
console.error("Logout Error: could not update the DB", err);
throw err;
});
} else {
return res.json({ result: "success" });
return res.json({
result: "success"
});
}
})
.catch(err => {
Expand Down Expand Up @@ -344,7 +354,10 @@ async function addNewProject(auth_token, position, experience_lvl, new_project)
await client.query(SQL, values)
.then(async (result) => {
const user_id = result.rows[0]['id'];
await createProject({ ...new_project, lead_id: user_id });
await createProject({
...new_project,
lead_id: user_id
});
})
.catch(err => {
throw err;
Expand Down Expand Up @@ -408,9 +421,26 @@ router.post("/projects/add_project/", async (req, res) => {
// TODO: atomic transaction
// TODO: safeguard and return error messages
await addNewProject(auth_token, req.body['position'], req.body['experience_lvl'], new_project).catch(e => console.error(e));
return res.send({ "result": "success" });
return res.send({
"result": "success"
});
});

//Positions method
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: space after // and before Positions

async function getPositions() {
let SQL = 'SELECT * FROM Positions;';
return client.query(SQL)
.then(result => {
console.log(result);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably return rather than console.log?

})
.catch(err => {
throw err;
});
}

router.get("/getPositions", (req, res) => {
const positions = getPositions(res);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's check if the user is authenticated first here. You can see an example of that from other APIs in this file.

return res.json(positions);
});

module.exports = router;
module.exports = router;