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

First merge if Backend branch vs v.2.x #22

Open
wants to merge 29 commits into
base: v.2.x
Choose a base branch
from
Open

Conversation

martacolombas
Copy link
Owner

This pull request aims to clean up all the commits and code added in the last couple of days, before opening a formal pull request.

Copy link
Owner Author

@martacolombas martacolombas left a comment

Choose a reason for hiding this comment

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

Fix changes, comment code and resend PR

client/src/App.js Show resolved Hide resolved
client/src/App.js Outdated Show resolved Hide resolved
client/src/Components/Login/Login.js Outdated Show resolved Hide resolved
server/config/db-config.js Outdated Show resolved Hide resolved
server/config/passport-config.js Show resolved Hide resolved
User.findOne({ githubId: profile.id }).then((errors, currentUser) => {
if (currentUser) {
currentUser.overwrite({ token: accessToken }).save();
response.status(201).send(rows);
Copy link
Owner Author

Choose a reason for hiding this comment

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

we are not declaring rows here. Remove

server/routes/oauth-routes.js Outdated Show resolved Hide resolved
@martacolombas
Copy link
Owner Author

First of all, I'd like to apologize for the magnitude of this PR. It is totally unacceptable and I will do my best to avoid this kind of situation next time.

What does this PR include?

Frontend (/client/src)

  • We've added the logic to understand whether a user is coming from manually fulfilling the form or through the Github authentication, since, in the latter case, we need to authenticate the user by id and bring the token from the DB to the frontend
  • We've added a Private route component, that is part of the Router component. The library used for the routes is react-router-dom

Backend (/server)

  • We've added the file structure for the project
  • The backend is built with Express.js and connected to a Mongo DB through mongoose
  • The authentication is done with passport.js
  • The backend includes two types of different routes (as of now): basic routes (getUsers and getUsersById) and oauth-routes (necessary routes to successfully fulfill the authentication through github)

Copy link
Collaborator

@ivanortegaalba ivanortegaalba left a comment

Choose a reason for hiding this comment

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

I tried to review everything but since it's a quite long PR I could miss some things 😅

This is an amazing job and this will set up a good base in order to move forward with the gitRank v2. So excited for that! 👏 💯

Comment on lines +7 to +9
BrowserRouter as Switch,
Route,
BrowserRouter,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we're importing BrowserRouter twice. Maybe what we wanted is something like the basic example of React Router
In that case, we should modify:

Suggested change
BrowserRouter as Switch,
Route,
BrowserRouter,
Switch,
Route,
BrowserRouter,

const [token, setToken] = useState('');
const [username, setUsername] = useState('');
const [offline, setOffline] = useState(!navigator.onLine);
const [token, setToken] = useState(localStorage.getItem('token') || '');
Copy link
Collaborator

Choose a reason for hiding this comment

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

I can see a very common pattern where you want to Load/Save state from the local storage and set it in the state. I'd think about having a custom hook for that. What do you think?

Comment on lines +20 to +22
/* the user can access the app through two different options:
1. indicating username and token manually. In the case of GitHub Enterprise the user also needs to indicate the endpoint.
2. Github authentication */
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's a very common practice to use block comment like this:

Suggested change
/* the user can access the app through two different options:
1. indicating username and token manually. In the case of GitHub Enterprise the user also needs to indicate the endpoint.
2. Github authentication */
/**
* the user can access the app through two different options:
* 1. indicating username and token manually. In the case of GitHub Enterprise the user also needs to indicate the endpoint.
* 2. Github authentication
**/

But this is totally up to you :)

const userId = getIdFromLocation();
if (userId) {
// if there's id, we need to look for the token stored in the ddbb
fetch(`http://localhost:8080/users/${userId}`)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Have you thought about using environment variables to set the API_URL? I think it could be a good step to upload your code without needing to app another PR to change this :)

Suggested change
fetch(`http://localhost:8080/users/${userId}`)
fetch(`${process.env.API_URL}/users/${userId}`)

Comment on lines +30 to +32
.then(res => {
return res.text();
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we use arrow return syntax instead? I think we can save a couple of lines :)

Suggested change
.then(res => {
return res.text();
})
.then(res => res.text())

@@ -0,0 +1,33 @@
{
"name": "server",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"name": "server",
"name": "gitRank API",

Comment on lines +7 to +8
router.get('/users', queries.getUsers);
router.get('/users/:id', queries.getUserById);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This must be private routes. Otherwise, the server will be accessible.

/* callback route for github to redirect to
hand control to passport to use code to grab profile info */
router.get('/github/redirect', passport.authenticate('github'), (req, res) => {
res.redirect(`http://localhost:3000/dashboard?id=${req.user.githubId}`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
res.redirect(`http://localhost:3000/dashboard?id=${req.user.githubId}`);
res.redirect(`${process.env.BASE_URL}/dashboard?id=${req.user.githubId}`);

const users = await User.find();
response.status(200).send(users);
} catch (error) {
response.status(500);
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's important to send the error message to the response in order to give context to the API client.

Suggested change
response.status(500);
response.status(500).send(error);

const user = await User.findOne({ githubId: id });
response.status(200).send(user);
} else {
response.status(204);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
response.status(204);
response.status(204).send(`User with id ${id} not found`);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants