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

Refactor files structure and do some little fixes #6

Merged
merged 9 commits into from Aug 10, 2018

Conversation

sandybemonkey
Copy link
Owner

not really tested, TODO add more tests

app.js Outdated
import idHintHelper from './helpers/idHintToken';
import logoutHelper from './helpers/logout';
import authorizationHelper from './helpers/authorization';
import utilsHelper from './helpers/utils';
import getAccessTokenHelper from './helpers/accessToken';

const app = express();
const port = process.env.PORT || '3000';
Copy link
Collaborator

Choose a reason for hiding this comment

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

app.js Outdated
* session config
* @type {{secret: string, cookie: {}, saveUninitialized: boolean}}
*/
const sess = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

sess => session

app.js Outdated
/**
* session config for production
*/
if (app.get('env') === 'production') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

are you sure it's needed ? This project intend to be the simpliest as possible. We must keep in mind that PHPers must understand this code

Copy link
Owner Author

Choose a reason for hiding this comment

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

I think that simple enough even me a PHPer can understand that piece of code!!

@@ -18,6 +18,7 @@
"debug": "^3.1.0",
"ejs": "^2.6.1",
"express": "^4.16.3",
"express-session": "^1.15.6",
Copy link
Collaborator

Choose a reason for hiding this comment

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

we should try to avoid adding external library to keep the codebase as simple as possible. Are you sure this one is necessary ?

Copy link
Owner Author

Choose a reason for hiding this comment

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

session was built in but like body-parser or ejs you now have to add this dependencies separately

app.js Outdated
app.set('trust proxy', 1); // trust first proxy
sess.cookie.secure = true; // serve secure cookies
}

