Skip to content

Instantly share code, notes, and snippets.

@jlescalonap
Created December 20, 2023 20:25
Show Gist options
  • Save jlescalonap/1f1e0d79aa9ebb9309a90ea49afdf853 to your computer and use it in GitHub Desktop.
Save jlescalonap/1f1e0d79aa9ebb9309a90ea49afdf853 to your computer and use it in GitHub Desktop.
Archivo de configuración del NextAuth
import NextAuth from 'next-auth';
import type { NextAuthOptions } from 'next-auth';
import CredentialsProvider from 'next-auth/providers/credentials';
import User from '@/models/userModel';
import { connectDb } from '@/utils/connect';
import bcrypt from 'bcryptjs';
const authOptions: NextAuthOptions = {
providers: [
CredentialsProvider({
id: 'credentials',
name: 'Credentials',
credentials: {
email: { label: 'Email', type: 'text' },
password: { label: 'Password', type: 'text' },
},
async authorize(credentials: any) {
await connectDb();
try {
const user = await User.findOne({ email: credentials.email });
if (user) {
const isPasswordCorrect = await bcrypt.compare(
credentials.password,
user.password
);
if (isPasswordCorrect) {
return user;
}
}
} catch (err: any) {
throw new Error(err);
}
},
}),
],
callbacks: {
async signIn({ user, account, profile, email, credentials }) {
return true
},
async redirect({ url, baseUrl }) {
return baseUrl
},
async session({ session, user, token }) {
return session
},
async jwt({ token, user, account, profile, isNewUser }) {
return token
}
},
};
const handler = NextAuth(authOptions);
export { handler as GET, handler as POST };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment