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

JWT #8

Merged
merged 3 commits into from
Oct 16, 2018
Merged

JWT #8

merged 3 commits into from
Oct 16, 2018

Conversation

vipulrawat
Copy link
Contributor

No description provided.

@vipulrawat
Copy link
Contributor Author

vipulrawat commented Oct 14, 2018

Error: Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout

  • Changed jest.setTimeout of whole test
  • Changed jest.setTimeout of each test
  • Changed Promises to async - await
  • Called callback after the test

@ackinc
Copy link
Contributor

ackinc commented Oct 15, 2018

Error: Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout

* [x]  Changed jest.setTimeout of whole test

* [x]  Changed jest.setTimeout of each test

* [x]  Changed Promises to async - await

* [x]  Called callback after the test

Timeout is because app has no DB to connect to when running on Travis. The test code is just waiting until a DB connection has been made.

@vipulrawat vipulrawat force-pushed the JWT branch 2 times, most recently from a0c4059 to 7db9817 Compare October 15, 2018 05:35
@vipulrawat vipulrawat changed the title [WIP] JWT JWT Oct 15, 2018
@@ -0,0 +1,17 @@
const sgMail = require('@sendgrid/mail');

sgMail.setApiKey('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');
Copy link
Contributor

Choose a reason for hiding this comment

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

should this be in the environment?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No.
I'll put this in Travis's env. variables.

Copy link
Contributor

Choose a reason for hiding this comment

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

So it should not be a plain string here, right?

Copy link
Contributor Author

@vipulrawat vipulrawat Oct 15, 2018

Choose a reason for hiding this comment

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

Yes. It shouldn't be.

packages/api/winston.config.js Outdated Show resolved Hide resolved
packages/api/winston.config.js Outdated Show resolved Hide resolved

function generate(payload, expiresIn = '1d') {
return new Promise((resolve, reject) => {
jwt.sign(payload, process.env.JWT_SECRET, { expiresIn }, (err, token) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Get the secret in a config file instead of calling process.env multiple times. Though, it may not be that bad in simple application. However, this routing will be called multiple times and since we don't expect the SECRET to change, we can cache it in the application layer.

Read this.

@vipulrawat
Copy link
Contributor Author

@ArfatSalman It's done. You can check it now.

PORT: 5000,
EMAIL_VERIFICATION_TOKEN_EXPIRY: 15 * 60, // 15 minutes
LOGIN_TOKEN_EXPIRY: '28d',
LOGIN_COOKIE_EXPIRY: 4 * 7 * 24 * 60 * 60 * 1000, // 4 weeks
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok. I see that you have ci.js, dev.js and prod.js with repeated key-val pairs.
I think you can do this -

const commonConfig = { ... };

const ci = {
  ...commonConfig,
  EMAIL_VERIFICATION_TOKEN_EXPIRY: over-ridden value

It's not good for a single value to live in 3 files.

What do you say?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's true. Modified the code now.


beforeAll((done) => {
jest.setTimeout(10000);
mongoose.connect(process.env.DB_URL, { useNewUrlParser: true }, () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

You can use your config files here too for importing constants.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

// In other cases, such as integration tests, we want to start the server elsewhere
// so we can stop it when the tests are done
if (!module.parent) {
if (!PORT) throw new Error('port not specified');
Copy link
Contributor

Choose a reason for hiding this comment

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

I see that you have a file called ERR_MSGS.

Can you put this error in there too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Added new errors into ERR_MSGS file

});
})
.catch(() => {
// TODO: error logging
Copy link
Contributor

Choose a reason for hiding this comment

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

do the TODO

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

function decode(token) {
return new Promise((resolve, reject) => {
jwt.verify(token, JWT_SECRET, (err, decoded) => {
if (err) reject(err);
Copy link
Contributor

Choose a reason for hiding this comment

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

Brackets.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@ArfatSalman
Copy link
Contributor

@vipulrawat Please see a new set of changes required.

@vipulrawat
Copy link
Contributor Author

@vipulrawat Please see a new set of changes required.

OK


module.exports = {
...commonConfig,
...process.env,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you put the ...process.env in the commnConfig too? Since it'll already override thecommonConfig so we don't need to spread process.env thrice in 3 files.

What do you say?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed.

@vipulrawat vipulrawat merged commit 861cf94 into master Oct 16, 2018
@ArfatSalman ArfatSalman deleted the JWT branch October 18, 2018 16:13
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.

4 participants