Next.js Discord

Discord Forum

Can not get .admin of user

Unanswered
Pont-Audemer Spaniel posted this in #help-forum
Open in Discord
Pont-Audemer SpanielOP
Hi, I am using typescript to get the .admin of the user but I get a typescript error. I already tried setting a next-auth.d.ts in the types folder like this
import NextAuth, { DefaultSession } from "next-auth";

declare module "next-auth" {
  /**
   * Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
   */
  interface Session {
    user: {
      admin: boolean;
    } & DefaultSession["user"];
  }
}

But it doesn't seem to work

67 Replies

that is the correct definition
you will need to show where you are accessing it
@DirtyCajunRice | AppDir you will need to show where you are accessing it
Pont-Audemer SpanielOP
I access it using useSession
@Pont-Audemer Spaniel I access it using useSession
show the error. show where you use it. show as much as you can. the existing information is not enough
@DirtyCajunRice | AppDir show the error. show where you use it. show as much as you can. the existing information is not enough
Pont-Audemer SpanielOP
next-auth.d.ts
import NextAuth, { DefaultSession } from "next-auth";

declare module "next-auth" {
  /**
   * Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
   */
  interface Session {
    user: {
      admin: boolean;
    } & DefaultSession["user"];
  }
}
@DirtyCajunRice | AppDir whats your ts config file look like
Pont-Audemer SpanielOP
{
  "compilerOptions": {
    "target": "es2017",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "checkJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "noUncheckedIndexedAccess": true,
    "baseUrl": ".",
    "paths": {
      "~/*": [
        "./src/*"
      ]
    },
    "plugins": [
      {
        "name": "next"
      }
    ]
  },
  "include": [
    ".eslintrc.cjs",
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    "**/*.cjs",
    "**/*.mjs",
    ".next/types/**/*.ts",
    "next-env.d.ts", "src/types/**/*.ts", "**/*.ts", "**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}
explicitly add that next-auth.d.ts to your include
i think thats what it is
@DirtyCajunRice | AppDir explicitly add that next-auth.d.ts to your include
Pont-Audemer SpanielOP
I added "./types/next-auth.d.ts" and restarted the ts server but doesn't seem to fix it
"include": [
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx",
    ".next/types/**/*.ts",
    "src/types/global.d.ts",
    "src/types/next-auth.d.ts"
  ],
@DirtyCajunRice | AppDir i have my types in my src dir
Pont-Audemer SpanielOP
Hmm, still doesn't work even if I includ it in the tsconfig
import { IUser } from "@/models/mongo";

declare module "next-auth" {
  /**
   * Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
   */
  interface Session extends Session {
    user: IUser;
  }
  interface User extends User {
    lastLogin?: Date;
    admin?: boolean;
  }
this works perfectly
i think it may be your editor?
@DirtyCajunRice | AppDir i think it may be your editor?
Pont-Audemer SpanielOP
I use vscode
so idk
is your typescript up to date?
Pont-Audemer SpanielOP
how do I check that
ohhh
dude
look at mine
mine extends
yours doesnt
try that
Pont-Audemer SpanielOP
like that ?
Doesn't fix
yeah
you have it set up right man.
@DirtyCajunRice | AppDir you have it set up right man.
Pont-Audemer SpanielOP
yea I followed the guide that's why i'm confused
@Pont-Audemer Spaniel yea I followed the guide that's why i'm confused
i feel like this is some vscode bullshit
does it type error when you run
or just in the editor view
@DirtyCajunRice | AppDir does it type error when you run
Pont-Audemer SpanielOP
let me try
It works
I can log the variable
with no errors
but vscode still gives me an error
yeah
boom
if it doesnt type error on dev run its not actually an error
and its vscode bullshit
Pont-Audemer SpanielOP
yea
anything I can do to fix it
i use webstorm 😦
Pont-Audemer SpanielOP
since it's kinda annoying
@DirtyCajunRice | AppDir i use webstorm 😦
Pont-Audemer SpanielOP
Hmm ok
that kinds bs is why i stopped using vscode
@Pont-Audemer Spaniel anything I can do to fix it
Try restarting the ts server
Ctrl shift p then type restarting ts server
@joulev Try restarting the ts server
Pont-Audemer SpanielOP
I already did that
If it doesn’t work then maybe do the classic purge steps
Remove node modules and .next
Reinstall dependencies
And restart vscode
ty joulevvvv
@joulev Reinstall dependencies
Pont-Audemer SpanielOP
So I delete .next and node_modules and then npm i ?
Yeah
Just a wild guess tho can’t guarantee it’ll work
if joulev solves it for you mark him as the answer. since adding it to types probably wasnt the real problem
@joulev Just a wild guess tho can’t guarantee it’ll work
Pont-Audemer SpanielOP
Still doesn't wrok
:sad:
I'll just set a ts-ignore so it stops giveing the error
Yeah ig then just ignore vscode
Dumdum sometimes