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
Conversation
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'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put this line closer to his usage like https://github.com/sandybemonkey/fd_mock/blob/use-csv-database/src/app.js#L29
app.js
Outdated
* session config | ||
* @type {{secret: string, cookie: {}, saveUninitialized: boolean}} | ||
*/ | ||
const sess = { |
There was a problem hiding this comment.
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') { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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", |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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') { |
There was a problem hiding this comment.
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 @@ | |||
{ |
There was a problem hiding this comment.
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:
- rm -rf node_modules package-lock.json
- npm i
- git add package-lock.json
- git commit -m "update package lock"
- 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'; |
There was a problem hiding this comment.
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'); | |||
}); | |||
|
|||
/** |
There was a problem hiding this comment.
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'; |
There was a problem hiding this comment.
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';
helpers/accessToken.js
Outdated
@@ -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'; |
There was a problem hiding this comment.
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'); | ||
}); | ||
|
There was a problem hiding this comment.
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)
helpers/accessToken.js
Outdated
/** | ||
* Use to send the access token to an data provider. | ||
* @return an object of data from the provider. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an object of data ?
helpers/accessToken.js
Outdated
@@ -5,30 +5,28 @@ | |||
import axios from 'axios'; | |||
import querystring from 'querystring'; | |||
import getUserHelper from './user'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use destructured import
helpers/accessToken.js
Outdated
@@ -42,9 +40,11 @@ exports.getAccessToken = async (res, queryCode) => { | |||
await axios.post(url, querystring.stringify(body), headerConfig) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
views/pages/end.ejs
Outdated
@@ -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> |
There was a problem hiding this comment.
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 ?
views/pages/profile.ejs
Outdated
@@ -8,20 +8,15 @@ | |||
<div class="subtitle"> | |||
<div class="card"> | |||
<div class="card-content"> |
There was a problem hiding this comment.
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
@sandybemonkey do you plan to have an offline mode like db ? |
views/pages/end.ejs
Outdated
@@ -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> |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ?
helpers/accessToken.js
Outdated
const clientId = config.CLIENT_SECRET; | ||
const secretKey = config.SECRET_KEY; | ||
|
||
exports.getAccessToken = async (res, req) => { |
There was a problem hiding this comment.
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 ?
helpers/accessToken.js
Outdated
const clientId = config.CLIENT_SECRET; | ||
const secretKey = config.SECRET_KEY; | ||
|
||
exports.getAccessToken = async (res, req) => { |
There was a problem hiding this comment.
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
helpers/accessToken.js
Outdated
@@ -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; | |||
/** |
There was a problem hiding this comment.
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
helpers/accessToken.js
Outdated
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# } |
There was a problem hiding this comment.
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
not really tested, TODO add more tests