I have created a authentication function using nextAuth.
Here is my login page:
const loginHandler = async (e) => {
e.preventDefault();
if (email && password) {
signIn("credentials", {
redirect: false,
email,
password,
}).then((res) => {
console.log("res",res)
}
}
}
And this is my nextAuth.js Page:
import NextAuth from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import main from "../../../database/conn";
const { AUTH_SECRET, HOST } = process.env;
export const authOptions = {
secret: AUTH_SECRET,
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Username", type: "text", placeholder: "email" },
password: { label: "Password", type: "password" },
},
async authorize(credentials, req) {
try {
await main().catch((err) => console.error(err));
const response = await fetch(`${HOST}/api/users/login`, {
method: "POST",
body: JSON.stringify(credentials),
headers: { "Content-Type": "application/json" },
credentials: "include", // Include cookies
});
const { ok, status } = response;
if (status === 200 && ok) {
const user = await response.json();
console.log({ user });
return user;
}
// Handle non-200 response
console.error(`Error: ${status}`);
return null;
} catch (error) {
console.error(`Error: ${error}`);
return null;
}
},
}),
],
session: {
strategy: "jwt",
maxAge: 1 * 1 * 10 * 60, // Set to 10 min in seconds
},
callbacks: {
async session({ session, token }) {
if (token && token.id) {
session.user.id = token.id;
session.user.accessToken = token.accessToken;
session.user.name = token.name;
}
return session;
},
async jwt({ token, user, account, profile, isNewUser }) {
if (user && user._id) {
token.id = user._id;
token.accessToken = user.token;
token.name = user.fullname;
}
return token;
},
},
};
export default NextAuth(authOptions);
After clicking on login button I am getting the response as this

Getting response as 200,but why the session is NULL??
I tried to debug the code add lots of console statements inside the nextAuth.js, they were not printing in the terminal.
But the after clicking on login, the invalid password is also giving status as 200.
I was expecting to get invalid login/password for wrong password and creating a session for authenticated user.