blitz-js / blitz-js/blitz

Page role authentication does not work when using `roles` instead of `role`

Open
#4,256 8 comments 1 reaction 0 assignees View on GitHub
kind/bug status/ready-to-work-on
Dominant language
TypeScript
Stars
14.1k
Forks
803
PR merge metrics
No merged PRs in 30d

Description

### What is the problem?

When creating a session and setting `publicData.roles = ["Role1", "Role2"]`, using `Page.authenticate = { role: "Role1" }` does not work and will always result in an `AuthenticationError`.

It looks like this assumes we're always setting a single `publicData.role` instead of an array of roles:
https://github.com/blitz-js/blitz/blob/fb232d126ebdbb9183417336a4ce461dc89b2a56/packages/blitz-auth/src/client/index.tsx#L352

### Paste all your error logs here:

```
AuthenticationError
```

### Paste all relevant code snippets here:

mutations/login.tsx
```ts
await ctx.session.$create({ userId: user.id, roles: ["Role1", "Role2"] })
```

pages/protected.tsx
```tsx
const Protected = () => {
return

Hi

}

export default Protected

Protected.authenticate = { role: ["Role1"] }
```

### What are detailed steps to reproduce this?

1. On login:
```ts
await ctx.session.$create({ userId: user.id, roles: ["Role1", "Role2"] })
```
2. Create `pages/protected.tsx`:
```tsx
const Protected = () => {
return

Hi

}

export default Protected

Protected.authenticate = { role: ["Role1"] }
```
3. Open browser and navigate to `/protected`

Will always get an error because the auth client only checks against the `publicData.role` instead of `publicData.roles`

### Run `blitz -v` and paste the output here:

```
Blitz version: 2.0.0-beta.35 (global)
Blitz version: 2.0.0-beta.35 (local)
macOS Ventura | darwin-arm64 | Node: v20.8.1

Package manager: pnpm

System:
OS: macOS 13.5.2
CPU: (10) arm64 Apple M1 Pro
Memory: 524.55 MB / 32.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 20.8.1 - ~/.asdf/installs/nodejs/20.8.1/bin/node
Yarn: 1.22.19 - /opt/homebrew/bin/yarn
npm: 10.1.0 - ~/.asdf/plugins/nodejs/shims/npm
npmPackages:
@blitzjs/auth: 2.0.0-beta.35 => 2.0.0-beta.35
@blitzjs/next: 2.0.0-beta.35 => 2.0.0-beta.35
@blitzjs/rpc: 2.0.0-beta.35 => 2.0.0-beta.35
@prisma/client: 5.4.2 => 5.4.2
blitz: 2.0.0-beta.35 => 2.0.0-beta.35
next: 13.5.4 => 13.5.4
prisma: 5.4.2 => 5.4.2
react: 18.2.0 => 18.2.0
react-dom: 18.2.0 => 18.2.0
typescript: ^4.8.4 => 4.9.5
```

### Please include below any other applicable logs and screenshots that show your problem:

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.