question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
Tobbecommented, 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

1reaction
Philzencommented, Jul 4, 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Node.js and TypeScript Tutorial: Secure an Express API - Auth0
Learn how to use TypeScript and Auth0 to secure a feature-complete Express.js API. Tutorial on how to use Auth0 to implement authorization in...
Read more >
How to get rid of the warning .ts file is part of the TypeScript ...
Explicitly adding the 'files' array with the 2 entry files above, cleared the warnings.
Read more >
The starting point for learning TypeScript
TypeScript Documentation. Get Started. Quick introductions based on your background or preference. TS for the New Programmer · TypeScript for JS ...
Read more >
Role-Based Access Control (RBAC) | RedwoodJS Docs
But that will fail with an error: • Step 0 Added the required column `role` to the `User` table without a default value....
Read more >
Widgets Development Guide | ThingsBoard Community Edition
There is no subscription data and no special widget API was used. Only custom showAlert function was implemented showing an alert with the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found