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

Setup boilerplate, Added Auth #1

Merged
merged 2 commits into from
Jul 22, 2020
Merged

Setup boilerplate, Added Auth #1

merged 2 commits into from
Jul 22, 2020

Conversation

temanisparsh
Copy link
Contributor

  • Setup a project structure
  • Setup auth service using passport-jwt
  • Setup basic login / register auth flows

@aniketnk
Copy link
Collaborator

Is this ready for merge?
Unit test included?
Let's work on getting a PR template? And CI/CD solution too.

@temanisparsh
Copy link
Contributor Author

@aniketnk Not yet, it needs to be updated with these things:

  • Token is stored while logging in and the token is deleted from the DB when the user logs out.
  • Need to update this using typescript.
  • Need to write unit tests for auth.
    Will update all these by tomorrow.
    Should I use Mocha or Jest for tests?

@aniketnk
Copy link
Collaborator

Oh that's great. Looking forward to review!
I've made issues #2 and #3 for the GitHub actions/workflows. I will work on this over the weekend, and hopefully
we will have a solid ecosystem for development.

@aniketnk aniketnk marked this pull request as draft July 10, 2020 19:32
@temanisparsh temanisparsh marked this pull request as ready for review July 12, 2020 12:13
@RITIKHARIANI
Copy link
Member

Also please add comments wherever possible 😊

Copy link
Collaborator

@aniketnk aniketnk left a comment

Choose a reason for hiding this comment

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

I've added comments in a few places. Resolve and/or reply if it needs to be discussed.
Can you also work on a README or CONTRIBUTING.md with dev setup instructions.


import passport from 'passport';

import config from './config';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need any error handling if the config file is not present?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Umm, I have an example config file and I will add in the instructions to create config in the rEADME, I don't see a reason to add error handling for this.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Okay.
But I think that a 'file not found' error doesn't provide the developer with enough information. That's why if we had some config store, and if that import failed we could throw an appropriate detailed error message. And that would have to be handled only in one place(where the store is initialised).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, I will update it so that it throws an appropriate error for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the error it throws for now. Is this fine?

 6 import config from './config';
                     ~~~~~~~~~~

    at createTSError (/home/sparsh/Desktop/Sparsh/Projects/alcoding/posterior/node_modules/ts-node/src/index.ts:434:12)
    at reportTSError (/home/sparsh/Desktop/Sparsh/Projects/alcoding/posterior/node_modules/ts-node/src/index.ts:438:19)
    at getOutput (/home/sparsh/Desktop/Sparsh/Projects/alcoding/posterior/node_modules/ts-node/src/index.ts:578:36)
    at Object.compile (/home/sparsh/Desktop/Sparsh/Projects/alcoding/posterior/node_modules/ts-node/src/index.ts:775:32)
    at Module.m._compile (/home/sparsh/Desktop/Sparsh/Projects/alcoding/posterior/node_modules/ts-node/src/index.ts:858:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/home/sparsh/Desktop/Sparsh/Projects/alcoding/posterior/node_modules/ts-node/src/index.ts:861:12)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
[nodemon] app crashed - waiting for file changes before starting...

Copy link
Collaborator

Choose a reason for hiding this comment

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

No, this error message is not informative enough. Let's open an issue to solve this config feature.

})
);

export const verifyUser = passport.authenticate('jwt', { session: false });
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since the JWT doesn't have any user information apart from the passport "id", does it do a database lookup to verify each time?

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, it does a DB lookup everytime

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmmm. That's an unnecessary overhead. Any way around this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will have to look into this. I came up with a few solutions but most of them seem to have some or the other side effect.
Think of an ideal scenario where we have established a system where DB lookup isn't needed. Now I have logged in from 2 systems s1 and s2. If I were to change my password from s2 and then try to access my account from s1..there has to be a DB lookup to invalidate those credentials right? This would contradict the first assumption.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, this issue won't arise once we have the refresh token mechanism. Let's add that as an issue and work on it later!

@@ -0,0 +1,27 @@
import User, { IUser } from '../models/user.model';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Some documentation needed here too!

Copy link
Contributor

@souravtecken souravtecken left a comment

Choose a reason for hiding this comment

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

There was some hook that applied changes after a commit was made, I'm not sure where that's configured, it didn't prevent the commit from happening, it formatted a few files after the commit. Could that behaviour be changed?

@temanisparsh
Copy link
Contributor Author

temanisparsh commented Jul 13, 2020

@souravtecken Yeah, it's a pre commit hook which runs the linter and checks other rules too.
Example: It will give you a warning when you have unused variables in your code. So you won't be able to commit till you fix it. According to me we should keep it. Another example would be to remove all console logs which everyone tends to use a lot during Dev.

@temanisparsh temanisparsh requested a review from aniketnk July 14, 2020 07:19
aniketnk
aniketnk previously approved these changes Jul 18, 2020
@aniketnk
Copy link
Collaborator

LGTM, let's merge this so other work can go on in parallel !!
Would've liked more comments everywhere, let's add this on later.

@aniketnk
Copy link
Collaborator

Can you clean up the commit history so we can "rebase and merge"?

* added dependencies
* added auth model and router
* setup passport config
* convert to typescript
* add types as ddev dependencies
* add eslint and prettier config
* setup nodemon dev server
@temanisparsh temanisparsh requested a review from aniketnk July 21, 2020 21:00
@temanisparsh temanisparsh merged commit 2fce313 into pes-alcoding-club:master Jul 22, 2020
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.

5 participants