我正在尝试使用 Next-Auth 向我的 Next.js 项目添加身份验证,但是我被困在 500 internal server error
上。提交凭据后 (http://localhost:3000/api/auth/error?error=Configuration
)。
我认为这可能与在 http://localhost:3000 上运行有关,但我不确定。谁能看到我做错了什么?
pages/api/auth/[...nextauth].js
import NextAuth from 'next-auth';
import Providers from 'next-auth/providers';
const options = {
site: 'http://localhost:3000',
providers: [
Providers.Credentials({
name: 'Credentials',
credentials: {
username: { label: 'Username', type: 'text', placeholder: 'jsmith' },
password: { label: 'Password', type: 'password' },
},
authorize: async (credentials) => {
consol.log('credentials', credentials);
const user = { id: 1, name: 'J Smith', email: 'jsmith@example.com' };
if (user) {
return Promise.resolve(user);
} else {
return Promise.resolve(null);
}
},
}),
],
database: process.env.MONGO_URI,
};
export default (req, res) => NextAuth(req, res, options);
页面/index.js import { useSession } from 'next-auth/client';
export default function Home() {
const [session, loading] = useSession();
console.log('session', session);
return (
<div className="container">
<main>
{session && <p>Signed in as {session.user.email}</p>}
{!session && (
<p>
<a href="/api/auth/signin">Sign in</a>
</p>
)}
</main>
</div>
);
}
pages/_app.js import { Provider } from 'next-auth/client';
import '../styles.css';
export default ({ Component, pageProps }) => {
const { session } = pageProps;
return (
<Provider options={{ site: process.env.SITE }} session={session}>
<Component {...pageProps} />
</Provider>
);
};
next.config.js module.exports = {
env: {
MONGO_URI: '...',
SITE: 'http://localhost:3000',
},
};
任何帮助将非常感激。
最佳答案
在您的 pages/api/auth/[...nextauth].js
添加:
secret:process.env.SECRET
SECRET
必须是这样的任何字符串值:SECRET:LlKq6ZtYbr+hTC073mAmAh9/h2HwMfsFo4hrfCx6gts=
也将其添加到您的 vercel
环境。就这样,你的问题就迎刃而解了。
关于javascript - 使用 Next-Auth 进行身份验证时如何修复内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62937146/