提问人:Xhris 提问时间:11/16/2023 更新时间:11/16/2023 访问量:20
Next身份验证凭据重定向错误,控制台中没有线索日志
NextAuth Credential redirect error with no clues logs in console
问:
我正在尝试在我的 NextJs 应用程序中使用。但是,在用户使用正确的凭据登录应用后,不会发生任何反应。NextAuth Credentials
预期行为是在身份验证成功后将用户重定向到仪表板,但没有任何反应,没有登录,选项卡中没有登录或任何提供线索的内容。请帮助调试这个。谢谢。console
Network
以下是我的代码副本,来自负责的文件:
/[...nextauth].js
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import { PrismaAdapter } from "@auth/prisma-adapter";
import prisma from '../../../utils/db';
import { compare } from "bcrypt";
export const authOptions = {
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "email", placeholder: "[email protected]" },
password: { label: "Password", type: "password" }
},
async authorize(credentials) {
let existingUser = null;
try {
if (!credentials?.email || !credentials?.password) {
throw new Error("Missing credentials");
}
existingUser = await prisma.user.findUnique({
where: {
email: credentials?.email
},
});
if (!existingUser) {
throw new Error("User not found");
}
const passwordMatch = await compare(credentials?.password, existingUser.password);
if (!passwordMatch) {
throw new Error("Invalid password");
}
return {
id: `${existingUser.id}`,
email: existingUser.email
};
} catch (error) {
// Log the error or handle it appropriately
console.error("Authentication error:", error.message);
return null;
}
}
}),
],
adapter: PrismaAdapter(prisma),
session: {
strategy: 'jwt',
},
secret: process.env.NEXTAUTH_SECRET,
pages: {
signIn: '/login',
signOut: '/login',
error: '/404',
dashboard: '/dashboard',
},
debug: true,
};
export default NextAuth(authOptions);
/signIn.js
//SignIn
import React, { useState } from 'react';
import { useRouter } from 'next/navigation';
import Link from 'next/link';
import Loading from './Loading';
import PasswordReset from './PassReset';
import { signIn } from 'next-auth/react';
import { useSession } from 'next-auth/react';
const SignIn = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const [open, setOpen] = useState(false);
const [loading, setLoading] = useState(false);
const { data: session } = useSession();
const router = useRouter();
const handleOnSubmit = async (e) => {
e.preventDefault();
try {
console.log("Attempting to sign in with:", email, password);
const signInData = await signIn('Credentials', {
email,
password,
// redirect: false,
});
console.log("signInData:", signInData);
if (signInData.ok & session) {
console.log("Sign-in successful!");
router.push('/dashboard');
} else {
console.error('Authentication failed:', signInData.error);
setErrors(`Authentication failed: ${signInData.error}`);
}
} catch (error) {
console.error('Authentication error:', error.message);
setErrors(`Authentication error: ${error.message}`);
}
};
const setErrors = (error) => {
return error.message
};
const openModal = (e) => {
e.preventDefault();
setOpen(true);
};
return (
<div className="">
<div className="">
<>
<form onSubmit={handleOnSubmit} className="">
<label htmlFor="email" >
Email address
</label>
<input
type="email"
label="Email address"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
<label htmlFor="password">
Password
</label>
<input
type="password"
label="Password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/>
</form>
</>
</div>
</div>
);
};
prisma
import { PrismaClient } from "@prisma/client";
import { withAccelerate } from "@prisma/extension-accelerate";
let prisma: PrismaClient;
const globalForPrisma = global as any;
if (process.env.NODE_ENV === "production") {
prisma = new PrismaClient();
} else {
// In development, use a global singleton to avoid multiple instances in hot-reloading
if (!globalForPrisma.prisma) {
globalForPrisma.prisma = new PrismaClient().$extends(withAccelerate());
}
prisma = globalForPrisma.prisma;
}
export default prisma;
答: 暂无答案
评论