next-auth example with multiple auth strategies?

Wondering if anyone has a good working example using multiple auth strategies with next.js. I’ve been having issues getting credentials auth working with other strategies. It’s odd because I can use credentials auth as long as I don’t use it in conjunction with the other strategies.
1 Reply
sen.py
sen.py2y ago
I'm using credentials with GoogleAuth and for not its working fine What are the issues you're facing?
export const authOptions: NextAuthOptions = {
session: {
strategy: "jwt",
},
callbacks: {
jwt({ token, account, profile }) {
if (account) {
token.accessToken = account.access_token;
token.id = profile?.sub;
}
return token;
},
session({ session, token }) {
if (session.user) {
session.user.name = token.name;
}
return session;
},
},
adapter: PrismaAdapter(prisma),
providers: [
GoogleProvider({
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
profile(profile) {
return {
id: profile.sub,
name: profile.name,
firstName: profile.given_name,
lastName: profile.family_name,
email: profile.email,
image: profile.picture,
};
},
}),
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "text", placeholder: "Email ID" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
const login = z
.object({
email: z.string().email(),
password: z.string(),
})
.safeParse(credentials);
if (!login.success) {
console.log(login.error);
throw new Error("Invalid credentials");
}

const user = await prisma.user.findUnique({
where: { email: credentials?.email },
});
if (!user || !user.password) {
throw new Error("Invalid credentials");
}

const isPasswordCorrect = await bcrypt.compare(
credentials?.password ?? "",
user.password
);
if (!isPasswordCorrect) {
throw new Error("Incorrect password");
}
return user;
},
}),
],
};
export const authOptions: NextAuthOptions = {
session: {
strategy: "jwt",
},
callbacks: {
jwt({ token, account, profile }) {
if (account) {
token.accessToken = account.access_token;
token.id = profile?.sub;
}
return token;
},
session({ session, token }) {
if (session.user) {
session.user.name = token.name;
}
return session;
},
},
adapter: PrismaAdapter(prisma),
providers: [
GoogleProvider({
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
profile(profile) {
return {
id: profile.sub,
name: profile.name,
firstName: profile.given_name,
lastName: profile.family_name,
email: profile.email,
image: profile.picture,
};
},
}),
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "text", placeholder: "Email ID" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
const login = z
.object({
email: z.string().email(),
password: z.string(),
})
.safeParse(credentials);
if (!login.success) {
console.log(login.error);
throw new Error("Invalid credentials");
}

const user = await prisma.user.findUnique({
where: { email: credentials?.email },
});
if (!user || !user.password) {
throw new Error("Invalid credentials");
}

const isPasswordCorrect = await bcrypt.compare(
credentials?.password ?? "",
user.password
);
if (!isPasswordCorrect) {
throw new Error("Incorrect password");
}
return user;
},
}),
],
};
Want results from more Discord servers?
Add your server