if (process.env.NODE_ENV !== 'test') {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I have added comments in https://github.com/sandybemonkey/fd_mock/blob/use-csv-database/src/app.js, it could be a good thing to have the same comments in both files

@@ -0,0 +1,5783 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

this package-lock is not in sync with the package.json: when I run npm i the file is modified. please do the following:

  1. rm -rf node_modules package-lock.json
  2. npm i
  3. git add package-lock.json
  4. git commit -m "update package lock"
  5. git push

You can also read this thread https://stackoverflow.com/questions/45022048/why-does-npm-install-rewrite-package-lock-json

app.js Outdated
import idHintHelper from './helpers/idHintToken';
import logoutHelper from './helpers/logout';
import authorizationHelper from './helpers/authorization';
import utilsHelper from './helpers/utils';
Copy link
Collaborator

Choose a reason for hiding this comment

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

please use destructured import to avoid too generic naming like "utilsHelper":

import { getAuthorizationUrl } from './helpers/utils';

app.js Outdated
@@ -31,34 +47,40 @@ app.get('/', (req, res) => {
res.render('pages/index');
});

/**
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can use line comment syntax instead of group syntax comment for line comment:

// Init authorization and login process

app.js Outdated
import idHintHelper from './helpers/idHintToken';
import logoutHelper from './helpers/logout';
import authorizationHelper from './helpers/authorization';
import utilsHelper from './helpers/utils';
import getAccessTokenHelper from './helpers/accessToken';
Copy link
Collaborator

Choose a reason for hiding this comment

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

please use destructured import to avoid useless variables like "getAccessTokenHelper":

import { getAccessToken } from './helpers/utils';

@@ -5,30 +5,28 @@
import axios from 'axios';
import querystring from 'querystring';
import getUserHelper from './user';
import idHintToken from './idHintToken';
import config from '../config/config.json';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please destructure this import

app.js Outdated
});

app.get('/end', (req, res) => {
// resetting the id token hint.
idHintHelper.resetHintToken();
req.session.id_token = null;
req.session.userInfo = null;
res.render('pages/end');
});

Copy link
Collaborator

Choose a reason for hiding this comment

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

please add a message in the console when the server start (see https://github.com/sandybemonkey/fd_mock/blob/use-csv-database/src/app.js#L33)

/**
* Use to send the access token to an data provider.
* @return an object of data from the provider.
Copy link
Collaborator

Choose a reason for hiding this comment

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

an object of data ?

@@ -5,30 +5,28 @@
import axios from 'axios';
import querystring from 'querystring';
import getUserHelper from './user';
Copy link
Collaborator

Choose a reason for hiding this comment

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

use destructured import

@@ -42,9 +40,11 @@ exports.getAccessToken = async (res, queryCode) => {
await axios.post(url, querystring.stringify(body), headerConfig)
Copy link
Collaborator

Choose a reason for hiding this comment

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

querystring.stringify => To my opinion this is typically the kind of library we should avoid. A simple reduce in native javascript can do the job.

Copy link
Collaborator

Choose a reason for hiding this comment

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

you should use the params option and remove this lib.

See axios/axios#739 (comment).

Copy link
Owner Author

Choose a reason for hiding this comment

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

Try with params but use querystring as the documention said.

helpers/user.js Outdated
exports.getUser = async (token, res) => {
if (!token) res.sendStatus(401);
exports.getUser = async (req, res) => {
if (!req.accessToken) res.status(401).send('Access token is required');
Copy link
Collaborator

Choose a reason for hiding this comment

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

you should return here, to stop the execution of the function here


describe('helpers/authorization', () => {
it('should return a correct authorization url to call the API "/api/v1/authorize"', () => {
// Setup
Copy link
Collaborator

Choose a reason for hiding this comment

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

I rather remove this comments as we won't repeat this on every tests made since it will weighten the reading without adding enough useful information

@@ -6,7 +6,7 @@
Guichet des Chiroquois
</h1>
<div class="subtitle">
<h4>Merci de votre visite, afin que votre deconnection soit totale, veuillez fermer votre navigateur.</h4>
<h4>Merci de votre visite, veuillez fermer votre navigateur afin que la deconnection soit complète.</h4>
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do the user has to close is browser ?

@@ -8,20 +8,15 @@
<div class="subtitle">
<div class="card">
<div class="card-content">
Copy link
Collaborator

Choose a reason for hiding this comment

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

can you factorise the page structure with ejs inheritance

@rdubigny
Copy link
Collaborator

rdubigny commented Aug 9, 2018

@sandybemonkey do you plan to have an offline mode like db ?

@@ -6,7 +6,7 @@
Guichet des Chiroquois
</h1>
<div class="subtitle">
<h4>Merci de votre visite, afin que votre deconnection soit totale, veuillez fermer votre navigateur.</h4>
<h4>Merci de votre visite, veuillez fermer votre navigateur afin que la deconnection soit complète.</h4>
Copy link
Collaborator

Choose a reason for hiding this comment

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

end.ejs => logged-out.ejs

Copy link
Collaborator

Choose a reason for hiding this comment

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

plus, can you remove the empty login.ejs file ?

const clientId = config.CLIENT_SECRET;
const secretKey = config.SECRET_KEY;

exports.getAccessToken = async (res, req) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I made a comment about using the export keyword instead of this #5 (comment) . Did you see it ?

const clientId = config.CLIENT_SECRET;
const secretKey = config.SECRET_KEY;

exports.getAccessToken = async (res, req) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

function with req & res as parameters = controller (or middleware)

=> plz move this file out of the helpers folder

@@ -42,9 +40,11 @@ exports.getAccessToken = async (res, queryCode) => {
await axios.post(url, querystring.stringify(body), headerConfig)
.then(response => response.data)
.then((tokenData) => {
idHintToken.setHintToken(tokenData.id_token);
req.accessToken = tokenData.access_token;
req.session.id_token = tokenData.id_token;
/**
Copy link
Collaborator

Choose a reason for hiding this comment

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

plz do not use jsdoc inside functions

const tokenUrl = config.TOKEN_URL;
const redirectUrl = config.REDIRECT_URL;
const clientId = config.CLIENT_SECRET;
const secretKey = config.SECRET_KEY;

/**
* Init FranceConnect authentication login process.
* Make every http call to the different API endpoints.
* @see @link{ https://partenaires.franceconnect.gouv.fr/fcp/fournisseur-service# }
Copy link
Collaborator

Choose a reason for hiding this comment

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

plz mention this link in only one comment in this file

@sandybemonkey sandybemonkey merged commit 4e36c05 into master Aug 10, 2018
@sandybemonkey sandybemonkey deleted the CLEAN_CODE_AND_REPO branch August 10, 2018 12:04
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.

None yet

2 participants