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

[Tutorial/TypeScript] TS warning when no RBAC scheme is implemented #5038

Closed
Philzen opened this issue Apr 4, 2022 · 9 comments · Fixed by #5856
Closed

[Tutorial/TypeScript] TS warning when no RBAC scheme is implemented #5038

Philzen opened this issue Apr 4, 2022 · 9 comments · Fixed by #5856
Labels
topic/typescript tutorial-qa Redwood Tutorial QA Week related items

Comments

@Philzen
Copy link
Contributor

Philzen commented Apr 4, 2022

Reporting this non-critical finding here as advised:

Discovered in https://redwoodjs.com/docs/tutorial/chapter4/authentication#an-admin-section

The auth.ts file generated by yarn rw setup auth dbAuth expects a roles property to exist on the user object. However, implementers may choose (like in this tutorial chapter) not to implement role-based access control.

In that case, we're getting

Property 'roles' does not exist on type '{ id: number; email: string; }'.ts(2339)

in three places in api/src/lib/auth.ts.

Maybe there is way to expand the interface InferredCurrentUser, making this an optional field?

@jtoar jtoar added this to Triage Apr 4, 2022
@jtoar jtoar moved this to Needs triage in Triage Apr 4, 2022
@Philzen Philzen changed the title [Tutorial/TypeScript] [Tutorial/TypeScript] TS warning when no RBAC scheme is implemented Apr 4, 2022
@Philzen
Copy link
Contributor Author

Philzen commented Apr 4, 2022

Just commited this chapter to Philzen/redwoodblog-tutorial-typescript@2c45a51 if wanna checkout the warning straightaway.

@Tobbe
Copy link
Member

Tobbe commented Apr 7, 2022

Thanks for the issue report @Philzen. Do you want to have a look at fixing this, or do you want me to do that?

@Philzen
Copy link
Contributor Author

Philzen commented Apr 7, 2022

@Tobbe it would be an honor, however i'm still relatively new to TypeScript and how to fix this relatively complex / inferred type (at least in the most appropriate way) seems a bit over the top of my head currently, at the least i'm unsure which would be the correct spot.

If you have a clue, kindly go ahead with a fix.

@dac09
Copy link
Collaborator

dac09 commented Apr 8, 2022

Thanks again for yet another good issue @Philzen ✌️

This is expected behaviour, though I believe. We ship auth templates with the ability to handle roles, but there's no reason you have to use it - all you have to do is remove the hasRole function if you don't want roles in your project.

I think its a good thing that you get that TypeScript error - imagine trying to implement Roles, if we made it optional - you wouldn't be able to spot that Roles isn't being picked out from the DB.

@simoncrypta simoncrypta added topic/typescript tutorial-qa Redwood Tutorial QA Week related items labels Apr 8, 2022
@simoncrypta simoncrypta moved this from Needs triage to Needs discussion in Triage Apr 8, 2022
@simoncrypta simoncrypta removed their assignment Apr 8, 2022
@thedavidprice
Copy link
Contributor

Off Topic
@Philzen your participation is 🤯 It would be a pleasure to have you join a future Contributors Meetup if you're interested:
https://community.redwoodjs.com/t/contributors-meetups-a-next-step-for-redwoodjs-participation/2470

DM me @thedavidprice on Twitter, Forums, or Discord. And/or email via my GH profile.

Most of all, thank you! 🙏

Someone else might have already invited you. Just know we're all super excited to have your help with these things.

@Tobbe
Copy link
Member

Tobbe commented Apr 8, 2022

This is expected behaviour, though I believe. We ship auth templates with the ability to handle roles, but there's no reason you have to use it - all you have to do is remove the hasRole function if you don't want roles in your project.

I think its a good thing that you get that TypeScript error - imagine trying to implement Roles, if we made it optional - you wouldn't be able to spot that Roles isn't being picked out from the DB.

I can't decide if I agree or not here 😆

I love the help TS gives you in spotting errors like what you describe above. But I also don't like it when generated code is throwing errors in my face 🙂 Always leaves me with questions like "Have I done something wrong?", "Is the generator broken?", "What do I need to do to fix this?" etc.

I need to go through the tutorial with a TS project to see this for myself to be able to give any good suggestions on how to handle this

@Tobbe
Copy link
Member

Tobbe commented Apr 12, 2022

Finally got around to try this all out in a TS run-through of the tutorial.

I totally see all the red squiggles. So maybe the easiest and least controversial option is to update the tutorial to instruct the user to just remove the contents of that function and just always make it return false.

Another option is to add a custom type guard. Something like this

const isRbacUser = (
  user: typeof context.currentUser
): user is typeof context.currentUser & { roles: string | string[] } =>
  'roles' in user

And then

export const hasRole = (roles: AllowedRoles): boolean => {
  if (!isAuthenticated() || !isRbacUser(context.currentUser)) {
    return false
  }

@dac09 Do you think adding a type guard is more of a disservice to the end user here?

EDIT:

Now that I actually added roles to the user db schema I'm getting a new error with my solution above

image

I need to think on this some more... Maybe we don't need to add rbac support from the start to the template. Or maybe we should make the tutorial add support for multiple roles from the start. I think that might get rid of this particular error.

@Philzen
Copy link
Contributor Author

Philzen commented Jun 30, 2022

I had totally forgotten about the fact i had written this issue when i handed in #5856 today 😆
Guess i lost track of it, sorry for not following up earlier.

@Tobbe

I also don't like it when generated code is throwing errors in my face 🙂 Always leaves me with questions like "Have I done something wrong?", "Is the generator broken?", "What do I need to do to fix this?" etc

That's exactly where i'm coming from. The question "Is the RedwoodJS generator somehow broken?" should never come up in the DX, i guess that's what everybody is working for here. Imho it must not ever generate code that violates its own ESLint rules (see #5827) or worse (like in this case) generates invalid types that will make yarn rw check-types fail in order to build trust.

@dac09

imagine trying to implement Roles, if we made it optional - you wouldn't be able to spot that Roles isn't being picked out from the DB.

What about adding some JSDoc here, linking to the getCurrentUser function and giving the heads-up that roles will always be undefined unless included in the selection ... would that suffice?

@redwoodjs-bot redwoodjs-bot bot removed this from Main Jul 1, 2022
@Tobbe
Copy link
Member

Tobbe commented Jul 4, 2022

FYI everyone: Discussion (also) continues in #5866

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic/typescript tutorial-qa Redwood Tutorial QA Week related items
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants