gpt4 book ai didi

Property `role` does not exist on type `User | AdapterUser` in NextAuth(NextAuth中的类型`User|AdapterUser`上不存在属性`role`)

转载 作者:bug小助手 更新时间:2023-10-28 20:49:21 24 4
gpt4 key购买 nike



I am attempting to build a NextJS application that implements NextAuth. I am encountering the following error in my [...nextauth].ts when configuring my callbacks:

我正在尝试构建一个实现NextAuth的NextJS应用程序。在配置回调时,我在我的[...nextauth].ts中遇到以下错误:


Type error: Property 'role' does not exist on type 'User | AdapterUser'.
Property 'role' does not exist on type 'User'.

56 | jwt: async ({ token, user }) => {
57 | // First time JWT callback is run, user object is available
> 58 | if (user && user.id && user.role) {
| ^
59 | token.id = user.id;
60 | token.role = user.role;
61 | }

The complete callback section of code looks like this:

代码的完整回调部分如下所示:


  callbacks: {
jwt: async ({ token, user }) => {
// First time JWT callback is run, user object is available
if (user && user.id && user.role) {
token.id = user.id;
token.role = user.role;
}
return token;
},
session: async ({ session, token }) => {
if (token && token.id && token.role) {
session.id = token.id;
session.role = token.role;
}
return session;
},
},

I am using the CredentialProvider with an email and a password. Here is authorize:

我正在使用CredentialProvider,并提供了电子邮件和密码。这里是授权:


    async authorize(credentials) {
if (!credentials || !credentials.email) return null;

const dbCredentials = await executeAccountQuery(
`SELECT password FROM auth WHERE email=?`,
[credentials.email]
);

if (Array.isArray(dbCredentials) && "password" in dbCredentials[0]) {
const isValid = await compare(
credentials.password,
dbCredentials[0].password
);

if (isValid) {
return {
id: "5",
role: 99,
name: "John Smith",
email: credentials.email,
};
}
return null;
}
return null; // login failed
},

Because of the way the authorize function is working, I know for a fact that the User object will have a role appended to it (because I have tested it), but I cannot figure out a way to handle this error and get rid it.

由于authorize函数的工作方式,我知道User对象将有一个角色附加到它(因为我已经测试过了),但我无法找到一种方法来处理这个错误并消除它。


Similarily, I also get an error with the session callback where the session.id and session.role are also not present on Session.

类似地,我还收到了一个关于会话回调的错误,其中会话上也没有会话.id和会话角色。


更多回答
优秀答案推荐

After a lot of digging through the internet and some trial and error, I was able to figure out how to solve this problem.

经过在互联网上的大量挖掘和一些反复试验,我终于找到了解决这个问题的方法。


You will need to manually extend the Session, User and JWT types by doing the following:

您需要通过执行以下操作手动扩展会话、用户和JWT类型:



  1. Create types/next-auth.d.ts in your root project directory.

  2. Insert the following into the file:


import { Session } from "next-auth";
import { JWT } from "next-auth/jwt";

declare module "next-auth" {
interface Session {
id: string;
role: number;
}

interface User {
id: string;
role: number;
}
}

declare module "next-auth/jwt" {
interface JWT {
id: string;
role: number;
}
}



  1. Run npm run build and verify that it builds correctly.



Type Definition:

类型定义:


types/next-auth.d.ts:

TYES/NEXT-Auth.d.ts:


import { DefaultUser } from 'next-auth';
declare module 'next-auth' {
interface Session {
user?: DefaultUser & { id: string; role: string };
}
interface User extends DefaultUser {
role: string;
}
}


Usage:

用途:


async session({ session, user }) {
if (session?.user) {
session.user.id = user.id;
session.user.role = user.role;
}

return session;
}


Just in case it helps anyone else, here's the official documentation: https://next-auth.js.org/getting-started/typescript#main-module

如果它对其他人有帮助,这里是官方文档:https://next-auth.js.org/getting-started/typescript#main-module


It also doesn't really matter where you declare your module. I was able to extend my User type with a role in the following way in the same file where I set my auth options (src/server/auth.ts).

在哪里声明模块也无关紧要。在设置身份验证选项的同一文件(src/server/auth.ts)中,我能够通过以下方式使用角色扩展我的用户类型。


import {
type DefaultSession,
type DefaultUser,
} from "next-auth";

declare module "next-auth" {
interface Session extends DefaultSession {
user: DefaultSession["user"] & {
id: string;
};
}
interface User extends DefaultUser {
role: Role;
}
}

更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com