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

[Bug?]: Wrong CurrentUser type from all-currentUser.d.ts #6112

Closed
1 task done
rowin1125 opened this issue Aug 2, 2022 · 2 comments
Closed
1 task done

[Bug?]: Wrong CurrentUser type from all-currentUser.d.ts #6112

rowin1125 opened this issue Aug 2, 2022 · 2 comments

Comments

@rowin1125
Copy link
Contributor

rowin1125 commented Aug 2, 2022

What's not working?

The currentUser type in the api seems all good, however type from useAuth() in the web is not the same as the ThenArg<ReturnType<typeof getCurrentUser>>.

My tsconfig from the web.
note: I changed strict to true

{
  "compilerOptions": {
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "allowJs": true,
    "esModuleInterop": true,
    "target": "esnext",
    "module": "esnext",
    "moduleResolution": "node",
    "baseUrl": "./",
    "rootDirs": [
      "./src",
      "../.redwood/types/mirror/web/src",
      "../api/src",
      "../.redwood/types/mirror/api/src"
    ],
    "paths": {
      "src/*": [
        "./src/*",
        "../.redwood/types/mirror/web/src/*",
        "../api/src/*",
        "../.redwood/types/mirror/api/src/*"
      ],
      "types/*": ["./types/*", "../types/*"],
      "@redwoodjs/testing": ["../node_modules/@redwoodjs/testing/web"]
    },
    "typeRoots": ["../node_modules/@types", "./node_modules/@types"],
    "types": ["jest", "@testing-library/jest-dom"],
    "jsx": "preserve"
  },
  "include": [
    "src",
    "../.redwood/types/includes/all-*",
    "../.redwood/types/includes/web-*",
    "./types",
  ]
}

The `all-currentUser.d.ts

// This file was generated by RedwoodJS
import '@redwoodjs/api'
import '@redwoodjs/auth'

import { getCurrentUser } from '../../../api/src/lib/auth'

type ThenArg<T> = T extends PromiseLike<infer U> ? U : T
export type InferredCurrentUser = ThenArg<ReturnType<typeof getCurrentUser>>

type UndefinedRoles = {
  /**
   * ⚠️ Heads-Up: This is undefined unless roles key is returned from {@link getCurrentUser()}
   *
   * You may take a look at https://redwoodjs.com/docs/tutorial/chapter7/rbac for a tutorial on
   * how to add fully-fledged RBAC (Role-based Access Control) to your database model.
   */
  roles?: unknown | unknown[]
}

type Overwrite<T, U> = Omit<T, keyof U> & U

declare module '@redwoodjs/graphql-server' {
  interface GlobalContext {
    currentUser?: Overwrite<UndefinedRoles, InferredCurrentUser>
  }
}

declare module '@redwoodjs/auth' {
  interface CurrentUser extends InferredCurrentUser {}
}

If I hover over InferredCurrentUser i see the following information:

type InferredCurrentUser = {
    id: string;
    email: string;
    verified: boolean;
    roles: Role;
} | null

When looking at the AuthProvider.d.ts I can see that the currentUser type is defined in this file:

export interface CurrentUser {
    roles?: Array<string> | string;
}

How do we reproduce the bug?

You can clone this repo here. Im using docker to run the postgres db so in order to run the project you have to add these keys to your .env file:

DATABASE_URL="postgresql://rowin:password@localhost:5432/teamstasts_development?connection_limit=1"
TEST_DATABASE_URL="postgresql://rowin:password@localhost:5432/teamstasts_test?connection_limit=1"

# Used to encrypt/decrypt session cookies. Change this value and re-deploy to log out all users of your app at once.
SESSION_SECRET=<YOUR SECRET>

API_URL="/.redwood/functions"
SENDINBLUE_SMTP_KEYS=<TOKEN> # -> Should not be required

Now run yarn rw dev and if you visit for example Homepage.tsx line 10 you will see the currentUser object and the type which isn't correct.

What's your environment? (If it applies)

System:
    OS: Linux 5.13 Ubuntu 21.10 21.10 (Impish Indri)
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 16.14.2 - /tmp/xfs-4f764c8e/node
    Yarn: 3.2.1 - /tmp/xfs-4f764c8e/yarn
  Databases:
    SQLite: 3.38.2 - /home/linuxbrew/.linuxbrew/bin/sqlite3
  npmPackages:
    @redwoodjs/core: 2.1.0 => 2.1.0

Are you interested in working on this?

  • I'm interested in working on this
@rowin1125 rowin1125 added the bug/needs-info More information is needed for reproduction label Aug 2, 2022
@redwoodjs-bot redwoodjs-bot bot moved this to Triage in Main Aug 2, 2022
@redwoodjs-bot redwoodjs-bot bot added this to Main Aug 2, 2022
@dac09
Copy link
Contributor

dac09 commented Aug 3, 2022

@rowin1125 thanks for raising this. I don't believe we've shipped strict mode support just yet, where we've solved some of these issues.

You are welcome to try it in canary however, by running yarn rw upgrade -t canary - but please note there may be breaking changes that will affect you in Canary.

@Philzen
Copy link
Contributor

Philzen commented Aug 7, 2022

Related to

… in particular requirement no. 3 i believe, which refers to the disjoint of CurrentUser in web vs. api

@simoncrypta simoncrypta moved this from Triage to In progress in Main Aug 20, 2022
@simoncrypta simoncrypta removed the bug/needs-info More information is needed for reproduction label Aug 20, 2022
@simoncrypta simoncrypta removed their assignment Aug 20, 2022
@github-project-automation github-project-automation bot moved this from In progress to Done in Main Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

No branches or pull requests

4 participants