Working with next-auth credentials
Answered
Standard Chinchilla posted this in #help-forum
Standard ChinchillaOP
I am trying to work with next-auth credentials rn and am facing issues where the session isn't created? This is my config for the next-auth
export const authOptions: NextAuthOptions = {
session: {
strategy: "database",
maxAge: 30 * 24 * 60 * 60,
updateAge: 24 * 60 * 60,
},
callbacks: {
session: ({ session, user }) => {
console.log(session);
return {
...session,
user: {
...session.user,
id: user.id,
},
};
},
},
adapter: PrismaAdapter(prisma),
pages: {
signIn: "/signin",
},
providers: [
GoogleProvider({
id: "google",
clientId: process.env.GOOGLE_CLIENT_ID!,
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
}),
CredentialsProvider({
name: "Sign In with email",
id: "credentials",
credentials: {
email: {
label: "Email",
type: "email",
placeholder: "Enter your email here",
},
password: {
label: "Password",
type: "password",
placeholder: "Enter your password",
},
},
async authorize(credentials, request) {
if (!credentials) {
throw new Error("No credentials supplied");
}
const user = await prisma.user.findUnique({
where: {
email: credentials.email,
},
});
if (user && user.password) {
const passwordMatch = await compare(
credentials.password,
user.password
);
if (!passwordMatch) {
throw new Error("Password doesn't match");
}
return {
email: user.email,
name: user.name,
id: user.id,
};
} else {
throw new Error("No user found");
}
},
}), this doesn't log anything in the sessionAnswered by Satin Angora
@Standard Chinchilla https://next-auth.js.org/configuration/providers/credentials The "Note" might be important for you:
"The Credentials provider can only be used if JSON Web Tokens are enabled for sessions. Users authenticated with the Credentials provider are not persisted in the database."
You cannot use CredentialsProvider with database sessions, only jwt session strategy is supported by next-auth. All other providers work fine with database sessions, that's why your google authentication is working fine.
"The Credentials provider can only be used if JSON Web Tokens are enabled for sessions. Users authenticated with the Credentials provider are not persisted in the database."
You cannot use CredentialsProvider with database sessions, only jwt session strategy is supported by next-auth. All other providers work fine with database sessions, that's why your google authentication is working fine.
19 Replies
Standard ChinchillaOP
Would be happy if someone can help me 😄
https://next-auth.js.org/providers/credentials#example---username--password i read here that it creates a jwt instead of attaching it to the session
whcih seems true
but I mentioned database specifically?
whereas if I am using google provider to login it creates a session?
+ if I use the same email as credentials in google it throws error? OAuthAccountNotLinked somethingg
Shouldn't it create an account for it? (Because user already exists)
const user = await ctx.prisma.user.findUnique({
where: {
email: input.email,
},
});
if (user) {
throw new TRPCError({
code: "CONFLICT",
message: "User already exists",
});
}
const hashedPassword = await hash(input.password, 10);
const newUser = await ctx.prisma.user.create({
data: {
email: input.email,
password: hashedPassword,
name: input.name,
accounts: {
create: {
type: "credentials",
providerAccountId: input.email,
provider: "credentials",
},
},
},
});
return {
status: 200,
message: "Created User Successfully",
result: newUser.id,
};
}), this is how my create user is looking for the credentials thingsignup route basically
callbacks: {
session: ({ session, user }) => {
console.log("INSIDE SESSION.");
console.log(session, user);
return {
...session,
user: {
...session.user,
id: user.id,
},
};
},
}, this doesn't even get called?with
CredentialsProviderPlease tag me if someone knows anything about this 🙂
Cause i am dead stuck
if this doesn't work then I might have to move to other options
and drop next-auth
Satin Angora
@Standard Chinchilla https://next-auth.js.org/configuration/providers/credentials The "Note" might be important for you:
"The Credentials provider can only be used if JSON Web Tokens are enabled for sessions. Users authenticated with the Credentials provider are not persisted in the database."
You cannot use CredentialsProvider with database sessions, only jwt session strategy is supported by next-auth. All other providers work fine with database sessions, that's why your google authentication is working fine.
"The Credentials provider can only be used if JSON Web Tokens are enabled for sessions. Users authenticated with the Credentials provider are not persisted in the database."
You cannot use CredentialsProvider with database sessions, only jwt session strategy is supported by next-auth. All other providers work fine with database sessions, that's why your google authentication is working fine.
Answer
Standard ChinchillaOP
;-;
I c
sucks