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

Distinguish user and admin #23

Open
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

d0peCode
Copy link
Contributor

@d0peCode d0peCode commented Jul 1, 2019

This PR fix #12 bug. I distinguish user and admin. Changes:

  • User and admin have now own collections, models, controllers and routes.
  • auth route contain only demonstration /secret routes, however there is no longer option to make only user access route (which was useless in my opinion anyway).
  • Added utils/seed.js which create default admin with credentials from config/index.js -> .env.
  • Moved most of the functions from models to external files in models/utils directory to keep DRY principle as functions are used in two models now (admin, user) and I didn't want to copy them. How ever I didn't manage to move Schema.method({ transform () { ... to external file, more about it in Move Schema.method transform to external file #22.
  • Updated readme, changed Changed from original project to Changelog, deleted one point from Todo.

Please do code review.

@kasvith
Copy link
Owner

kasvith commented Jul 1, 2019

Hi @d0peCode can you resolve the merge conflict?

@d0peCode
Copy link
Contributor Author

d0peCode commented Jul 1, 2019

@kasvith I can tonight. At the moment I need to do some tasks in private repo

@kasvith
Copy link
Owner

kasvith commented Jul 1, 2019

Sure no problem :)

@d0peCode
Copy link
Contributor Author

d0peCode commented Jul 2, 2019

@kasvith this branch has no conflicts and jarvis passed but additional code review is highly recommended :)

@d0peCode
Copy link
Contributor Author

d0peCode commented Jul 2, 2019

Also I moved functions from models to models/utils directory. Maybe you find some better name for this directory than utils.

@kasvith
Copy link
Owner

kasvith commented Jul 2, 2019

Hi thanks @d0peCode for this awesome work. I will do a code review and merge soon.


userSchema.post('save', async function saved (doc, next) {
try {
const mailOptions = {
from: 'noreply',
to: this.email,
subject: 'Confirm creating account',
html: `<div><h1>Hello new user!</h1><p>Click <a href="${config.hostname}/api/auth/confirm?key=${this.activationKey}">link</a> to activate your new account.</p></div><div><h1>Hello developer!</h1><p>Feel free to change this template ;).</p></div>`
html: `<div><h1>Hello new user!</h1><p>Click <a href="${config.hostname}/api/user/confirm?key=${this.activationKey}">link</a> to activate your new account.</p></div><div><h1>Hello developer!</h1><p>Feel free to change this template ;).</p></div>`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

config.hostname is not available, this will not work

Copy link
Owner

Choose a reason for hiding this comment

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

I added a config param. Check in config file

Copy link
Owner

Choose a reason for hiding this comment

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

Look here

But it seems I've forgotten to add it to .env.example somehow

Copy link
Contributor Author

@d0peCode d0peCode Jul 3, 2019

Choose a reason for hiding this comment

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

Oh I didn't know but wanted to keep this change so I renamed it to BASE_URI in next PR. Sorry for confusion.

Copy link
Owner

Choose a reason for hiding this comment

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

I added hostname because imagine we are running in something like kubernets. In that scenario when sending emails we should use the web address not the internal IP.

But I see the name is more confusing, we should rename it to something like siteUrl which makes more sense

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 agree, we can rename it and when this PR will be merged I'll resolve conflicts in second one.

DEFAULT_ADMIN_NAME=admin
[email protected]
DEFAULT_ADMIN_PASSWORD=password
Copy link
Owner

Choose a reason for hiding this comment

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

Can you add a new line at the end of file

@@ -24,7 +22,7 @@ const handleJWT = (req, res, next, roles) => async (err, user, info) => {
}

// see if user is authorized to do the action
if (!roles.includes(user.role)) {
if (role && role.includes('admin') && !user.admin) {
Copy link
Owner

Choose a reason for hiding this comment

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

This method was used to add new roles to user model. Not just admin. There maybe an editor, moderator etc as many as possible.

In route definition we can add which roles can see the resouece. Like say you want a protected route only for admins and editors.

we can simply put

route.post('/page-edit', authorize(['admin', 'editor']))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

admin can be added by anyone
2 participants