我正在开发 Nextjs 项目 15.0.3。 使用 next-auth.js 进行身份验证。在设置我的
app/api/auth/[...nextauth].ts
页面时,
我收到以下错误:
Module '"next-auth"' has no exported member 'AuthOptions'. Did you mean to use 'import AuthOptions from "next-auth"' instead?
Binding element 'user' implicitly has an 'any' type.
Binding element 'account' implicitly has an 'any' type.
Binding element 'profile' implicitly has an 'any' type.
这似乎是 Typescript 不兼容的问题。 附上我的 [...nextauth.ts] 代码:
import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
export const authOptions = {
providers :
[
GoogleProvider({clientId : process.env.GOOGLE_CLIENT_ID , clientSecret : process.env.GOOGLE_CLIENT_SECRET}),
],
callbacks :{
async signIn({ user, account, profile }
{ return true; },
async redirect({ url, baseUrl }
{ return "/details" }
},
};
const handler = NextAuth(authOptions);
export {handler as GET , handler as POST};
尝试从 nextauth 导入类型 {AuthOptions}。 但错误仍然存在。 还将登录和重定向的类型分别定义为
Promise<Boolean>
和 .Promise`
您有一些语法错误,请尝试这个
import NextAuth, {NextAuthOptions} from "next-auth";
import GoogleProvider from "next-auth/providers/google";
const authOptions = {
providers:
[
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID || "", // added fallback
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "" // added fallback
}),
],
callbacks: {
async signIn() { // added closing ")" and removed unused params
return true;
},
async redirect() { // added closing ")" and removed unused params
return "/details"
}
},
};
const handler = NextAuth(authOptions);
export {handler as GET, handler as POST};