Does Prisma adapter work with credentials in NextAuth?

I'm trying to get credentials login to work with Prisma adapter, but I'm running into issues, and I saw some posts on stack overflow, that it isn't possible. Did it get updated already or do I still need to use JWT sessions?
10 Replies
rocawear
rocawear•2y ago
There is already multiple threads for this question. Would suggest to search from the questions
Forsto
Forsto•2y ago
hmm I looked at a lot of older threads and github repos but the session isnt working this is my nextauth file, am I maybe missing something?
import NextAuth, { type NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
// Prisma adapter for NextAuth, optional and can be removed
import { PrismaAdapter } from "@next-auth/prisma-adapter";

import { env } from "../../../env/server.mjs";
import { prisma } from "../../../server/db/client";

export const authOptions: NextAuthOptions = {
// Include user.id on session
callbacks: {
jwt: async ({ token, user }) => {
if (user) {
token.id = user.id;
}

return token;
},
session({ session, token }) {
if (token && session.user) {
session.user.id = token.id as string;
}

return session;
},
},
adapter: PrismaAdapter(prisma),
pages: {
signIn: "/login",
},
providers: [
CredentialsProvider({
name: "credentials",
credentials: {
email: { type: "email" },
password: { type: "password" },
},
async authorize(credentials, req) {
const user = await prisma.user.findFirst({
where: {
email: credentials?.email,
password: credentials?.password,
},
});

console.log(user);

if (user)
return {
id: user.id,
};
else return null;
},
}),
],
};

export default NextAuth(authOptions);
import NextAuth, { type NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
// Prisma adapter for NextAuth, optional and can be removed
import { PrismaAdapter } from "@next-auth/prisma-adapter";

import { env } from "../../../env/server.mjs";
import { prisma } from "../../../server/db/client";

export const authOptions: NextAuthOptions = {
// Include user.id on session
callbacks: {
jwt: async ({ token, user }) => {
if (user) {
token.id = user.id;
}

return token;
},
session({ session, token }) {
if (token && session.user) {
session.user.id = token.id as string;
}

return session;
},
},
adapter: PrismaAdapter(prisma),
pages: {
signIn: "/login",
},
providers: [
CredentialsProvider({
name: "credentials",
credentials: {
email: { type: "email" },
password: { type: "password" },
},
async authorize(credentials, req) {
const user = await prisma.user.findFirst({
where: {
email: credentials?.email,
password: credentials?.password,
},
});

console.log(user);

if (user)
return {
id: user.id,
};
else return null;
},
}),
],
};

export default NextAuth(authOptions);
the user does get found in the db but the session doenst work
Lopen
Lopen•2y ago
this is how mine looks like👇
Lopen
Lopen•2y ago
Pastebin
next auth code snippet - Pastebin.com
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Lopen
Lopen•2y ago
It's not public 😬 But i can basically bootstrap a step up will next auth though if that's what u want i will do and push public
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Lopen
Lopen•2y ago
I use credentials with jwt and session
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
rocawear
rocawear•2y ago
There is already like 11 posts for this topic that has been solved
Want results from more Discord servers?
Add your server