Problem, trying to get the session to make a login and register page.
Unanswered
Boreal Owl posted this in #help-forum
Boreal OwlOP
Hi, im implementing a register/login system with NextAuth, prisma and postgreSQL, when I log-in with the Oauth method, everything works fine, but if i log with the credentials (obviously with an existing user), then the session doesnt create.
7 Replies
Boreal OwlOP
CredentialsProvider({
name: "Credentials",
credentials: {
email: {
label: "Email",
type: "text",
placeholder: "John",
},
password: {
label: "Password",
type: "password",
},
},
async authorize(credentials, req) {
console.log(credentials);
const userFound = await prisma.user.findUnique({
where: {
email: credentials.email,
},
});
if (!userFound) throw new Error("User not found");
console.log(userFound);
const matchPassword = await bcrypt.compare(
credentials.password,
userFound.password
);
if (!matchPassword) throw new Error("Password isnt correct");
return {
id: userFound.id,
name: userFound.name,
email: userFound.email,
};
},
}),thats my credentialsProvider
callbacks: {
async session({ session, user, credentials }) {
session.user.role = user.role;
console.log(session);
return session;
},
},and thats the session, as you can see, im also implementing roles.
everything works fine, no errors. although i cant get the session to use the user name, email, image, etc.
Here I try to get if the user is authenticated or not.
export default function LoginButton() {
const { data: session, status } = useSession();
// console.log(session);
return (
<>
{status === "authenticated" && (
<div className="flex flex-col justify-center items-center w-full ">
//AUTHENTICATED CONTENT
</div>
)}
{status === "unauthenticated" && (
<button
className="rounded-md w-full text-green-500 hover:text-green-600 duration-200 text-l font-bold px-4 py-2 dark:border-stone-600"
onClick={() => signIn()}
>
Register/Login
</button>
)}
</>
);
}the problem is that if i log in with the credentials, it goes directly to unauthenticated, instead of authenticated