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

fix(docs): Fix & make verifyOwnership consistent #8596

Merged

Conversation

drikusroor
Copy link
Contributor

Currently, Chapter 7 (Accessing currentUser in the API Side) introduces a helper function to check whether the currentUser "owns" the post it wants to modify. However, the helper function is referred to as verifyOwnership and later as validateOwnership.

This PR aims to:

  • De-duplicate this naming conflict.
  • De-duplicate the two functions' parameters (destructured id)
  • Add missing arrow in arrow function

Also, before the function is introduced, an inline check is done (see here) which returns true instead of the db.post.update(...) result. We might want to convert that throw an early error, although it doesn't look very pretty.

// highlight-next-line
import { ForbiddenError } from '@redwoodjs/graphql-server'

// highlight-start
export const updatePost = async ({ id, input }) => {
  if (!(await adminPost({ id }))) {
    throw new ForbiddenError("You don't have access to this post")
  }
  // highlight-end

  return db.post.update({
    data: input,
    where: { id },
  })
}

@jtoar
Copy link
Contributor

jtoar commented Jun 12, 2023

Hey @drikusroor, the name change seems sensible and was probably just an oversight on our part, thanks! @cannikin confirming the name and signature are good?

And @drikusroor for your second point, is it just that it's a matter of three lines vs one line for the conditional? I feel like that was also a stylistic choice we made for readability, but @cannikin can you confirm that for me too?

@jtoar jtoar added the release:docs This PR only updates docs label Jun 12, 2023
@cannikin
Copy link
Member

cannikin commented Jun 12, 2023

Yeah those different names must have been an oversight on my part, whoops!

I'm not sure what I was thinking in that updatePost function, since it would never update, just return true! Yeah that negated check is a sea of parenthesis...I guess the alternative would be run the update inside of the if and throw in the else:

export const updatePost = async ({ id, input }) => {
  if (await adminPost({ id })) {
    return db.post.update({
      data: input,
      where: { id },
    })
  } else {
    throw new ForbiddenError("You don't have access to this post")
  }
}

Which might flow a little nicer? But do all the other errors actually short circuit? We should probably stick to that convention if that's the case.

@drikusroor
Copy link
Contributor Author

Hi @cannikin, thank you for your response.

With regards to:

Which might flow a little nicer? But do all the other errors actually short circuit?

I agree, your alternative "flows" nicer. I'm not sure if all the other errors will short circuit here. For now, this is just an example that is better than the one in the current documentation.

We should probably stick to that convention if that's the case.

What do you mean by this? Should the later verifyOwnership function in the documentation have the same form, or? I'm not sure what you are expecting from me here. 😅 I could already commit your proposed updatePost example if you want me to?

@cannikin
Copy link
Member

What do you mean by this? Should the later verifyOwnership function in the documentation have the same form, or? I'm not sure what you are expecting from me here. 😅 I could already commit your proposed updatePost example if you want me to?

Sorry, I just meant that if we have other errors in the tutorial that short circuit then this would should too. But if not, I would stick with the longer one that's a little easier to read (do the update in the if and throw the error in the else).

@drikusroor
Copy link
Contributor Author

Hey, sorry for the days of silence. I just wanted to let you know I've updated the updatePost method in the docs according to your proposal. ;)

@cannikin
Copy link
Member

Awesome, thank you!

@cannikin cannikin merged commit 7333926 into redwoodjs:main Jun 20, 2023
@redwoodjs-bot redwoodjs-bot bot added this to the next-release milestone Jun 20, 2023
@drikusroor drikusroor deleted the docs/fix-verify-ownership-function branch June 20, 2023 20:36
dac09 added a commit to dac09/redwood that referenced this pull request Jun 21, 2023
…redwood into codemod/replace-svg-as-components

* 'codemod/replace-svg-as-components' of github.com:dac09/redwood:
  chore(deps): update react monorepo (redwoodjs#8677)
  chore(deps): update dependency postcss to v8.4.24 (redwoodjs#8675)
  chore(deps): update dependency esbuild to v0.18.6 (redwoodjs#8670)
  chore(deps): update dependency dependency-cruiser to v13.0.4 (redwoodjs#8674)
  chore(deps): update dependency autoprefixer to v10.4.14 (redwoodjs#8668)
  chore(deps): update dependency @clerk/clerk-react to v4.20.5 (redwoodjs#8672)
  Fix typos in documentation (redwoodjs#8659)
  fix(docs): Fix & make verifyOwnership consistent (redwoodjs#8596)
  chore(deps): update dependency vite to v4.1.5 [security] (redwoodjs#8671)
  fix(deps): update dependency react-hook-form to v7.45.0 (redwoodjs#8664)
  chore(studio): update tremor to v3 (redwoodjs#8645)
  fix(deps): update dependency webpack to v5.87.0 (redwoodjs#8549)
  fix(deps): update dependency dotenv to v16.3.1 (redwoodjs#8663)
  chore(deps): update dependency @clerk/clerk-react to v4.20.4 (redwoodjs#8662)
  fix(vite): Change config for mantine and chakra to use export default (redwoodjs#8639)
  fix(deps): update storybook monorepo to v7.0.22 (redwoodjs#8652)
  Fix failing v5 codemod for DevFatalErrorPage (redwoodjs#8661)
jtoar pushed a commit that referenced this pull request Jun 22, 2023
* fix(docs): Add arrow to arrow function

* fix(docs): De-duplicate verifyOwnership & validateOwnership naming and params

* docs: Fix erroneous updatePost method example, which returned true instead of db.post.update(...)

---------

Co-authored-by: Rob Cameron <cannikin@fastmail.com>
@jtoar jtoar modified the milestones: next-release, v5.4.0 Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:docs This PR only updates docs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants