Login in with credentials with next auth

Hello! I'm trying to learn how to use next auth with credentials. So i got this next auth settings:
export const authOptions: NextAuthOptions = {
session: {
strategy: "jwt",
},
providers: [
CredentialsProvider({
type: "credentials",
credentials: {},
async authorize(credentials, req) {
const { email, password } = credentials as {
email: string;
password: string;
};
// perform you login logic
// find out user from db
const user = await prisma.user.findFirst({
where: {
email: email,
password: password,
},
});
if (!user) {
throw new Error("bad user");
}
return user;
},
}),
],
};

export default NextAuth(authOptions);
export const authOptions: NextAuthOptions = {
session: {
strategy: "jwt",
},
providers: [
CredentialsProvider({
type: "credentials",
credentials: {},
async authorize(credentials, req) {
const { email, password } = credentials as {
email: string;
password: string;
};
// perform you login logic
// find out user from db
const user = await prisma.user.findFirst({
where: {
email: email,
password: password,
},
});
if (!user) {
throw new Error("bad user");
}
return user;
},
}),
],
};

export default NextAuth(authOptions);
And i'm using this function to login
const handleSubmit = () => {
try {
signIn("credentials", {
email: emailValue,
password: passwordValue,
redirect: false,
});
} catch (error) {
console.log(error);
}
};
const handleSubmit = () => {
try {
signIn("credentials", {
email: emailValue,
password: passwordValue,
redirect: false,
});
} catch (error) {
console.log(error);
}
};
, signIn is imported from next-auth. And everything seems to be okay, but that authentication is not persisting. I mean, when i login and go to index file and again to login page, I'm again unauthenticated. Whats the problem?
5 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
4b47
4b47OP3y ago
is using default next auth middleware okay?
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
4b47
4b47OP3y ago
https://next-auth.js.org/configuration/nextjs#basic-usage i was using this, and it seems to be working fine, but when i call signOut function from next auth i'm still authenticated after reload. whats the reason?
Next.js | NextAuth.js
unstable_getServerSession
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?