Skip to content

Commit

Permalink
Merge branch 'main' of github.com:redwoodjs/redwood into fix/generate…
Browse files Browse the repository at this point in the history
…-types-on-setup-auth

* 'main' of github.com:redwoodjs/redwood: (33 commits)
  fix(generators): Dont set tests=false in sdl generators by default (redwoodjs#6195)
  Harmonize optional chaining across all `hasRole` flavors & reduce duplication, avoiding errors on empty currentUser (redwoodjs#6159)
  Amend import order in auth templates according to ESLint rules introduced in v2.0 (redwoodjs#6059)
  docs(tutorial): Adds more TS hints in the toturial (redwoodjs#6115)
  Revert auth provider refactor (redwoodjs#6135)
  fix(deps): update docusaurus monorepo to v2.0.1 (redwoodjs#6176)
  fix(deps): update typescript-eslint monorepo to v5.33.0 (redwoodjs#6192)
  fix(deps): update dependency yargs-parser to v21.1.1 (redwoodjs#6191)
  fix(deps): update dependency fastify to v4.4.0 (redwoodjs#6190)
  fix(deps): update dependency @testing-library/user-event to v14.4.2 (redwoodjs#6189)
  chore(deps): update dependency @types/vscode to v1.70.0 (redwoodjs#6188)
  chore(deps): update dependency @types/prettier to v2.7.0 (redwoodjs#6187)
  fix(deps): update dependency systeminformation to v5.12.4 (redwoodjs#6186)
  chore(deps): update dependency @clerk/types to v2.21.0 (redwoodjs#6179)
  chore(deps): update dependency lerna to v5.4.0 (redwoodjs#6180)
  fix(deps): update prisma monorepo to v4.1.1 (redwoodjs#6181)
  chore(deps): update dependency @clerk/clerk-js to v3.17.0 (redwoodjs#6178)
  fix(deps): update storybook monorepo to v6.5.10 (redwoodjs#6177)
  fix(deps): update dependency @actions/core to v1.9.1 (redwoodjs#6175)
  chore(deps): update dependency magic-sdk to v9 (redwoodjs#6138)
  ...
  • Loading branch information
dac09 committed Aug 10, 2022
2 parents 6b4e8fb + 0156be9 commit 4e27dec
Show file tree
Hide file tree
Showing 116 changed files with 5,997 additions and 3,713 deletions.
2 changes: 1 addition & 1 deletion .github/actions/check_test_project_fixture/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "check_test_project_fixture",
"private": true,
"dependencies": {
"@actions/core": "1.9.0",
"@actions/core": "1.9.1",
"@actions/exec": "1.1.1"
},
"packageManager": "[email protected]"
Expand Down
20 changes: 15 additions & 5 deletions .github/actions/check_test_project_fixture/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ __metadata:
version: 6
cacheKey: 8c0

"@actions/core@npm:1.9.0":
version: 1.9.0
resolution: "@actions/core@npm:1.9.0"
"@actions/core@npm:1.9.1":
version: 1.9.1
resolution: "@actions/core@npm:1.9.1"
dependencies:
"@actions/http-client": ^2.0.1
checksum: 66a238981d7f9aa3c8ca0d80eb87aa291203dabd5c8e60e50d5075632c643883337f342fd677470d6cfd3588c483f32897fbb5c03a400b9c60171a32d2cbeb8d
uuid: ^8.3.2
checksum: 9e568d0df7d659ad0738da66f167d411714f7351fab527fcb91dd6afbeb58006e6cf1f752adc567aab1b9043f96581b2457746f394f7a756f7b14b554d7c4710
languageName: node
linkType: hard

Expand Down Expand Up @@ -43,7 +44,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "check_test_project_fixture@workspace:."
dependencies:
"@actions/core": 1.9.0
"@actions/core": 1.9.1
"@actions/exec": 1.1.1
languageName: unknown
linkType: soft
Expand All @@ -54,3 +55,12 @@ __metadata:
checksum: e27e7e896f2426c1c747325b5f54efebc1a004647d853fad892b46d64e37591ccd0b97439470795e5262b5c0748d22beb4489a04a0a448029636670bfd801b75
languageName: node
linkType: hard

"uuid@npm:^8.3.2":
version: 8.3.2
resolution: "uuid@npm:8.3.2"
bin:
uuid: dist/bin/uuid
checksum: bcbb807a917d374a49f475fae2e87fdca7da5e5530820ef53f65ba1d12131bd81a92ecf259cc7ce317cbe0f289e7d79fdfebcef9bfa3087c8c8a2fa304c9be54
languageName: node
linkType: hard
2 changes: 1 addition & 1 deletion .github/actions/only_doc_changes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "only_doc_changes",
"private": true,
"dependencies": {
"@actions/core": "1.9.0",
"@actions/core": "1.9.1",
"@actions/exec": "1.1.1"
},
"packageManager": "[email protected]"
Expand Down
20 changes: 15 additions & 5 deletions .github/actions/only_doc_changes/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ __metadata:
version: 6
cacheKey: 8c0

"@actions/core@npm:1.9.0":
version: 1.9.0
resolution: "@actions/core@npm:1.9.0"
"@actions/core@npm:1.9.1":
version: 1.9.1
resolution: "@actions/core@npm:1.9.1"
dependencies:
"@actions/http-client": ^2.0.1
checksum: 66a238981d7f9aa3c8ca0d80eb87aa291203dabd5c8e60e50d5075632c643883337f342fd677470d6cfd3588c483f32897fbb5c03a400b9c60171a32d2cbeb8d
uuid: ^8.3.2
checksum: 9e568d0df7d659ad0738da66f167d411714f7351fab527fcb91dd6afbeb58006e6cf1f752adc567aab1b9043f96581b2457746f394f7a756f7b14b554d7c4710
languageName: node
linkType: hard

Expand Down Expand Up @@ -43,7 +44,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "only_doc_changes@workspace:."
dependencies:
"@actions/core": 1.9.0
"@actions/core": 1.9.1
"@actions/exec": 1.1.1
languageName: unknown
linkType: soft
Expand All @@ -54,3 +55,12 @@ __metadata:
checksum: e27e7e896f2426c1c747325b5f54efebc1a004647d853fad892b46d64e37591ccd0b97439470795e5262b5c0748d22beb4489a04a0a448029636670bfd801b75
languageName: node
linkType: hard

"uuid@npm:^8.3.2":
version: 8.3.2
resolution: "uuid@npm:8.3.2"
bin:
uuid: dist/bin/uuid
checksum: bcbb807a917d374a49f475fae2e87fdca7da5e5530820ef53f65ba1d12131bd81a92ecf259cc7ce317cbe0f289e7d79fdfebcef9bfa3087c8c8a2fa304c9be54
languageName: node
linkType: hard
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- CreateTable
CREATE TABLE "UserExample" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"email" TEXT NOT NULL,
"name" TEXT
);

-- CreateTable
CREATE TABLE "Post" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"title" TEXT NOT NULL,
"body" TEXT NOT NULL,
"authorId" INTEGER NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User" ("id") ON DELETE RESTRICT ON UPDATE CASCADE
);

-- CreateTable
CREATE TABLE "User" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"email" TEXT NOT NULL,
"hashedPassword" TEXT NOT NULL,
"fullName" TEXT NOT NULL,
"salt" TEXT NOT NULL,
"resetToken" TEXT,
"resetTokenExpiresAt" DATETIME,
"roles" TEXT
);

-- CreateIndex
CREATE UNIQUE INDEX "UserExample_email_key" ON "UserExample"("email");

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- CreateTable
CREATE TABLE "Contact" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" TEXT NOT NULL,
"email" TEXT NOT NULL,
"message" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
6 changes: 2 additions & 4 deletions __fixtures__/test-project/api/src/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,9 @@ export const hasRole = (roles: AllowedRoles): boolean => {
return currentUserRoles?.some((allowedRole) =>
roles.includes(allowedRole)
)
} else if (typeof context?.currentUser?.roles === 'string') {
} else if (typeof currentUserRoles === 'string') {
// roles to check is an array, currentUser.roles is a string
return roles.some(
(allowedRole) => context.currentUser?.roles === allowedRole
)
return roles.some((allowedRole) => currentUserRoles === allowedRole)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const standard = defineScenario<Prisma.PostCreateArgs>({
body: 'String',
author: {
create: {
email: 'String6109395',
email: 'String7710942',
hashedPassword: 'String',
fullName: 'String',
salt: 'String',
Expand All @@ -24,7 +24,7 @@ export const standard = defineScenario<Prisma.PostCreateArgs>({
body: 'String',
author: {
create: {
email: 'String7342380',
email: 'String6732615',
hashedPassword: 'String',
fullName: 'String',
salt: 'String',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { Prisma, User } from '@prisma/client'

import type { ScenarioData } from '@redwoodjs/testing/api'

export const standard = defineScenario<Prisma.UserCreateArgs>({
user: {
one: {
data: {
email: 'String1552241',
hashedPassword: 'String',
fullName: 'String',
salt: 'String',
},
},
two: {
data: {
email: 'String7382597',
hashedPassword: 'String',
fullName: 'String',
salt: 'String',
},
},
},
})

export type StandardScenario = ScenarioData<User, 'user'>
5 changes: 4 additions & 1 deletion __fixtures__/test-project/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,8 @@
"prisma": {
"seed": "yarn rw exec seed"
},
"packageManager": "[email protected]"
"packageManager": "[email protected]",
"resolutions": {
"jest": "28.1.3"
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from '@redwoodjs/testing/web'
import { render } from '@redwoodjs/testing/web'

import { Loading, Empty, Failure, Success } from './AuthorCell'
import { standard } from './AuthorCell.mock'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from '@redwoodjs/testing/web'
import { render } from '@redwoodjs/testing/web'

import { Loading, Empty, Failure, Success } from './BlogPostCell'
import { standard } from './BlogPostCell.mock'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from '@redwoodjs/testing/web'
import { render } from '@redwoodjs/testing/web'

import { Loading, Empty, Failure, Success } from './BlogPostsCell'
import { standard } from './BlogPostsCell.mock'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen } from '@redwoodjs/testing/web'
import { render } from '@redwoodjs/testing/web'

import { Loading, Empty, Failure, Success } from './WaterfallBlogPostCell'
import { standard } from './WaterfallBlogPostCell.mock'
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/cli-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -1826,15 +1826,15 @@ yarn redwood setup tsconfig
### setup ui
Set up a UI design or style library. Right now the choices are [Chakra UI](https://chakra-ui.com/), [TailwindCSS](https://tailwindcss.com/) and [WindiCSS](https://windicss.org/).
Set up a UI design or style library. Right now the choices are [TailwindCSS](https://tailwindcss.com/), [Chakra UI](https://chakra-ui.com/), [Mantine UI](https://ui.mantine.dev/) and [WindiCSS](https://windicss.org/).
```
yarn rw setup ui <library>
```
| Arguments & Options | Description |
| :------------------ | :-------------------------------------------------------------------------- |
| `library` | Library to configure. Choices are `chakra-ui`, `tailwindcss` and `windicss` |
| `library` | Library to configure. Choices are `chakra-ui`, `tailwindcss`, `mantine`, and `windicss` |
| `--force, -f` | Overwrite existing configuration |
## storybook
Expand Down
18 changes: 18 additions & 0 deletions docs/docs/tutorial/chapter2/cells.md
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,24 @@ export const Success = ({ posts }: CellSuccessProps<ArticlesQuery>) => {
</TabItem>
</Tabs>

<ShowForTs>

:::tip Using generated types

At this point, you might see an error in your Cell while trying to import from `types/graphql`: "The type ArticlesQuery does not exist"

When you have the dev server (via `yarn rw dev`) running, the CLI watches files for changes and triggers type generation automatically, but you can trigger it manually too by running:

```bash
yarn rw g types
```

This looks at your Cell's `QUERY` and—as long as it's valid—tries to automatically create a TypeScript type for you to use in your code.

:::

</ShowForTs>

Let's plug this cell into our `HomePage` and see what happens:

<Tabs groupId="js-ts">
Expand Down
14 changes: 5 additions & 9 deletions docs/docs/tutorial/chapter4/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,19 +751,17 @@ export const hasRole = (roles) => {
return currentUserRoles?.some((allowedRole) =>
roles.includes(allowedRole)
)
} else if (typeof context?.currentUser?.roles === 'string') {
} else if (typeof currentUserRoles === 'string') {
// roles to check is an array, currentUser.roles is a string
return roles.some(
(allowedRole) => context.currentUser?.roles === allowedRole
)
return roles.some((allowedRole) => currentUserRoles === allowedRole)
}
}

// roles not found
return false
}

export const requireAuth = ({ roles }) => {
export const requireAuth = ({ roles } = {}) => {
if (!isAuthenticated()) {
throw new AuthenticationError("You don't have permission to do that.")
}
Expand Down Expand Up @@ -818,11 +816,9 @@ export const hasRole = (roles: AllowedRoles): boolean => {
return currentUserRoles?.some((allowedRole) =>
roles.includes(allowedRole)
)
} else if (typeof context?.currentUser?.roles === 'string') {
} else if (typeof currentUserRoles === 'string') {
// roles to check is an array, currentUser.roles is a string
return roles.some(
(allowedRole) => context.currentUser?.roles === allowedRole
)
return roles.some((allowedRole) => currentUserRoles === allowedRole)
}
}

Expand Down
58 changes: 58 additions & 0 deletions docs/docs/tutorial/chapter7/rbac.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,64 @@ export const getCurrentUser = async (session) => {
</TabItem>
</Tabs>
<ShowForTs>
### Fixing the hasRole function
At this point, you might notice an error in your `api/src/lib/auth.ts` file, in the `hasRole` function. TypeScript is trying to help you here, by highlighting that roles can never be an array of strings:
```ts title="api/src/lib/auth.ts"
export const hasRole = (roles: AllowedRoles): boolean => {

// ...

} else if (Array.isArray(currentUserRoles)) {
// 👇 TypeScript will now be telling you 'some' doesn't exist on type never:
// highlight-next-line
return currentUserRoles?.some((allowedRole) => roles === allowedRole)
}
}
```
This is because we now know that the type of `currentUser.roles` is a `string` based on the type being returned from Prisma. So you can safely remove the block of code where it's checking if roles is an array:
```diff title="api/src/lib/auth.ts"
export const hasRole = (roles: AllowedRoles): boolean => {
if (!isAuthenticated()) {
return false
}

const currentUserRoles = context.currentUser?.roles

if (typeof roles === 'string') {
if (typeof currentUserRoles === 'string') {
// roles to check is a string, currentUser.roles is a string
return currentUserRoles === roles
- } else if (Array.isArray(currentUserRoles)) {
- // roles to check is a string, currentUser.roles is an array
- return currentUserRoles?.some((allowedRole) => roles === allowedRole)
}
}

if (Array.isArray(roles)) {
if (Array.isArray(currentUserRoles)) {
// roles to check is an array, currentUser.roles is an array
return currentUserRoles?.some((allowedRole) =>
roles.includes(allowedRole)
)
} else if (typeof currentUserRoles === 'string') {
// roles to check is an array, currentUser.roles is a string
return roles.some((allowedRole) => currentUserRoles === allowedRole)
}
}

// roles not found
return false
}
```
</ShowForTs>
### Restricting Access via Routes
The easiest way to prevent access to an entire URL is via the Router. The `<Private>` component takes a prop `roles` in which you can give a list of only those role(s) that should have access:
Expand Down
Loading

0 comments on commit 4e27dec

Please sign in to comment.