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

#7-User Authentication #16

Open
wants to merge 24 commits into
base: develop
Choose a base branch
from
Open

#7-User Authentication #16

wants to merge 24 commits into from

Conversation

mernxl
Copy link
Contributor

@mernxl mernxl commented May 9, 2020

Description

Implementation of authentication, accessible through /auth/{action} where action stands for the action to be carried out. These actions include;

  • sign-up: Normal signup, takes at least an email and password
  • sign-in: Sign in with an email and password
  • sign-out: Clear the session information
  • update-password: Update password of current user
  • reset-password: Reset password using a token generated and sent via mail or other methods
  • request-reset-password: Request the reset Token for password reset.

Other Notes

  • Passwords are stored in a schema on the user and by default are never populated when requesting user. The password value is harshed using bcrypt.
  • On many failed login attempts, we lock user account for a given period.

You can check up my comment on issue for more

Fixes #7

How Has This Been Tested?

I did minor testing with PostMan to see that the features. Still to in app tests

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added necessary inline code documentation
  • I have added tests that prove my fix is effective and that this feature works
  • New and existing unit tests pass locally with my changes

middlewares/index.js Outdated Show resolved Hide resolved
pages/api/auth/sign-up.js Outdated Show resolved Hide resolved
schemas/user/user.model.js Outdated Show resolved Hide resolved
@tanerochris
Copy link
Contributor

@mernxl fix the codacy errors

@tanerochris tanerochris self-requested a review May 10, 2020 22:48
@mernxl
Copy link
Contributor Author

mernxl commented May 11, 2020

image

The review from codacity is somewhat incorrect, well will raise an issue later. Clearly i destructured that object which is valid in JS. Well i will fix it now, raise a flag in codacity.com so we move on.

- set content type for catchall error response
- workaround for JS Object destructuring inside an if block considered error by codacity
@KaiserPhemi KaiserPhemi added this to the sprint-2 milestone May 11, 2020
Copy link
Contributor

@tanerochris tanerochris left a comment

Choose a reason for hiding this comment

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

changes can be merged

cliffordten
cliffordten previously approved these changes May 12, 2020
@mernxl mernxl requested review from KaiserPhemi and removed request for dherve19 May 12, 2020 21:49
@tanerochris tanerochris dismissed their stale review May 13, 2020 18:30

it was an error on my part, wrongly spelled a paramter

@mernxl
Copy link
Contributor Author

mernxl commented May 13, 2020

Well fixed it. 😅In case future such mistakes.

@mernxl mernxl mentioned this pull request May 13, 2020
Copy link
Contributor

@KaiserPhemi KaiserPhemi left a comment

Choose a reason for hiding this comment

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

Critical

  • The folder structure seems a bit cumbersome. This does not make it easy for other users to navigate through the project during development
  • Many files aren't documented properly i.e. functions, classes & methods should carry at least the following docs:
/**
* @desc description of what the function does
* @param {type} param
*/
  • All database related files or functions should be in a models folder i.e. /models not /libs

  • Also, all frontend related files should be in a separate folder for easy identification cos as it is, everything seems to be not well structured.

  • Please attach screenshots of test results.

index: true,
unique: true,
required: true,
validate: [/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, 'Must be a valid email address.']
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick

This makes the code a bit difficult to read. Why not abstract the data validation to another function or library like Joi

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I was to raise the issue of validation. A framework was supposed to be set before i worked on this task. That is to permit us carry out validations at the controller level.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we leave validation currently at the database, validation at controller will be functionality update on another issue.

*
* name, message, stack
*/
export class ErrorResponse extends Error {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick

I understand that this handles error messages for the API but if we can include all that in the response object in the controller, there won't be a need 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.

@KaiserPhemi you mean like, i create an error method on the response object, like i did for .json to send json responses??

Copy link
Contributor

Choose a reason for hiding this comment

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

@mernxl
Typically you'll send an API response like this:

return response.send({
message: "",
data,
})

This class appears to only handle error message. What I'm saying is that, why create a class for that when I can easily send the error message as an API response like this:

return response
  .status(500)
  .send({
     message: "Internal Server Error",
     details,
});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok i see. Maybe i state the reasons for my doing that, you see if they are legit.

Although i know theres a down side of having to deal with the error generation and handling but the reasons for doing that are as follows.

  • I wanted to reuse the error messages, status codes, so i don't have to always create new ones which may mismatch.
  • Gives a general and continuous structure for all backend errors. Especially when it comes to validation errors.

libs/mongoose/Database.js Show resolved Hide resolved
@mernxl
Copy link
Contributor Author

mernxl commented May 17, 2020

@KaiserPhemi Thank you for your reviews. I guess the whole structure will need to be changes in another pull request then i rebase and add this changes to follow up a better structure. That was actually the structure i found so i had to flow with it.

@tanerochris maybe we will have to look into the file structure, given you setup the initial one.

  • About the code documenting, i will document all the functions i created for this tasks.
  • For tests, i will merge the test feature

@mernxl
Copy link
Contributor Author

mernxl commented May 17, 2020

I noticed @tanerochris already took in the changes of this pull request in #20 pull. I guess this pull request will see a closure then, So @tanerochris will have to do the updates on this pull if thats ok.

@tanerochris
Copy link
Contributor

tanerochris commented May 18, 2020 via email

tanerochris added a commit that referenced this pull request May 20, 2020
- documented project api endpoints
- refactored project structure, renamed folders and moved files raised in #16
- added prettier configuration

partially resolve #14 reviews
tanerochris added a commit that referenced this pull request May 20, 2020
- documented project api endpoints
- refactored project structure, renamed folders and moved files raised in #16
- added prettier configuration

partially resolve #14 reviews
tanerochris added a commit that referenced this pull request May 20, 2020
- documented project api endpoints
- refactored project structure, renamed folders and moved files raised in #16
- added prettier configuration

partially resolve #14 reviews
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.

Basic Implementation of User Authentication
4 participants