You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey :)
I've been having an issue for days now and can't figure it out.
My sign in with google works great but when I add the credentials provider, it doesn't work in production (deployed on vercel) - and yes I have the environment variables set up.
I don't know what's wrong because it happens when I just try to load the page and not even trying to sign in..
I provided my code below for [...nextauth].js. I would love some help or guidance for what I'm missing or did wrong.
The project is Next.js V12, next auth v4.
Thank you in advance :)
import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import CredentialsProvider from "next-auth/providers/credentials";
import { createOrFetchUser } from "../getUser";
import { getUserByCredentials } from "../getUserByCredentials";
export const authOptions = {
// Configure one or more authentication providers
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
CredentialsProvider({
name: "Email and Password",
credentials: {},
authorize: async ({ email, password, createdUserBySignUp }) => {
// in case of sign up
if (createdUserBySignUp) {
const signUpUser = JSON.parse(createdUserBySignUp);
return signUpUser;
}
// in case of sign in
const user = await getUserByCredentials(email, password);
if (!user) {
return null;
}
return user;
},
}),
// ...add more providers here
],
pages: {
signIn: "/signin",
},
callbacks: {
async jwt({ token, account }) {
// Persist the OAuth access_token to the token right after signin
if (account) {
token.accessToken = account.access_token;
}
return token;
},
async session({ session, token, user }) {
// Send properties to the client, like an access_token from a provider.
if (session && session.user) {
const extraUserInfo = await createOrFetchUser(
session.user.name,
session.user.email,
session.user.image
);
session.accessToken = token.accessToken;
session.user = { ...session.user, ...extraUserInfo };
}
return session;
},
},
};
export default NextAuth(authOptions);
`
### How to reproduce ☕️
`
import NextAuth from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import CredentialsProvider from "next-auth/providers/credentials";
import { createOrFetchUser } from "../getUser";
import { getUserByCredentials } from "../getUserByCredentials";
export const authOptions = {
// Configure one or more authentication providers
providers: [
GoogleProvider({
clientId: process.env.GOOGLE_CLIENT_ID,
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
}),
CredentialsProvider({
name: "Email and Password",
credentials: {},
authorize: async ({ email, password, createdUserBySignUp }) => {
// in case of sign up
if (createdUserBySignUp) {
const signUpUser = JSON.parse(createdUserBySignUp);
return signUpUser;
}
// in case of sign in
const user = await getUserByCredentials(email, password);
if (!user) {
return null;
}
return user;
},
}),
// ...add more providers here
],
pages: {
signIn: "/signin",
},
callbacks: {
async jwt({ token, account }) {
// Persist the OAuth access_token to the token right after signin
if (account) {
token.accessToken = account.access_token;
}
return token;
},
async session({ session, token, user }) {
// Send properties to the client, like an access_token from a provider.
if (session && session.user) {
const extraUserInfo = await createOrFetchUser(
session.user.name,
session.user.email,
session.user.image
);
session.accessToken = token.accessToken;
session.user = { ...session.user, ...extraUserInfo };
}
return session;
},
},
};
export default NextAuth(authOptions);
questionAsk how to do something or how something works
1 participant
Converted from issue
This discussion was converted from issue #5887 on November 28, 2022 11:12.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Question 💬
Hey :)
I've been having an issue for days now and can't figure it out.
My sign in with google works great but when I add the credentials provider, it doesn't work in production (deployed on vercel) - and yes I have the environment variables set up.
I don't know what's wrong because it happens when I just try to load the page and not even trying to sign in..
I provided my code below for [...nextauth].js. I would love some help or guidance for what I'm missing or did wrong.
The project is Next.js V12, next auth v4.
Thank you in advance :)
Contributing 🙌🏽
No, I am afraid I cannot help regarding this
Beta Was this translation helpful? Give feedback.
All reactions