roo*_*ooo 11 typescript next.js prisma next-auth
我只在下一个身份验证中使用一个CredentialsProvider,但我对如何处理async authorize()自定义用户界面感到困惑。
我定义用户界面如下types/next-auth.d.ts:
import NextAuth from "next-auth"
declare module "next-auth" {
interface User {
id: string
address: string
name?: string
}
}
Run Code Online (Sandbox Code Playgroud)
这是提供者定义[...nextauth].ts:
CredentialsProvider({
name: "Ethereum",
credentials: {
message: {
label: "Message",
type: "text",
},
signature: {
label: "Signature",
type: "text",
},
},
async authorize(credentials) {
try {
const nextAuthUrl = process.env.NEXTAUTH_URL
if (!nextAuthUrl) return null
if (!credentials) return null
// [verify the credential here]
// "message" contains the verified information
let user = await prisma.user.findUnique({
where: {
address: message.address,
},
})
if (!user) {
user = await prisma.user.create({
data: {
address: message.address,
},
})
}
return {
id: user.id,
address: user.address,
name: user.name
}
} catch (e) {
console.error(e)
return null
}
},
})
Run Code Online (Sandbox Code Playgroud)
现在我看到打字稿错误async authorize(credentials)
Type '(credentials: Record<"message" | "signature", string> | undefined) => Promise<{ id: string; address: string; name: string | null; } | null>' is not assignable to type '(credentials: Record<"message" | "signature", string> | undefined, req: Pick<RequestInternal, "body" | "query" | "headers" | "method">) => Awaitable<...>'.
Type 'Promise<{ id: string; address: string; name: string | null; } | null>' is not assignable to type 'Awaitable<User | null>'.
Type 'Promise<{ id: string; address: string; name: string | null; } | null>' is not assignable to type 'PromiseLike<User | null>'.
Types of property 'then' are incompatible.
Type '<TResult1 = { id: string; address: string; name: string | null; } | null, TResult2 = never>(onfulfilled?: ((value: { id: string; address: string; name: string | null; } | null) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | ... 1 more ... | unde...' is not assignable to type '<TResult1 = User | null, TResult2 = never>(onfulfilled?: ((value: User | null) => TResult1 | PromiseLike<TResult1>) | null | undefined, onrejected?: ((reason: any) => TResult2 | PromiseLike<...>) | null | undefined) => PromiseLike<...>'.
Types of parameters 'onfulfilled' and 'onfulfilled' are incompatible.
Types of parameters 'value' and 'value' are incompatible.
Type '{ id: string; address: string; name: string | null; } | null' is not assignable to type 'User | null'.
Type '{ id: string; address: string; name: string | null; }' is not assignable to type 'User'.
Types of property 'name' are incompatible.
Type 'string | null' is not assignable to type 'string | undefined'.
Type 'null' is not assignable to type 'string | undefined'.
Run Code Online (Sandbox Code Playgroud)
Fro*_*oxx 12
遇到同样的问题。由于我不想在 .tsconfig 中打开严格模式,因此我只是将返回的对象解析authorize()为任何...
async authorize(credentials) {
// ...
return {
// ...
} as any. // <-- This here
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢它,但我认为这比关闭严格模式更好。
此外,它不会破坏后续操作中的类型安全性,因为使用返回类型的下一步将在回调中进行jwt({user}),并且类型仍然可以正常工作。
| 归档时间: |
|
| 查看次数: |
8284 次 |
| 最近记录: |